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

Code to check roles in a workflow

Highlighted
Level 12

Code to check roles in a workflow

I have the code (below) to check if a role has someone assigned to it or not.  What I need is how to change the code to check for two different roles to make sure that they are both assigned.

 

wt.workflow.work.WfAssignedActivity act = (wt.workflow.work.WfAssignedActivity) self.getObject();

	wt.workflow.engine.WfProcess pro = (wt.workflow.engine.WfProcess) act.getParentProcess();
	wt.team.Team t = wt.team.TeamHelper.service.getTeam(pro);
	java.util.Map members = t.getRolePrincipalMap();
	java.util.Set keys = members.keySet();
	java.util.Iterator itr = keys.iterator();
	while (itr.hasNext()) {
		wt.project.Role role = (wt.project.Role) itr.next();
		System.out.println("Role-=-=-=" + role);
		if (role.toString().equalsIgnoreCase("MyCustomRole")) {
			java.util.ArrayList users = (java.util.ArrayList) members.get(role);
		//If there is no user in MyCustomRole role throw exception
			if (users.isEmpty()) {
			throw new Exception("Select user for " + role.getDisplay());
			}
		}
	}
3 REPLIES 3

Re: Code to check roles in a workflow

Hello,

You can consider something like that:

java.util.ArrayList<String> rolesToCheck = new java.util.ArrayList<String>();
rolesToCheck.add("MYCUSTOMROLE");
rolesToCheck.add("MYCUSTOMROLE2");

wt.workflow.work.WfAssignedActivity act = (wt.workflow.work.WfAssignedActivity) self.getObject();

wt.workflow.engine.WfProcess pro = (wt.workflow.engine.WfProcess) act.getParentProcess();
wt.team.Team t = wt.team.TeamHelper.service.getTeam(pro);
java.util.Map members = t.getRolePrincipalMap();
java.util.Set keys = members.keySet();
java.util.Iterator itr = keys.iterator();
while (itr.hasNext()) {
	wt.project.Role role = (wt.project.Role) itr.next();
	System.out.println("Role-=-=-=" + role);
	if (rolesToCheck.contains(role.toString().toUpper()))) {
		java.util.ArrayList users = (java.util.ArrayList) members.get(role);
	//If there is no user in MyCustomRole role throw exception
		if (users.isEmpty()) {
		throw new Exception("Select user for " + role.getDisplay());
		}
	}
}

The roles must be uppercase and internal value.

That way, you can check as many roles you want.
Florent

Re: Code to check roles in a workflow

Hello Florent,

I am using the internal name for my roles and capitalized them in my first line of code.  However my roles are not all capitalized.

 

I got the following error when checking syntax....

 

Checking Syntax...

Checking Syntax...
D:\ptc\Windchill_11.0\Windchill\temp\WfExpression188770479.java:56: error: illegal start of expression
	if (rolesToCheck.contains(role.toString().toUpper()))) {
	                             ^
1 error
Syntax check complete.

Brian

Re: Code to check roles in a workflow

Sorry, I made a mistake.

 

Try replacing the first line with

java.util.ArrayList<String> rolesToCheck = new java.util.ArrayList<String>();
rolesToCheck.add("MYCUSTOMROLE");
rolesToCheck.add("MYCUSTOMROLE2");