Groovy script to assign task operator name to a variable?

Is it possible to use a Groovy script to assign the operator name of a specific task to a variable?

The use case here is that I’m saving key data from the process to an audit table in MySQL and I need to log which user performed each step in the process.

Thank you!

Hi,
you have access to a provided variable called loggedUser.

I think it should answer your use case.

Thanks David, but I’m still not doing something right. I’ve pasted into a “operations” task to assign the username to the vAuditor_Name variable. However, I had to drop the first elements in each line (for example, the first “ProcessAPI” on line 1, the “DataInstance” on line 3, etc) as your exact code caused errors when validating the process. With the edited code, I’m now getting a warning that “processApi cannot be resolved”, so I’m not sure how I fix that. (BTW…I’m on v6.4.2 Community Edition, if that has any impact).

Hello Paul,

You have a special variable named taskAssigneeId who is availble only in Human Tasks, and who store the userId of the human to which the task is assigned to.

What I am usually doing is :

  1. create a long variable named currentUserId on the pool level

  2. on the first human task on a lane, on General > Operations > add an operation to assign to the currentUserId tthe result of an expression (here named taskAssigneeId)
    currentUserid

this expression is the following groovy script, which return a java.lang.Long :

return taskAssigneeId as long;

You can now fetch the user name in any expression using the following code :

import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.identity.User;
import org.bonitasoft.engine.bpm.data.DataInstance;

ProcessAPI processApi = apiAccessor.getProcessAPI();

DataInstance currentUserIdData = processApi.getProcessDataInstance(“currentUserId”, processInstanceId);
User user = apiAccessor.getIdentityAPI().getUser(currentUserIdData.getValue());
String username = user.getFirstName() + " " + user.getLastName()

paulfischer2000, has this worked? I am also interested in this issue.

My apologies for the newbie questions here, but I’m not sure where/how the ProcessAPI code segment should be implemented. Following David’s guidance, I’ve assigned the taskAssigneeID to a variable, but then where do I implement the ‘fetch’ routine…in a subsequent Groovy expression?

Thanks in advance,
Paul

Hello Paul,

indeed you have to use the script provided in a Groovy script: it can be a script connector defined before the sql connector, in the sql connector itself, and so on.

It depend on how your processus is organised.
Note than the taskAssigneeId variable is available in Operations and Script expressions of human tasks only.

OK…it’s working now. The issue was that I was not using the script connector as David suggested…I was trying to do it through an operator. However, I also had to trim his example code a bit, as shown here (noting that I’ve also changed the initial variable name to “vCurrentUserID” used in the currentUserIdData line):

processApi = apiAccessor.getProcessAPI();

currentUserIdData = processApi.getProcessDataInstance(“vCurrentUserID”, processInstanceId);
user = apiAccessor.getIdentityAPI().getUser(currentUserIdData.getValue());
String username = user.getFirstName() + " " + user.getLastName()

Thanks, David, for your guidance on this!!
Paul

You are welcome Paul,

I updated my answer on the second code part : you need to add the import statements, otherwise it can’t code. The operator section is a more obscure part of studio, so you should stay with more basic things like connector.

You may have a look on this exemple : vAuditorName-1.0.bos