Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
I have code to make sure that Roles are filled in before Completing a task. What I am trying to do is to check multiple roles as being required or not based on workflow variables. Does anyone have the code for this?
Example (The first two variables are an if this or this):
If varible1=outcome1{
Then Role1 must be filled in
}
if variable2=outcome2{
Then Role1 must be filled in
}
if variable3=outcome3{
Then Role2 must be filled in
}
Solved! Go to Solution.
I accidentally marked this as the solution when it wasn't. It was that the internet was too slow and I double hit the reply but it decided it liked the solution button. 🤣 When I did my testing initially, I made statement one false, so when I get into testing multiple scenarios, I found that it didn't work. Below though is code that works now.
You actually have to use multiple variables for the Iterator and use a different one at each IF. This was the easiest and one that worked the quickest from Tech Support.
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();
java.util.Iterator itr1 = keys.iterator();
java.util.Iterator itr2 = keys.iterator();
java.util.Iterator itr3 = keys.iterator();
if(<attribute1>.equalsIgnoreCase("<value1>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("<role1>")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if(<attribute2>.equalsIgnoreCase("<value2>")){
while (itr1.hasNext()) {
wt.project.Role role = (wt.project.Role) itr1.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value3>")){
while (itr2.hasNext()) {
wt.project.Role role = (wt.project.Role) itr2.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value4>")){
while (itr3.hasNext()) {
wt.project.Role role = (wt.project.Role) itr3.next();
if (role.toString().equalsIgnoreCase("role2")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role2 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
I've done several workflow templates with conditionals that includes / skips parallel tasks, one for each Role. Start with the user at the beginning setting Boolean variables; can use more sophisticated code if needed.
Based on eacah Boolean variable, each parallel branch is either included or skipped, resulting in those Roles acting. Need AND after all Roles.
@MikeLockwood Sorry for my ignorance with programming but here is my code with the information stripped. If I have just one "if" statement in there, it works.
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();
if(CT_VAR1.equalsIgnoreCase("value1")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("ROLE1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in ROLE1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
||
if(CT_VAR2.equalsIgnoreCase("value2")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("ROLE1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in ROLE1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
&&
if(CT_VAR3.equalsIgnoreCase("value3")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("ROLEA")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in ROLEA role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
One way with minimal code is to use a separate activity for each Role as shown here. Each conditional has a bit of code for the workflow variable representing each Role. Can provide more detail if needed.
That actually helps for a different piece of the puzzle that I was going to be getting to. The problem I am having is that I have a step where a user defines the participants for Roles 1-3. I know how to force it to fail if any of the Roles are blank. What I am trying to do is only error out if ones are blank based on certain attributes. So in the example below, either all three would need to be filled out or just one would based on choices when creating the workflow.
There can be code in each of the conditionals, including a path to loop back to the creator or other user who is responsible for determining which Roles to include.
It just simplifies things in terms of code to separate out like shown - at the expense of a more complex looking WF template.
Can also have a single conditional in front of the parallel paths that determines Roles based on some logic if a user doesn't select (instead of having all Roles in a single activity).
@MikeLockwood I'm using that in a different area of my workflow to send out emails to certain users based on some attributes. Thanks for the tip.
I however figured out the code to do what I was wanting without creating additional tasks. I am including it below.
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();
if(<attribute1>.equalsIgnoreCase("<value1>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("<role1>")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if(<attribute2>.equalsIgnoreCase("<value2>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value3>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value4>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("role2")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role2 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
I accidentally marked this as the solution when it wasn't. It was that the internet was too slow and I double hit the reply but it decided it liked the solution button. 🤣 When I did my testing initially, I made statement one false, so when I get into testing multiple scenarios, I found that it didn't work. Below though is code that works now.
You actually have to use multiple variables for the Iterator and use a different one at each IF. This was the easiest and one that worked the quickest from Tech Support.
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();
java.util.Iterator itr1 = keys.iterator();
java.util.Iterator itr2 = keys.iterator();
java.util.Iterator itr3 = keys.iterator();
if(<attribute1>.equalsIgnoreCase("<value1>")){
while (itr.hasNext()) {
wt.project.Role role = (wt.project.Role) itr.next();
if (role.toString().equalsIgnoreCase("<role1>")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if(<attribute2>.equalsIgnoreCase("<value2>")){
while (itr1.hasNext()) {
wt.project.Role role = (wt.project.Role) itr1.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value3>")){
while (itr2.hasNext()) {
wt.project.Role role = (wt.project.Role) itr2.next();
if (role.toString().equalsIgnoreCase("role1")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role1 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}
if (<attribute3>.equalsIgnoreCase("<value4>")){
while (itr3.hasNext()) {
wt.project.Role role = (wt.project.Role) itr3.next();
if (role.toString().equalsIgnoreCase("role2")) {
java.util.ArrayList users = (java.util.ArrayList) members.get(role);
//If there is no user in role2 role throw exception
if (users.isEmpty()) {
throw new Exception("Select user for " + role.getDisplay());
}
}
}
}