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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Code to check roles in a workflow

BrianToussaint
19-Tanzanite

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

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

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

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

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

 

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
TomU
23-Emerald IV
(To:BrianToussaint)

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?

TomU
23-Emerald IV
(To:froussel_fr)

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...

Florent
15-Moonstone
(To:TomU)

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

 

Florent ROUSSEL
www.4cad.ca
TomU
23-Emerald IV
(To:Florent)

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...

Florent
15-Moonstone
(To:TomU)

@TomU: you mention the wizard. Hence I understand you're working on the promotion request workflow (and wizard).

This piece of code is more likely applicable for change management workflows that contain a real setup participant task.

In your case, there is unfortunately no way to check this upon the wizard completion (unless with some tricky customization). I would rather suggest to use the business rules, but here again, nothing exists OOTB (at least to my knowledge).

 

 

Florent ROUSSEL
www.4cad.ca
smcvr
15-Moonstone
(To:BrianToussaint)

Hi;

How can I assign my own desired participants to a role with code?

Announcements

Top Tags