How do I get access to the "execution" object in JavaScript?

Hi Guys -

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

Similarly this post gives the following examples of logging from JavaScript:

println("Normal print output")

var logger = java.util.logging.Logger;
var log = logger.getLogger("MY_JS_LOGGER");
log.info("I'm logging");

var system = java.lang.System
system.out.println("System out println");

but again, when I

Cannot instantiate process definition Finswitch_Tx:17:9bacc456-67e6-11e8-a127-0242ac11001b: Unable to evaluate script: ReferenceError: "println" is not defined in <eval> at line number 8

Please could you give me an example of how to instantiate access to that execution object, and is there a way to log from JavaScript in a Service Task output??

1 Like

Execution is created automatically by the engine when the scripting env is generated. See the scripting docs: https://docs.camunda.org/manual/7.7/user-guide/process-engine/scripting/#variables-available-during-script-execution

1 Like

Thanks @StephenOTT, I thought this might be the case, but why do I get the error:

Unable to evaluate script: ReferenceError: "execution" is not defined in <eval> at line number 7

when this piece of code is executed in a Service Task JavaScript output parameter?

var statusCode = connector.getVariable("statusCode");
if (statusCode != 200) {
    throw new Error(connector.getVariable("response"));
}
else {
    var output = S(connector.getVariable("response"));
    execution.setVariable("testVar", true);
    output.prop("success").boolValue=="true";
}

If I take the line containing execution out, it works fine. Are these objects only available in some of the places you can use JavaScript? Perhaps they are not available for use in a Service Task output variable of type JavaScript? connector is available, though, and it is mentioned as one of the ‘special variables’ along with execution in the page you linked.

Is it the output of the http connector or the output of the task?
Execution is not available when using a connector. You have to use “connector”. This is mentioned in the scripting docs and the http connector docs.

1 Like

Thank you Stephen. I have just worked that out through experimentation. Thanks for your input, I was trying to use it in the output of the http connector.

@StephenOTT, I’ve been digging in the connector, scripting and script task docs and don’t see mention of this limitation - could you point me in the right direction? I’d like to fully understand it.

1 Like

In this section: https://docs.camunda.org/manual/7.8/user-guide/process-engine/scripting/#variables-available-during-script-execution: But not overly clear about the implications and how to visualize when you are in a specific scope.

Note that you can still access to the execution when inside of connector.

var parentScope = connector.getParentVariableScope()

context.setActivityId(parentScope.getCurrentActivityId());
context.setExecutionId(parentScope.getProcessInstanceId());
context.setProcessDefinitionId(parentScope.getProcessDefinitionId());

BUT see the following thread for more details and how its not a formal public interface, thus there are not guarantees: Error creating incident via javascript

Thanks again, great input.

Hi,

Is it possible from a child process to get the SuperExecution from the parent Call Activity with javascript?
If I write sth like:
execution.getSuperExecution().setVariable("myVariableInParentProcess", aValue);
will it work in a javascript script?
I want from a child process to update the value of a variable in the parent process (for some reasons, long-story to explain, the mapping doesn’t help me).

Thx

Yes. When you use javascript you are using the nashorn engine which gives you access to the java API. When you are using “execution” you are accessing the java API/getting a java interface/object

2 Likes