How can I check if a variable is set?

Hi

I’m using an exclusive gateway in my workflow. This GW should decide if a service needs to be called or if the call can be skipped. (Either an ‘service result id’ was provided to the workflow or none was provided).

Currently I modeled this check with an expression on the outgoing sequence flows this way:

alternative 1: ${service_result_id != ‘0’}
alternative 2: Default Flow

But for this to work I have to set the 'service_result_id ’ artificially to 0 if the result wasn’t provided to the workflow.

What I real want to do, is to check if the variable 'service_result_id ’ is defined or not. Is there a way to achieve this?

Thanks
Alex

1 Like

Hi @albex,

you likely want ${service_result_id != null}.

Simon

Nope. If the variable is not defined in the workflow this will rise an exception. (But I will double check :wink:

I’ve actually just fought the exception during startup of a process (in my case it was because of a DMN defining the variable but the Business Rule task did not specified the result).

How do you invoke this workflow?

If the input comes from an intermediate task I’d “initialize” the variable to null before (possibly) invoking the intermediate task.

If this isn’t the case I’d use what you likely have done already - call a service task that only checks if the variable is set and if not set to to null (I still would use this instead of 0).

As said:
22-Aug-2017 15:46:19.051 SEVERE [http-nio-80-exec-4] org.camunda.commons.logging.BaseLogger.logError ENGINE-16004 Exception while closing command context: Unknown property used in expression
: ${service_result_id != null}. Cause: Cannot resolve identifier ‘service_result_id’
org.camunda.bpm.engine.ProcessEngineException: Unknown property used in expression: ${service_result_id != null}. Cause: Cannot resolve identifier ‘service_result_id’
at org.camunda.bpm.engine.impl.el.JuelExpression.getValue(JuelExpression.java:60)

I know i can use ‘null’ or maybe ‘empty’ (not sure about the second) - but I want to know if there is another way. Maybe using input/output variables etc.

As example: I saw someone using an output like: Name:result_id_global, Type:Text, Value:{#result_id}
But I do not understand yet what this is doing.

Rather than use a expression, can you a script?

doing execution.getVariable('myVar');, where myVar does not exist will return null.

4 Likes

I’m working with web API. So I need to write some inline script code in the modeler?

You would change your “implementation” of the sequence flow from “expression” to “script”.

When use “javascript” as the script format and just write a little function in the inline script field to return True or False based on your logic.

4 Likes

Nice - this did the trick:

execution.getVariable('service_result_id') != null;

Thank you!

2 Likes

@albex: For finding the good answer faster please mark it as solution :wink:

Here’s another variant of the above that I’ve found helpful that uses the ternary operator:

(execution.hasVariable("myVar") ? execution.getVariable("myVar") : "null")

This tests for the existence of the variable first and if found can use its value, otherwise it is set to null.

Another use would be short-circuiting in a conditional statement like this:

if ((execution.hasVariable("myVar")) && (execution.getVariable("myVar")) == "whatever")) { ....

5 Likes

@mppfor_manu nice suggestion! did not know that one existed!

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.7/org/camunda/bpm/engine/delegate/VariableScope.html#hasVariable(java.lang.String)

.hasVariable seems much better to use.

2 Likes

Wow! If I’ve actually provided something you didn’t know Stephen, I’m thrilled. I feel so ignorant compared to so many in this forum, so I appreciate the comment.

Thanks.

Michael

I would try ${not empty service_result_id}.

Have you tried ${myvar??} ? The double question mark usually checks in freemarker if a variable exists. I don’t know if it works on a gateway as well but you could give it a try.

Is their any other scripting other than “javascript” or “groovy” if it is their. Could you please post me an example.

What script language are you trying to use?

do you have any javascript examples?

@Smith can you open a new thread and explain what you are trying to do

1 Like