cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically assign a task participant

Highlighted
Level 8

Programmatically assign a task participant

Hello, all!

 

For a current workflow task, how do I programmatically add a user and assign them the current task (add them to the current task pool)?

 

So.  Adding users to a role via api.  Check.  There are a bunch of ways to do this, actually.  The two main ways were getting the team for the object and adding and getting the process team (which I figure is the same in this case?) and adding the participant.

 

I can add the participant, but the task for that role is not generating.  As always, I figure I am missing something obvious.......

 

Latest iteration I was trying:

 

private String assignUserToRole(WTChangeOrder2 coIn,String roleNameIn,String userNameToAdd){
	Vector<Role> rolesToReturn = new Vector<Role>();
	StringBuilder dataToReturn = new StringBuilder();
	
	try{
		Team curCNTeam = TeamHelper.service.getTeam(coIn);
		rolesToReturn = curCNTeam.getRoles();
		for(Role curRole:rolesToReturn){
			if(curRole.getDisplay().toLowerCase().contains(roleNameIn.toLowerCase())){
				
				QuerySpec qs = new QuerySpec(wt.org.WTUser.class);
				qs.appendWhere(new SearchCondition(wt.org.WTUser.class, wt.org.WTUser.NAME, SearchCondition.LIKE, userNameToAdd,false), null);

				StatementSpec statementSpec = (StatementSpec) qs;
				
				QueryResult qr = PersistenceHelper.manager.find(statementSpec);
				
				if (qr.size() > 0) {
					wt.org.WTUser user = (wt.org.WTUser) qr.nextElement();
					TeamHelper.service.addRolePrincipalMap(curRole, user, curCNTeam);
				}
			}
			
			
				}
		}
	}
	catch(WTException e){
		e.printStackTrace();
		dataToReturn.append("Error adding " + userNameToAdd + " to role " + roleNameIn);
	}

	return dataToReturn.toString();
}
1 REPLY 1

Re: Programmatically assign a task participant

Hi Brian,

 

Workflow tasks can be modified if you can get the WfAssignment. From the WfAssignment you can try the following:

 

 

wfAssignment.setAssignee(WfAssignee assignee);

 

I believe that will work. Getting the WfAssignment itself can be difficult though. You'll have to query for the workflow task name:

 

 

 /**
* This method returns all workflow activities of the given name from the
* given workflow
*
* @param self This is an ObjectReference to a workflow.
* @param activityName This is a String that contains the name of the workflow
* activity to find. An example is "Submit Change Notice".
* @return Latest of WfAssignedActivity that the method searched for.
*/
WfProcess proc = (WfProcess) self.getObject();
Class<WfAssignedActivity> qc = WfAssignedActivity.class;
QuerySpec qs = new QuerySpec();
int idx = qs.addClassList(qc, true);
SearchCondition sc = new SearchCondition(WfAssignedActivity.class, "parentProcessRef.key.id",
SearchCondition.EQUAL, proc.getPersistInfo().getObjectIdentifier().getId());
qs.appendWhere(sc, new int[]{idx});
QueryResult qr = PersistenceHelper.manager.find((StatementSpec) qs);
WfAssignedActivity activity;
Timestamp modtime=proc.getCreateTimestamp();
while (qr.hasMoreElements()) {
Persistable[] ar = (Persistable[]) qr.nextElement();
activity = (WfAssignedActivity) ar[idx];
if (activity.isComplete()) {
if (activity.getName().equalsIgnoreCase(activityName) && activity.getModifyTimestamp().after(modtime) ) {
wfpvReviewActivity = activity;
modtime=activity.getModifyTimestamp();
}
}
}

 

You'll have to clean that up a bit but that should return the workflow activity. Next you can use:

QueryResult wfAssignments = (QueryResult) wfpvReviewActivity.getAllAssignments();

This will get a QueryResult of all assignments. You can then iterate through to get the right one.

 

I'm not certain that this will work but it might help you get on the right track.