• SOLVED Find Active Task Owner

  • OpenKM has many interesting features, but requires some configuration process to show its full potential.
OpenKM has many interesting features, but requires some configuration process to show its full potential.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #41986  by alexwgordon
 
Hi all,

While in a workflow, I'm trying to figure out how to assign the current user of a task to a variable and I'm just not sure how to pull it out.

Basically I have a task that is initially assigned to a pooled actor group then once a person assigns that task to themselves, I want to assign that user's name to a variable – this is the "assigned to" part of the task instance (see image). I'm sure this is pretty trivial but haven't been able to figure it out.

I've tried this but it comes back null
Code: Select all
String taskActor = executionContext.getTaskInstance().getActorId();
If I do:
Code: Select all
long taskId = executionContext.getTaskInstance().getId();
I get the correct task ID for the task currently running.

Any help would be greatly appreciated!

Thanks so much!
Attachments
Screen Shot 2016-06-29 at 16.44.51.png
Screen Shot 2016-06-29 at 16.44.51.png (14.18 KiB) Viewed 6622 times
Last edited by alexwgordon on Mon Oct 24, 2016 10:41 pm, edited 1 time in total.
 #42001  by alexwgordon
 
Hi jllort,

So I looked at the link you sent and all that makes sense, but I'm not sure if I'm missing how to actually pull the actor's name out. I basically want to assign their name to a variable if possible. But using something like
Code: Select all
executionContext.getContextInstance().getVariable("Assigned To")
doesn't help me to get the assigned actor's name or id. Do you have any other advice/guidance about this issue?

Or do you have a better way to do this --> After a pooled actor assigns himself a task, how do I make the next node also be assigned to that same actor?
 #42030  by alexwgordon
 
Hi all! so I still haven't found a resolution for this? Does anyone have any ideas for how to accomplish this? It seems like
Code: Select all
executionContext.getTaskInstance().getActorId();
should be working, but it never returns a value.

Thanks all!
 #42044  by pavila
 
In the ActionHandler you can perform this:
Code: Select all
TaskInstance ti = Utils.getLastTaskInstance(ctx);
String user = ti.getActorId();
This is the getLastTaskInstance implementation:
Code: Select all
public static TaskInstance getLastTaskInstance(ExecutionContext ctx) {
	TaskInstance ret = null;
	for (TaskInstance ti : ctx.getTaskMgmtInstance().getTaskInstances()) {
		if (ret == null || ti.hasEnded() && ti.getEnd().after(ret.getEnd())) {
			ret = ti;
		}
	}
	return ret;
}
You have to call this ActionHandler from a transition:
Code: Select all
<transition to="other-node" name="confirm">
    <action class='YourImplementedAction' name="Comment"></action>
</transition>
 #46865  by dferguson
 
I have struggled with this as well, and here is what I have currently.
Code: Select all
package com.openkm.workflow.action;

import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.exe.SwimlaneInstance;

import com.openkm.api.*;
import com.openkm.bean.workflow.Comment;
import com.openkm.module.db.stuff.DbSessionManager;
import java.lang.String;
import com.openkm.api.OKMNote;
import com.openkm.bean.Note;


public class TransactionApproved implements ActionHandler {
	
	private static final long serialVersionUID = 1L;
	
	public void execute(ExecutionContext context, SwimlaneInstance swim) throws Exception {
		
		String token = DbSessionManager.getInstance().getSystemToken();
		Long processInstanceId = context.getProcessInstance().getId();
		List< Comment > commEX = OKMWorkflow.getInstance().getProcessInstance(token, processInstanceId).getRootToken().getComments();
		String uuid = (String) context.getContextInstance().getVariable("uuid");
		String commentString = "";
		StringBuilder sb = new StringBuilder();
		Formatter formatter = new Formatter(sb, Locale.US);
		
		int i = 0;
		if ( i < commEX.size() ) {
		{
			String mess = commEX.get(i).getMessage();
			String act = commEX.get(i).getActorId();
			commentString = formatter.format("%s approved with comment - %s%n", act, mess).toString();
			i++;
		}
		
		}
		else {
			{
			String act = (String) swim.getActorId();
			commentString = formatter.format("%s approved with no comment %n", act).toString();
		}
			
		}
		formatter.close();
		 try {
				Note note = OKMNote.getInstance().add(token, uuid, commentString);
				System.out.println(note);
	} catch (Exception e) {
		e.printStackTrace();
    }
	}

	@Override
	public void execute(ExecutionContext arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}
}
Everything works well excect for cases where the actor won't need to enter a comment. When I try to capture the actor in this case no matter what I tried it either came back as 'null' or a true null whuch would throw an error and not complete as I wish. How can I implement the above into my case and capture the actor doing the approving?
 #46870  by dferguson
 
I was able to figure it out, but had to get much simpler with my implementation since I basically know zero Java.
Code: Select all
package com.openkm.workflow.action;

import java.util.List;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.taskmgmt.exe.TaskInstance;

import com.openkm.bean.Note;
import com.openkm.api.OKMNote;
import com.openkm.api.OKMWorkflow;
import com.openkm.bean.workflow.Comment;
import com.openkm.module.db.stuff.DbSessionManager;


public class TransactionApproved implements ActionHandler {
	private static final long serialVersionUID = 1L;
	
	public static TaskInstance getLastTaskInstance(ExecutionContext ctx) {
		TaskInstance ret = null;
		for (TaskInstance ti : ctx.getTaskMgmtInstance().getTaskInstances()) {
			if (ret == null || ti.hasEnded() && ti.getEnd().after(ret.getEnd())) {
				ret = ti;
			}
		}
		return ret;
	}
	
	public void execute(ExecutionContext context) throws Exception {
		String token = DbSessionManager.getInstance().getSystemToken();
		Long processInstanceId = context.getProcessInstance().getId();
		List<Comment> commEX = OKMWorkflow.getInstance().getProcessInstance(token, processInstanceId).getRootToken().getComments();
		String uuid = (String) context.getContextInstance().getVariable("uuid");
		String message = "";
		String comment = "";
		String act = "okmAdmin";
		int i = commEX.size();
		
		if ( i >= 1 ) {
			act = commEX.get(0).getActorId();
			message = commEX.get(0).getMessage(); //message is built with comment from UI
			comment  = String.format("%s approved with comment - %s%n", act, message);
		} else {
			TaskInstance ti = TransactionApproved.getLastTaskInstance(context);
			act = ti.getActorId();
			comment  = String.format("%s approved without comment%n", act); //message built with no comment
		}
		
		Note note = OKMNote.getInstance().add(token, uuid, comment);
		System.out.println(note);
    }
}
 #46888  by dferguson
 
I need the owner of the task. If the task owner attaches a comment I can get the user's id from the comment list data. However, if they don't leave a comment the comment list doesn't contain any info, so I need to get the task owner from another source.

I am sure my implementation is poor since I have never seen Java before.

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.