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

Having trouble setting your variable sometimes? Find out how to check if your variable is set and why it can occasionally appear non-existent.
By
Upgrading Spring Boot maven dependencies
  • Blog
  • >
  • Q&A: The One with the Non-existent Variable
TOPICS

30 Day Free Trial

Bring together legacy systems, RPA bots, microservices and more with Camunda

Sign Up for Camunda Content

Get the latest on Camunda features, events, top trends, and more.

TRENDING CONTENT

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?  

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.

Try All Features of Camunda

Related Content

An integral part of process orchestration is process automation—get those repeatable, well-understood tasks that don't require complex decision-making on autopilot!
Enhance your business's operational efficiency with business process management, and streamline your workflows to reduce cost and minimize risk.
Transition smoothly from design to implementation with an end-to-end business process. We'll show you how!