Join our largest event of the year | CamundaCon 2022 October 5-6

Icon Close
Register
Upgrading Spring Boot maven dependencies

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?  

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.

  • A featured image that reads "Camunda Champion Program" and featuring a woman holding a microphone

    Are you the next Camunda Champion?

    Since the first day I joined Camunda (which is already over four years ago – time really does fly when you are having fun), I was amazed at how many dedicated, passionate, and invested Camunda Community members there are. Numerous times, I have met individuals who spoke with such excitement about Camunda’s technologies and what the community means to them, that you had to believe it even if you would have never heard of Camunda before. Not only are Camunda Community members passionate about the products, many also actively contribute and give back to the community in several ways. Just to name a few examples: they contribute code and help improve Camunda products, organize meetups, answer questions from fellow users...

    Read more
  • Black title slide that reads, "The CAM project in JIRA is moving to GitHub"

    The CAM project in JIRA is moving...

    For almost 10 years, JIRA has been our development issue tracker for the core components of Camunda Platform 7. About 15,000 tickets later, Camunda’s engineering has grown from one team to a team of teams. Many new projects have used GitHub exclusively as a code repository and an issue tracker from the start. In October, we will move the CAM project to GitHub issues. With this change we are reducing the gap: our community can participate in our development processes with a single account. We achieve a unified approach for managing development issues, making it easier for everyone to participate. We thank everyone who has contributed to our CAM issue tracker and hope that the collaboration continues in our new...

    Read more
  • Automation in banking: 6 considerations for digital...

    Automation and banking have been two peas in a technological pod since the 1934 introduction of the IBM ® 801 Bank Proof machine, a check sorting machine. Later innovations like the automated teller machine and the debit card continued the banking automation trend of digitizing analog processes. With the advent of the internet, machine learning, and cloud computing, there are still so many automation opportunities to explore.  For instance, using AI for risk assessment, automating the classification of unstructured data, or reducing manual work can help you increase the agility of the overall organization, improve the customer experience, and get new features and products to market much more rapidly. We’re going to explore six concepts to keep in mind for...

    Read more

Ready to get started?

Still have questions?