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

Code to check multiple participants based off of multiple workflow variables

BrianToussaint
19-Tanzanite

Code to check multiple participants based off of multiple workflow variables

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

}

6 REPLIES 6

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.

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

mlockwood_0-1626302910633.png

 

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.

 

2021-07-15_6-48-15.jpg

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

@mlockwood 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());
			}
		}
	}
}

 

Announcements