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

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());
			}
		}
	}
10 REPLIES 10

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");

Re: Code to check roles in a workflow

Hello Brian,

 

I am looking for more concise code to stay within the 4000 Character Limit.

Did you find a solution to your code?  I see the same error when attempted the code in the Community Message as My Internal Names also are not all Capitalized.

 

I have code that will check a few roles but need to check 10 downstream roles which your code appears it would handle easily

 

Brian

 

Re: Code to check roles in a workflow

To all,

in the first lines, you can either:

  • put the key of the roles you want to check in uppercase (no mater how the key is) or
  • put the key in the exact manner (as in RoleRB.rbInfo) and remove the toUpperCase()

 

java.util.ArrayList<String> rolesToCheck = new java.util.ArrayList<String>();
rolesToCheck.add("<KEY OF THE ROLE1>");
rolesToCheck.add("<KEY OF THE ROLE2>");

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().toUpperCase())) {
		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());
		}
	}
}

@BrianSullivan If you have length issues, you can remove the comments and System.out.print lines.

 

 

Florent ROUSSEL
www.4cad.ca

Re: Code to check roles in a workflow

I'm getting the same error.  If I remove the .toString().toUpper() it passes the syntax check but still fails to run:

 

wt.workflow.engine.FailedExpressionException: wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity

 

Any ideas?

Re: Code to check roles in a workflow

Does this code have to go inside an activity?  I was trying to use it at the beginning of the workflow in an expression block but I think it might be failing due to no activities being assigned yet.  I'd like to make sure all the roles are populated before the activities are assigned...

Highlighted

Re: Code to check roles in a workflow

@TomU Yes, I believe @BrianToussaint had this in the "Complete task" transition of a "Setup Participant" task.

 

Florent ROUSSEL
www.4cad.ca

Re: Code to check roles in a workflow

Bummer.  I don't use any of those (and don't want to).  The users are supposed to assign users to the roles during creation (from the wizard.)  I'd love to find a way to prevent the wizard from finishing if the roles are empty...

Announcements
LiveWorx Call For Papers Happening Now!