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

Based on the conversations in: Getting Stack Trace in Boundary Error Event - #12 by StephenOTT

Wanted to move it into its own thread for better discoverability:

Came up with the following:

Where Generate Incident is:

(Javascript)

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();
context.setActivityId(execution.getCurrentActivityId());
context.setExecutionId(execution.getProcessInstanceId());
context.setProcessDefinitionId(execution.getProcessDefinitionId());

var newIncident  = IncidentEntity.createAndInsertIncident("myCustomIncidentType", context, "A custom incident message.");

newIncident.id

I store the newIncident.id as a process variable (result variable in the script task) so in the next task i can search for it and resolve it.

The Resolve Incident task has a Execution Listener with a End type:

(Javascript)

execution.getProcessEngineServices().getRuntimeService().createIncidentQuery().incidentId(execution.getVariable('IncidentId')).singleResult().resolve();

This finds the Incident with the IndicidentId process variable as the filter and resolves it. I had to do this, because as far as i can tell there is no Web API to resolve incidents? Incident | docs.camunda.org But maybe i am missing something?
Update: found the following that shows it has been a feature request for a while: https://app.camunda.com/jira/browse/CAM-1689 (“I can create / resolve an incident through public API”)

Where .createIncidentQuery() returns a instance of IncidentEntity, and .resolve() is (IncidentEntity (camunda BPM Javadocs 7.6.13-ee))

In Cockpit there is not ability to “resolve” the incidents as a action. Would be nice to have a “generic” resolve function in Cockpit, so you can generate a Incident and resolve it without having to write and deploy custom Java.

Some screenshots of what it looks like in Cockpit:

With Two process instances (1 with a incident, and the other had its incident resolved)

Instance with the Incident:

Instance with the Incident, in the Incidents Tab:

Instance with its Incident Resolved:


References:

  1. General Info: Incidents | docs.camunda.org
  2. Example I based my search on: https://github.com/camunda/camunda-consulting/blob/master/snippets/custom-incident/src/main/java/com/camunda/bpm/demo/custom_incident/CreateCustomIncidentTaskListener.java
  3. Important!: The Incident Entity: IncidentEntity (camunda BPM Javadocs 7.6.13-ee)
  4. How i find the Entity to resolve it in another task: IncidentQuery (camunda BPM Javadocs 7.6.13-ee)
  5. Important!: How to create the Context object which is used by the IncidentEntity in #3 above: IncidentContext (camunda BPM Javadocs 7.6.13-ee)
1 Like

Code snippet if anyone wants to call this code in the future:

javascript:

function Incidents(){};
Incidents.prototype.createIncident = function(incidentType, incidentMessage) {

  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();
  context.setActivityId(execution.getCurrentActivityId());
  context.setExecutionId(execution.getProcessInstanceId());
  context.setProcessDefinitionId(execution.getProcessDefinitionId());

  var newIncident  = IncidentEntity.createAndInsertIncident(incidentType, context, incidentMessage);

  return newIncident;
};
 
Incidents.prototype.resolveIncident = function(incidentId){

    execution.getProcessEngineServices()
             .getRuntimeService()
             .createIncidentQuery()
             .incidentId(incidentId)
             .singleResult()
             .resolve();
};
1 Like

This was just added to Fix Version 7.8

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.

Note that this is out of date. If you are using new releases of Camunda see the Execution API:

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.8/org/camunda/bpm/engine/delegate/DelegateExecution.html

which has the create incident api: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.8/org/camunda/bpm/engine/delegate/DelegateExecution.html#createIncident(java.lang.String,%20java.lang.String,%20java.lang.String)

1 Like