cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing Process Instance ID inside a ScriptTask

gavnunns
Champ on-the-rise
Champ on-the-rise
Hi,

I need to access the process instance id from a groovy script task.

According to the docs the script task has access to all process variables, however I'm not clear on if this includes the process instance id or exactly what name I would use to refernce it - http://activiti.org/userguide/index.html#bpmnScriptTask

Any pointers or simple examples would be greatly appreciated.

Thanks

Gavin
3 REPLIES 3

jbarrez
Star Contributor
Star Contributor
The 'execution' variable should be available. That one should have a getProcessInstanceId() method.

tstephen
Champ on-the-rise
Champ on-the-rise
Hi Gavin,

An implicit object named 'execution' is exposed to the script task. This example: https://github.com/Activiti/Activiti/blob/master/modules/activiti-engine/src/test/resources/org/acti... shows it being used to set a process variable (line 42) and there is a getVariable method too.

execution is an instance of ExecutionEntity, which I think is intended as internal (at least it does not appear in JavaDoc) but at least you can rely on it implementing http://activiti.org/javadocs/org/activiti/engine/delegate/VariableScope.html and http://activiti.org/javadocs/org/activiti/engine/runtime/ProcessInstance.html allowing you access to execution.getProcessInstanceId amongst other things.

I was surprised myself that it does not also implement http://activiti.org/javadocs/org/activiti/engine/delegate/DelegateExecution.html which is what you have access too when writing Java Service Task implementations (though it does fulfil that contract).

All the best,
Tim

gavnunns
Champ on-the-rise
Champ on-the-rise
Perfect, thanks again for both rapid and informative response! ❤️ these forums!

As stated execution object is available to the Script Task which provides you will the following;

<code>execution.getProcessInstanceId()

Provides Methods

String getActivityId()
String getId()
String getParentId()
String getProcessInstanceId()
boolean isEnded()
boolean isSuspended()