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.
@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>
// 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());
...
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.
@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…
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.