# IF statement resolving non-existing variables

**URL:** <https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138>\
**Category:** Robot Framework\
**Created:** [9 December 2024 08:35 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138 "2024-12-09T08:35:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [9 December 2024 08:35 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/1 "2024-12-09T08:35:13Z")

</div>

Hello,

During testing I ran across a point where I wanted to improve the terminal command I run.  
In this case I’d like to do an IF statement on a variable that used to be passed as --variable in the terminal.  
When the --variable doesn’t exist I want a VAR to be set with the scope GLOBAL, otherwise I want the --variable passed through the terminal to be used.

`IF not ${CHECK_DATA_EXISTS} VAR ${CHECK_DATA_EXISTS} N scope=GLOBAL ELSE IF ${CHECK_DATA_EXISTS} VAR ${CHECK_DATA_EXISTS} ${CHECK_DATA_EXISTS} scope=GLOBAL ELSE FAIL Not defined if it should be checked if the data exists`

However I get this error and I can’t seem to find a way around it:  
`Invalid IF condition: Evaluating expression 'not ${CHECK_DATA_EXISTS}' failed: Variable '${CHECK_DATA_EXISTS}' not found.`

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [9 December 2024 08:57 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/2 "2024-12-09T08:57:25Z")

</div>

Hello,

Rather than `IF not` you could use the [Variable Should Exist](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Variable%20Should%20Exist) built in keyword. Then catch the failure with a TRY/EXCEPT block or via [Run Keyword And Return Status](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Run%20Keyword%20And%20Return%20Status).

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [9 December 2024 08:59 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/3 "2024-12-09T08:59:20Z")

</div>

You might also be able to do it with the `&{OPTIONS}` [automatic variable](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#automatic-variables).

---

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [9 December 2024 10:38 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/4 "2024-12-09T10:38:26Z")

</div>

So, I feel like this answer has gotten me a step closer to what I want to achieve, thank you for that!

However, right now my TRY/EXCEPT block only works when it includes a variable that does not exist.  
I want it to work at both times, so when a variable does exist and when it does not exist.

```
TRY
        Variable Should Exist ${VARIABLE} msg=False
    EXCEPT False
        VAR ${VARIABLE} ${SUITE_VARIABLE} scope=SUITE
    EXCEPT PASS
        VAR ${VARIABLE} ${VARIABLE} scope=SUITE
    ELSE
        FAIL ${MESSAGE}
    END

```

Is there a way to do that in this TRY/EXCEPT block?  
or should I remove the second EXCEPT and replace the variable setting to the ELSE clause?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [9 December 2024 12:01 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/5 "2024-12-09T12:01:42Z")

</div>

Hi,

As you set _False_ in the first except, did you try setting _true_ for the second one instead of PASS?

As Variable Should Exist might return a boolean that should work.  
Or check exact return status/value of the keyword when it passes. You override the error message with false so first except fit, but the PASS might not be the right syntax.

Regards  
Charlie

---

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [9 December 2024 13:09 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/6 "2024-12-09T13:09:34Z")

</div>

So, what I’ve done in the meantime was exactly like I said in my second question.  
I removed the original ELSE clause, and replaced the second except to the ELSE clause.

This does work as intended.

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [9 December 2024 13:11 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/7 "2024-12-09T13:11:41Z")

</div>

Yes, if the variable exist, then the `Variable Should Exist` keyword will not fail and no exception is raised. Therefore, it goes to the ELSE block.

Something like that should work

```
TRY
    Variable Should Exist ${VARIABLE} msg=My Custom Error Message
EXCEPT My Custom Error Message
    VAR ${VARIABLE} ${SUITE_VARIABLE} scope=SUITE
ELSE # KW did not fail
    VAR ${VARIABLE} ${VARIABLE} scope=SUITE
END

```

---

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [9 December 2024 13:12 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/8 "2024-12-09T13:12:49Z")

</div>

Now I did walk into another problem with setting the variables.

As you can see, when I set the VAR when no variable existed in the first place I try to assign it to a suite scope, however this does not work when it comes to a datalist within a .resource file.

I did try to change the VAR scope to global, but even then the logs show me that the moment the datalist is gathered from the resource file an error occurs because it could not find the variable I set in the TRY/EXCEPT clause.

This clause is always run through a keyword that is included in the suite setup, so I expected this variable to be set in each test when they are in the same suite.

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [9 December 2024 13:29 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/9 "2024-12-09T13:29:05Z")

</div>

Sounds like it should be working…

I do not quite get what you mean by

> this does not work when it comes to a datalist within a .resource file

Can you reproduce with a simple example? I suggest that you share it via the [code playground](https://robotframework.org/code/?code-gh-url=https://github.com/Snooz82/robotframework-code-examples/tree/main/Examples/TwoRobotOnePyOneResource/).

---

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [10 December 2024 08:43 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/10 "2024-12-10T08:43:30Z")

</div>

Hello Francis, I’ve set up a simple example here: [Robotframework Example](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZRAGhABMY0BjAJwEsAHDSrMBEfEAM0oBsSEBtUdrgDlocRABUSGAMopKGGADpyWAEZYMLUg3lgNiAFSGABFJgY6YAOZojh-QB0wMuTBNmU1I17cYj8zDBgAG6UymCwuo6OdkYSmEZIEGgktoaOYkgADJkAjI7eRgAyWJYFAJJG0H7kAJ6UVn5YRpYQGAAWMOR+HUb1rE0QYIRGUBAA1q5oKOSu7a5BEFQQKlxGACTA-hjkKGAAvr02yb5JRgBK+d4A4q0dXWVg-VFg0cYA0jA1AO5Y5IQ2dkcNzm90eWEuXh4AEFyJYUBEMGgALoFDboToHbwbLS7bY1TFeDascgiH7kMYE9abSQ7fYQoolVHAdHkSnYrC42ps4DE0m-ClMra0vb04qlLHUzDC+BGNBtDkcYYqSZmSo2M5GQgoWZNAC0uoWSxWrjA6iMyvqpWOvTA3VmnSg9QgHHpUnlKEVRgAQq4AKIARxQzsFNN2lLOID2eH4nBgwlgTAm31+-yUJA55FIcAIWl0gT0IBipnMloBaTAhUoKhJtQKAAUau0GB9k380M8Yi2yf9Ug4nKqtoEQmEEfSeAARLCkeH51r0MAo7xiNqUI7uz3WubNDiqYNbIxD0IMBFGQ2UZarfpdMS+qRiM4AVUE9PvAE16QUAGqLc-Gkzr4ZfQAD1XXwJRvO9H0EEVbW8X0AA0kF9OsxA-bxPyhDVwNve8n3DAoyCwagYAAXiuQoAHkvShQp6V9QopF9NCvAwrDCWACDcOgplOKgylCOIsjKOo2jYK8X1BHHRxI2jNhY3jUQQDPC8SDTNAMyzTRtHzJgYm-I0uDLPsAAFgHHKExChHJKRZEjCl9CjBAKHFdFqEjBDMO4OEGHtvF5WAyTGEizlUdR-JgQKCiFXYSI2Xi8McAAyDislySlTPMyzrOkvYkQIYgqCCGBCDrZQACsYFIPRtm1AhlDUDBP06NB5yYAB2BQck6lh6vUGFrAQYAoxAGZA1CGAETQXgkT2IA)

I’ve also included log parts where I explained my expectations of what should happen.

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [10 December 2024 09:14 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/11 "2024-12-10T09:14:14Z")

</div>

Hello,

I had a look at your code, and [made a couple of small fixes](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZRAGhABMY0BjAJwEsAHDSrMBEfEAM0oBsSEBtUdrgDlocRABUSGAMopKGGADpyWAEZYMLUg3lgNiAFSGABFJgY6YAOZojh-QB0wAJRJYU5UjCPejAaxgAngDuWOSEaEqu7p4+PgDERgBiAJIAGgCiACJGGAAWlDZQBWiUVn6BIWFGlFDUoRiOMnJephgo1LGtOZIwYABulMpgsLqOjnZGEphGSBBoJLaGjmJIAAyrAIyOsQAyWJaxyUbQOeQBpQfYRpYQeTDkOblepaxYx2CERlAQ-kZo7l47kY+hAqBAVFwjAASYDyTDkFBgAC+1Rs8wwxxsTliXD6MA4AF4kAB5QRSYk7dLbHwAcVuTweyTAr1iG1iRgATOyAMyxBIpDLZPIFL7FC7HciWFAjDBoMZgcbGADSFVC4UWDjAdLujOZWGp3h4AEFJdLerKALqxGHoe4onwwrSIjBne3eGGscgiSq+N3eflpLKPEUqAJGcgwKBYAZlOEuxFGAAU6OOGP+zT+ZnaAEoDUY9gcHcBbeQUY63LpXdDgJ7vaFfdW4wjkeycTA8YSSWSKVSwLt9tbYZJm0j4H9cm4OJ8VF4U3MjNjCChAW8ALSrkFgiFeMDqIwz8Up0qPQH3IqQDit7y4-FE0nkyl5qQTlBTowAIS86QAjigIJeiybRE-ScEAkTwfhOBgYRYCYfxgjVCIIzQNwPDgAgtF0c0mAmFUELCGw7EaMxukwXoBiGGU8x4TIsFIM1dFuegwCtHwxHyNEXzfFMgUsDhVH-UiMXIwYGBlYFQUocFIVeB4xHSKQxCcABVQQ8yUgBNPNYgANUk6SWi4z50gADwKDEi3kxSVMEJE83SVIkHSAAFMRtJ8HSjWxSyFKU1SQNiMgsGoGACRpHZiXfI0dnsnYpF7K8jE87z3WAKy-Nswd0psv0gpCsKIqimK+x8dJBEyRwwItAhiCoPFCGc5QACsYFIPR4xgAhlDUDAdPuEoGCYAB2BQNlGlhuvUE1rAQYBwJACNf0GSNzTQXgLSRIA).

Now the logged text shows the value set because the variable does not exist.

To summarize, the error was due to one of the keyword arguments being named with the same name as the suite scope variable.

Does that close your issue?

Note that if you want to share the same playground code again, make sure to rewrite (copy paste) all the files before clicking share, otherwise the initial code will be there instead of yours.

---

<div class="post-metadata">

**Author:** ![Leon2699](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/leon2699/32/4377_2.png) [@Leon2699](https://forum.robotframework.org/u/Leon2699)\
**Post date:** [10 December 2024 10:26 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/12 "2024-12-10T10:26:53Z")

</div>

[changed some things I had seem to forgotten](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZRAGhABMY0BjAJwEsAHDSrMBEfEAM0oBsSEBtUdrgDlocRABUSGAMopKGGADpyWAEZYMLUg3lgNiAFSGABFJgY6YAOZojh-QB0wAJRJYU5UjCPejAaxgAngDuWOSEaEqu7p6OLmhuHl4+AG4QVBAqXBHkUYk+RgDERgBioZbqRthGEISElQAWlDbUMOTxkBwcAY6OMnJephgo1PmDRhKYMGDJlMpgsLo9YHbjkkZIEGgktoaOYkgADAcAjI75ADJYlvkAktVQleQBlFaVWEaWEBj1rQ1eL6x3hAwHUoBB-EY0O4vN8vKl0pkvAASYDyTDkFBgAC+RiakLM1RsTnyXGSMA4AF4kAB5QRSannACiZx8AHEvj9yEYbmBAfkAALAfZHY5YpaOFYAaUCITCNjsjnZsK5PMBLO8PAAguRLCgFhg0ABdfIo9CtHE+FFaTEYJ4W7wo1jkESy3z2owotG2zHuopSepuDiEMAAcgwlUkGLARhU-zApA4KGIdW+eLosGqykxdUIKBh7zkMZgL2uECM1DS4awrD+RkIlFI2EgTwA-EZ1UZLtdLcAzeQcVa3Lo7R7gE6XaE3aOvVH3fkjKTyVTafSmR2uybUZGffBIQGUEGi-jw5sjMTc-mjABaK-wygZLhGMAVWMl4+46OwiPkKAvCAcecfEXSkaTpBlmWjHx-UDOoACEvEZABHFB-03GcfXyJwQCxPB+E4GBhFgJh-GCUJwkieJojgAgtF0KY9BAKUZTI+VdjAMYJh0GY5n1DseAAESwUg9Xor56DAY0fDERobDQfdDy2cMv0sDhVFQr0jCmbiGH1Iw7wfLxAS5MRGSkMQnAAVUEDtzIATQ7fIADU0nvRETHkupGQADyacMexMszLMEMVIO8RkAA0kEZAAFMQHJ8RzNWJfzTPMqz3WS7wyCwFoKVZc5qVgzVzg7RlzikCDAO8RLMtHAK0uCzd6qC91sty-LCuK0rBH4xxsNwth8MI0QQH0xFslyTxNG0eimBWZyESyHYHDAAAyIV+JOd0+wpc4YAYfJrWHAIKUEMxOQ4YFwnycdYFdCknFUCpimdO7J3ydCwApFFmvSxxBWFLams20VsMNAhiCoMlCGi5QACsYEbBBvRgAhlDUDBHNaNBxKYAB2BRjkJlh0fUbVrAQYAcJAHJkNmGB9TQXhDSxIA), as you can see in the terminal, the ${testrun} isn’t being resolved even though it is being set through the suite setup. could this problem possibly occur because the .resource file is loaded in before the suite setup is ran?

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [10 December 2024 12:31 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/13 "2024-12-10T12:31:29Z")

</div>

Yes it would seem so, `${TESTRUN}` is only set at suite setup, but this occurs after importing the `variables.resource` file. So that the variables `&{TD01}` and `@{TC001}` silent fail to resolve…

I believe [it is not great that this import silently fails](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZQAQGMsATOAGhBLXwCcBLABw1qzARBHIDNaAbGNBAG1Q3PgDlocRAGUUtDDACMAOmpYARlgwcQhMAv1sAVCdzSYGJmADmaXCaMAdMACV+WFNXwxcvs3IUAJlUNLWdnWXkfcwwUej8zC38o3AA1CDoIdT5wsGcHXAAVfgxcJAg0fnsTZ3TM7J9FXDQACw8eIlwYAA9aTGcEutosvjM2lA7cAFFezASAEmBUgEEXAEllgCEAGSmAfUUAX1yhkZ9A5vHJnr6MAb9ThrH2zpnbhaXVjZ39wOO8sAFADSMAAngB3LDUIh2BwRJKRBRpDLDBr3XwrFx+RYAWQAmntpABVNaFfaY767Q4JCC4ABuEB4KB8CSoWHoMAAvMTSVNnCBDqQRLwYBJYGxETBgmpNNpyHoDNpEAVHnxYTUwIsKVtdgdqX4ABIwag+Pq4XEEnlkvban7-LVfHW-fW+ZZgLQtY30xnMgUAXXIJDodJgRAACmoAFYwfBKziMyrkGVaVLGtDMViIADsyhUih0yYwy2otgQwEFIBNAEc5CbYPoBPBBH7DkA)… Probably @pekkaklarck already knows about this. Why does it not fail at resource import? Is there a way this could work and the `${MY_SUITE_VARIABLE}` suite variable is filled in later in the already imported variables? Or make the substitution for this variable more of a last moment thing: when the variable is used rather than atthe resource import?

But to come back to @Leon2699 specific case, I would [suggest the following fix](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZRAGhABMY0BjAJwEsAHDSrMBEfEAM0oBsSEBtUdrgDlocRABUSGAMopKGGADpyWAEZYMLUg3lgNiAFSGABFJgY6YAOZojh-QB0wAJRJYU5UjCPejAaxgAngDuWOSEaEqu7p6OLmhuHl7eAG4QVBAqXBHkUYmOjjJyXqYYKNQ+JmZGEpgwYMmUymCwuvlgdtWSRkgQaCS2ho5iSAAMIwCMjhUAMliWFQCSRtBGGOQBlFarWEaWEBgAFjDkq0dGm6w7EGCERlAQ-kZo7l6HXqnpmV4AJMDymOQUGAAL7nGx9DDLGxOCpcZIwDgAXiQAHlBFIUdMAKJTHyzeYANTSlAyWVx3iJny4JgObg4tyxAA9KJgKr8xAARCbA8lGSkkr40ukM5msnzs0bc3n46o7JAMeLU8XAYZjcY8sAVADi+yOJwWYEuFQAZCqueqKv81kDEeysVIxE4AKqCDVtDoAaUCITCNjsjh1b31hqwvJ4AEFyJYUC0MGgALps4DoY6g5VaIFrAJp7y-VjkEQ+3w5oy-K2AkHSuZJlPkUG-DO6db14D5wuhYulv6SCslipGOEI5FojHYqvzZXloHA+BPWkoelGFReCFQowwwgoV47AC0O4+AupYHUS5gm3mq82p1exygmwgHH7PkHSNR6MxOM1Pik88XACEvCxABHFAHyTKcQQqJwQGBPB+E4GBhFgJh-GCUJwkieJojgAgtF0Oo9BAT1vXQv1BjAEpOlqepGgYWMww5LBSBjAj9noMBEx8MQDhZOdhSeKo3l2DhVDAq0jDqBomljIwD1JLxLhOMR7UdF1eUdABNXkKn5eShQXEUWUhZVlIdZ1XV5LEAA0kCxAAFMRtJ8AlwxhEyVPMks3O8MgsGoGBES1aYUT-cNpks6YpE-J8KVcpNTNU114o8l0S18-zAuC0Lwq-bwsUEDlHBguC2AQpDRBAOSvmyXJPE0bQCKYDpdOqgYHDAU1OW5Cpa0RaYYAYCpGyzRFBDMPUOGucIKjbWAi0RJxVBPAAxAs5o7bwAGJvAg20VRSpKfG2qqsmWHIjGPSFV2XUgIBTLsEs8oxCCwfpLok0UMEcAABFVJQtEzzWBGD4wIYgqHhQg7OUAArGBSD0a0YAIZQ1AwAljjQdimAAdgUcZ8ZYVH1EjawEGAWCQByEDGhgWM0F4eNgSAA) if that matches your needs:

- expand `${TD01}` dictionary rather tahn `${TC001}` list (which by the way has only one item)
- pass `${TESTRUN}` separately

Another work around could be (needs testing, which I don’t think can be done on the playgroud) to use [initialisation files](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#suite-initialization-files), in there you would run the suite setup in the suite directory, before the import of `variables.resource` file in the suite file.

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [19 December 2024 10:51 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/14 "2024-12-19T10:51:51Z")

</div>

Could you clarify what you mean with the import silently failing? I see there failures about creating variables, but that doesn’t mean that the import itself would be considered failed by Robot. Import fails for real, for example, if the file isn’t found at all.

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [19 December 2024 10:58 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/15 "2024-12-19T10:58:03Z")

</div>

Probably the easiest way to check does a variable exist and to use a default value if it doesn’t is using the BuiltIn keyword `Get Variable Value`.

```python
${value} = Get Variable Value $var default

```

Using the `$var` syntax here isn’t absolutely necessary, but it’s highly recommended as resolving `${var}` depends on its value.

We’ve also been thinking about adding a possibility to [specify a default value when accessing a variable](https://github.com/robotframework/robotframework/issues/4712). The syntax would be `${var=default}` that we [already support with environment variables](https://github.com/robotframework/robotframework/issues/3382).

---

<div class="post-metadata">

**Author:** ![FrancisG](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/francisg/32/4247_2.png) [@FrancisG](https://forum.robotframework.org/u/FrancisG)\
**Post date:** [23 January 2025 07:24 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/16 "2025-01-23T07:24:33Z")

</div>

Yes, this might not be an import issue. But I still think the error is arriving too late.

The code is simple [here](https://robotframework.org/code/?codeProject=N4IgdghgtgpiBcIDCBXAzgFwPZQAQGMsATOAGhBLXwCcBLABw1qzARBHIDNaAbGNBAG1Q3PgDlocRAGUUtDDACMAOmpYARlgwcQhMAv1sAVCdzSYGJmADmaXCaMAdMACV+WFNXwxcvs3IUAJlUNLWdnWXkfcwwUej8zC38o3AA1CDoIdT5wsGcHXAAVfgxcJAg0fnsTZ3TM7J9FXDQACw8eIlwYAA9aTGcEutosvjM2lA7cAFFezASAEmBUgEEXAEllgCEAGSmAfUUAX1yhkZ9A5vHJnr6MAb9ThrH2zpnbhaXVjZ39wOO8sAFADSMAAngB3LDUIh2BwRJKRBRpDLDBr3XwrFx+RYAWQAmntpABVNaFfaY767Q4JCC4ABuEB4KB8CSoWHoMAAvMTSVNnCBDqQRLwYBJYGxETBgmpNNpyHoDNpEAVHnxYTUwIsKVtdgdqX4ABIwag+Pq4XEEnlkvban7-LVfHW-fW+ZZgLQtY30xnMgUAXXIJDodJgRAACmoAFYwfBKziMyrkGVaVLGtDMViIADsyhUih0yYwy2otgQwEFIBNAEc5CbYPoBPBBH7DkA). But in actual tests it might be tricky to work out why `${VARIABLE_2}` is defined but not `${VARIABLE_1}`.

Apparently `${VARIABLE_1}` is not defined because `${MY_SUITE_VARIABLE}` is only defined in the suite setup (so actually available by the time we try to use `${VARIABLE_1}`). So I am not quite sure when the issue should be raised if not at import.

Actually, I don’t get why it does not work at all. Scope usage seems good. I don’t know when the variable is actually defined/evaluated.

Do you want me to open an issue on Github, and have a dedicated discussion there?

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [23 January 2025 10:12 UTC](https://forum.robotframework.org/t/if-statement-resolving-non-existing-variables/8138/17 "2025-01-23T10:12:47Z")

</div>

`${VARIABLE_1}` cannot be created because `${MY_SUITE_VARIABLE}` isn’t available at that time. That’s by design and I don’t think changing the design is feasible.

The following example demonstrates a similar situation. The `${Y}` variable is created based on `${X}`, but its value isn’t changed if `${X}` is changed.

```robotframework
***Variables***
${X} x
${Y} ${X}

***Test Cases***
Example
    Log ${Y}
    VAR ${X} z scope=suite
    Log ${Y}

```
