I see lots of JavaScript references and code snippets on the forum/documentation (like this one) where the execution
object is used for a range of useful things, such as:
execution.createIncident(String incidentType, String configuration);
execution.resolveIncident(String incidentId);
execution.setVariable("name", value);
However, nowhere have I seen an example of how to instantiate the execution
object, and when I try to use it I get an error like this:
The process could not be started. :
Cannot instantiate process definition Finswitch_Tx:14:42ef803b-67df-11e8-a127-0242ac11001b: Unable to
evaluate script: ReferenceError: "execution" is not defined in <eval> at line number 7
Please could you give me an example of how to instantiate access to that object?
So a more comprehensive answer to this after some help on the Camunda forum.
According to this documentation, all process variables available in the current scope are available to the script by name, as well as some special variables: execution
, task
and connector
.
What's not clear in the documentation is that scripts in the output variables of a connector are running in a child (connector) scope, so while the connector
object is available, the execution
is not. Some for the functionality on the execution instance can be accessed like this:
var execution = connector.getParentVariableScope();
var activityId = execution.getCurrentActivityId();
However, I see the object returned from is actually an AbstractVariableScope
so I'm not sure how far this can be used.
Similarly, inside the script of a Task Listener, the task instance is available, which corresponds to the DelegateTask interface. Here the execution
instance can be accessed as follows:
task.execution.getCurrentActivityId();
Within an Execution Listener, the execution
instance is accessible directly.
execution.setVariable("testVar", true);