cancel
Showing results for 
Search instead for 
Did you mean: 

Get process's initiator username from Java class

jsalvador
Champ in-the-making
Champ in-the-making
Hi folks!

I'm using Activiti with Alfresco Community (currently AC 5.1). I'm working in some workflows, and few of them have an userTask that needs to be assigneed to an especific user, taked from a database.

Example: some users need their tasks must be approved by User A, and some users need their tasks must be approved by User B. This is defined by a database (with users and assigneed reviewers).

I have a Java class into a ServiceTask, that calls a JDBC service and get reviewer for each user, but the query needs the initiator username to take it properly. Is there any way to get the initiator's username?

At this moment, I tried with this code, but I get
null
.


    String initiator = execution.getVariable("initiator.properties.userName");


(Just like
${initiator.properties.userName}
used into User Task elements)

Thanks!
4 REPLIES 4

jsalvador
Champ in-the-making
Champ in-the-making
Ok, I guess I find a solution, but it isn't the good way…

1. I added a ScriptTask with this code, BEFORE my ServiceTask:
<code>
execution.setVariable("workflow_starter", initiator.properties.userName);
</code>

2. Into my Java ServiceTask, I added the following code:
<java>
String userName =(String) execution.getVariable("workflow_starter");
</java>

And my query uses the <code>userName</code> variable to get the reviewer. Not clean, but works.

jbarrez
Star Contributor
Star Contributor
hmm, if the initiator.properties.userName is available just before the service task, it should be available to in the service task.
Is there anything special about your process why it would remove that variable?

jsalvador
Champ in-the-making
Champ in-the-making
Hi Joram, nice to see you here.

Well, at the first try, I tried to get the workflow starter's username by <code>execution.getVariable("initiator.properties.userName");</code> but it doesn't work, and I get an ActivitiException here. So I need to try other solutions. As I said, this is not the best way to do it.

Update.

And now I see the error… I wasn't casting the value as String.

alramlo
Champ on-the-rise
Champ on-the-rise

You can access to the initiator using this code in a ExecutionListener implementation:

ActivitiScriptNode initiatorScriptNode = (ActivitiScriptNode) execution.getVariable("initiator");
NodeRef initiatorNodeRef = initiatorScriptNode.getNodeRef();‍‍‍‍‍

Sorry for the delay