How can I check if a variable is set?


#1

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


#2

Hi @albex,

you likely want ${service_result_id != null}.

Simon


#3

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


#4

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).


#5

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)


#6

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.


#7

Rather than use a expression, can you a script?

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


#8

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


#9

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.


#10

Nice - this did the trick:

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

Thank you!


#11

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


#12

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")) { ....


#13

@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.


#14

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


#15

I would try ${not empty service_result_id}.


#16

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.


#17

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


#18

What script language are you trying to use?


#20

do you have any javascript examples?


#21

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