Join us on December 15 for Looking Back to the Future: 5 Process Automation Trends in 2022

Icon Close SAVE YOUR SEAT
SSO Implementation in Camunda

Q&A: The One with the Non-existent Variable

Or is it non-existent after all? You were sure you had a variable set, but your application crashed again because it wasn’t set after all. But it doesn’t happen all the time, just sometimes, making it even more maddening. So how do you stop this from happening?  

Question 3

How can I check if a variable is set?

This is an interesting question – with a couple of great answers, depending on your implementation.

First, you could define a variable that gets set when the process begins with var MyVar.

And then in your execution, you could have an expression that checks this variable ${ MyVar != null }.

But then, you’d have a variable hanging around in your process that may or may not get used. This probably isn’t the ideal solution.

If you try to use that expression without having the variable set, it will cause an exception, which is definitely not what you want.

The better solution here would be to change the implementation in the sequence flow from expression to script. Then, in the implementation box, enter a short javascript implementation execution.getVariable(‘MyVar’) != null.

Which will check if the variable exists at all, and return true if it exists (if not null).

But it turns out that there is a documented hasVariable() call that can simplify this even more. If you prefer ternary operators (execution.hasVariable(“myVar”) ? execution.getVariable(“myVar”) : “null”).

Plus, if you don’t like ternary operators if ((execution.hasVariable(“myVar”)) && (execution.getVariable(“myVar”)) == “whatever”)) { …. }.

This will handle it.

I definitely recommend this last solution as it’s a cleaner, more robust solution. It has the advantage of avoiding exceptions, checking the existence of a variable (which means you don’t have to ensure that the variable exists to begin with) and getting a value (or null) for the variable quickly and efficiently. 

Until Next Time

Give it a try and let us know if you need any additional help. Stay tuned for another popular question to be answered in this Questions that Need Answers series.

Recently, we were asked what our most popular user questions are regarding Camunda Platform and Camunda Cloud. We decided to look through our forum and Question Corner inquiries to find out, and share the collection of answers with you. 

Each month, we will work down the list and post scenarios along with their solutions to help support you with engineering excellence. Check out our previous post, The One With the Model No One Understands.