How can I get the username in the task script?

Viewed 76

I have a task within a workflow that invokes a service on the back end that, along with other data sent from the task, waits for the name of the user who executed that task.

How can I get the user or the username in the task script which is written in Groovy?

2 Answers

You mean a script task following a user task? Assuming you would like to access the name of the user who completed the previous user task from your script, you can store the user name on completion in a process data as shown here: https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L23

You can then subsequently access the process data in the groovy script task like this: https://github.com/rob2universe/bpmrun-add-to-dockerimg/blob/e2dd62122b7d006a8a719be96b0a3d043efc9851/configuration/resources/groovyprocess.bpmn#L18

You could either get the task assignee as mentioned by @rob2universe, or make use of the execution object, which is always accessible from the script (see Camunda Docs), as follows:

user = execution.getProcessEngineServices().getIdentityService().getCurrentAuthentication().getUserId()

See IdentityService.

The username can be then saved to a process variable:

execution.setVariable('username', user)

And accessed:

username = execution.getVariable('username')
Related