Error creating incident via javascript

Hi all,
I’m trying to create an incident using the example here by @StephenOTT.

I’m getting this error
org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script: ReferenceError: "execution" is not defined in <eval> at line number 9

here’s my code, put inside a http-connector output parameter (javascript). There is some other if/else javascript code to determine when to run the below code and that has been omitted:
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("httpPostFailure", context, "Error message"); newIncident.id

I’ve tried changing things to “task.getCurrentActivityId()” and so on, and “connector.getCurrentActivityId()” and so on, and I still get an error.

I believe it has something to do with the fact that the code is inside an http-connector config.

How can I get the incident creation working?

Can you try executing the code in a script task after your http connector. Just see if you can execute it or if it’s a specific issue with the code

@StephenOTT - Ok so I’ve executed the code in a standalone script task and it executed without incident.

Here’s the incident error output I get when I use ‘connector’. I can see that this has a different error than when I use ‘task’ or ‘execution’ but I don’t know what this error means either.

org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script: TypeError: connector.getCurrentActivityId is not a function in at line number 9

Here’s the incident error output when I use ‘task’

org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script: ReferenceError: “task” is not defined in at line number 9

Here is the entire http-connector service task xml with execution listener, using the “connector” prefix. It’s located inside an event subprocess.

  <bpmn:serviceTask id="Task_1k6348" name="Do ABC">
    <bpmn:extensionElements>
      <camunda:connector>
        <camunda:inputOutput>
          <camunda:inputParameter name="url"><![CDATA[http://my-url.com]]></camunda:inputParameter>
          <camunda:inputParameter name="method">POST</camunda:inputParameter>
          <camunda:outputParameter name="errorCheck">
            <camunda:script scriptFormat="Javascript"><![CDATA[// fetch variables
var statusCode = connector.getVariable("statusCode");
// 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();
	context.setActivityId(connector.getCurrentActivityId());
	context.setExecutionId(connector.getProcessInstanceId());
	context.setProcessDefinitionId(connector.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
}]]></camunda:script>
          </camunda:outputParameter>
        </camunda:inputOutput>
        <camunda:connectorId>http-connector</camunda:connectorId>
      </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_0ix3bb2</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_1mo6n8k</bpmn:outgoing>
  </bpmn:serviceTask>

Is there anything else can I try?

I remember seeing a similar bug related to execution not being available in output…

can you run your code in a execution listener?

@Rob1 so your problem looks to be specifically the connector.getCurrentActivityId() usage. Its because if you look at:

https://docs.camunda.org/manual/7.7/user-guide/process-engine/scripting/#variables-available-during-script-execution bullet 3, you will see link to:

connector variable scope: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.7/org/camunda/connect/plugin/impl/ConnectorVariableScope.html

that does not appear to have the methods you need. Looking to see how you get access them

@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

@thorben this usage (assuming its correct with accessing the variables through connector.getParentVariableScope(), would be a good thing to add into the docs and explain the chain/path it takes to get there. I would think in bullet #3 in: https://docs.camunda.org/manual/7.7/user-guide/process-engine/scripting/#variables-available-during-script-execution would be a good small note to make about connector allowing access to the parent scope through connector.getParentVariableScope(), it is very unclear that it would function given the deep layers it takes to get there…

@StephenOTT - That resolved it, it’s working now. Thank you so much!

Hi Stephen,

I consider the workaround you found rather a hack, because it involves internal API (implcitly by calling parentScope.getCurrentActivityId() etc. where parentScope in java is declared as AbstractVariableScope, while #getCurrentActivityId is a method declared on its subclass ExecutionEntity; so the implicit cast (in Java terms) is where you are leaving public API). Thus, I would not document it.

I’d rather we make execution available in connector parameters. This would also make implementing this use case more straightforward. Feel free to raise a feature request for that and/or provide an implementation.

Cheers,
Thorben

1 Like

jira reference: https://app.camunda.com/jira/browse/CAM-8937

1 Like

Note that this thread is mostly stale as execution.createIncident() has mostly replaced the above comments

1 Like