Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
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());
}
}
}
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:
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.
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?
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...
@TomU Yes, I believe @BrianToussaint had this in the "Complete task" transition of a "Setup Participant" task.
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...
@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).
Hi;
How can I assign my own desired participants to a role with code?
