Error creating incident via javascript

@Rob1

there we go:


Nice usage find!

so basically update your script to the following:

// fetch variables
var statusCode = connector.getVariable("status_code");
// take action based on variable value
if ( statusCode < 200 || statusCode >= 300 ) {
	// there is an error, so create a camunda incident
	var IncidentEntity  = Java.type('org.camunda.bpm.engine.impl.persistence.entity.IncidentEntity');
	var IncidentContext = Java.type('org.camunda.bpm.engine.impl.incident.IncidentContext');
	var context = new IncidentContext();

  var parentScope = connector.getParentVariableScope()

	context.setActivityId(parentScope.getCurrentActivityId());
	context.setExecutionId(parentScope.getProcessInstanceId());
	context.setProcessDefinitionId(parentScope.getProcessDefinitionId());
	var newIncident  = IncidentEntity.createAndInsertIncident("incidentName", context, "Content message");
	newIncident.id
} else if ( statusCode >= 200 && statusCode < 300 ) {
	// no error, so do something with the response
	// get response data
	var var1 = S(response).prop('var1').value();
	// set variable
	connector.setVariable("var1", var1);
} else {
	// no status code, so do nothing
}

see the lines:

...
var parentScope = connector.getParentVariableScope()

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

This works by using the connector.getParentVariableScope method as shown here: ConnectorVariableScope (Camunda BPM Javadocs 7.7.10-ee)

and that returns the AbstractVariableScope: AbstractVariableScope (Camunda BPM Javadocs 7.7.10-ee)

and that Class has Direct Known Subclasses: CoreExecution: CoreExecution (Camunda BPM Javadocs 7.7.10-ee)

which implements: PvmExecutionImpl (Camunda BPM Javadocs 7.7.10-ee)

which has: PvmExecutionImpl (Camunda BPM Javadocs 7.7.10-ee)

pretty sure thats the path it takes. but maybe someone from @camunda can confirm ;).

Either way, but adding var parentScope = connector.getParentVariableScope() you then have access to all of the parent level items that are available in the activity/task.

@Rob1 can you also update the Thread title to be more descriptive and change from “instance” to “incident”. Will make findability better, as I think this is a important usage nuance for scripting.

3 Likes