Create and Resolve Incidents through Scripts(Javascript examples) (Internal API)

Code Snippet update for any future references:

If you try and call this code through a connector such as HTTP-connector (example: if Status Code does not equal 200 then create an incident), code adjustments are required:

when calling the following code in the context of a HTTP connector output variable,
the execution variable will not be available as per: https://docs.camunda.org/manual/7.8/user-guide/process-engine/scripting/#variables-available-during-script-execution bullet number 3 which says that the “connector” variable will be available. The connector variable points to: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.8/org/camunda/connect/plugin/impl/ConnectorVariableScope.html.

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

when accessing the ConnectorVariableScope it is a “variable” scope rather than a execution scope. So the methods such as getCurrentActivityId are not available.

see: Error creating incident via javascript for example of the issue.

The fix is to get the parent scope:

update the code for the following:

...
var parentScope = connector.getParentVariableScope()

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

see Error creating incident via javascript for further explanation and usage.