Skip to main content
1-Visitor
November 9, 2011
Solved

Expression to Determine the Number of Participants in a Role

  • November 9, 2011
  • 4 replies
  • 4164 views

Does anyone know an expression which picks up the number of participants in a particular role?

For example, using a conditional node to determine if enough principals are in Promotion Approvers role?

Regards,

Toby

Best answer by MatthewKnight

You have to use full package names for anything outside of java.lang.

result = "ERROR";

try {

if (primaryBusinessObject instanceof wt.team.TeamManaged) {

wt.team.Team team = wt.team.TeamHelper.service.getTeam((wt.team.TeamManaged) primaryBusinessObject);

Role role = Role.toRole("APPROVER");

int count = 0;

Enumeration<?> principals = team.getPrincipalTarget(role);

while (principals.hasMoreElements()) {

wt.org.WTPrincipalReference next = (wt.org.WTPrincipalReference) principals.nextElement();

if (next != null) {

count++;

}

}

if (count > 1) {

result = "CONTINUE";

}

}

} catch (Exception e) {

e.printStackTrace();

}

4 replies

1-Visitor
November 9, 2011

There are several ways you could do it. Here's one approach that does not expand group participants

Team team = .......

Role role = Role.toRole("APPROVER");

int count = 0;

Enumeration<?> principals = team.getPrincipalTarget(role);

while(principals.hasMoreElements()) {

WTPrincipalReference next = (WTPrincipalReference)principals.nextElement();

if(next != null) {

count++;

} else {

System.out.println("null");

}

}

System.out.println("count: " + count);

1-Visitor
November 9, 2011

Thanks.

For the team what should I be using?

The context team, team template or team instance? And if it is the latter then how would I get an expression for it?

Toby

1-Visitor
November 9, 2011

I would assume you want to check the primaryBusinessObject's team. That's more than likely going to be wt.team.Team.

if(primaryBusinessObject instanceof TeamManaged) {

Team team = TeamHelper.service.getTeam((TeamManaged)primaryBusinessObject);

}

FYI, from my perspective:

Team and TeamTemplate are designed pretty much identical to each other.

ContainerTeam(s) are much different and several of the WTRoleHolder2 APIs do not work for ContainerTeam. ContainerTeam uses WTGroups to represent its Role's.

1-Visitor
November 9, 2011

I tried it all and it came up with an error..

When I check the syntax in the conditional it says:

Checking Syntax...

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression_htorgadmin-toby.java:16: '{' expected

public class WfExpression_htorgadmin-toby {

^

1 error

Syntax check complete.

The thing is this comes up even if the conditional block is empty... What am I missing?..

The code I am trying to use is:

result = ERROR;

if(primaryBusinessObject instanceof TeamManaged) {

Team team = TeamHelper.service.getTeam((TeamManaged)primaryBusinessObject);

}

Role role = Role.toRole("APPROVER");

int count = 0;

Enumeration<?> principals = team.getPrincipalTarget(role);

while(principals.hasMoreElements()) {

WTPrincipalReference next = (WTPrincipalReference)principals.nextElement();

if(next != null) {

count++;

} else {

System.out.println("null");

}

}

if(count<2) {

result = ERROR;

} else {

result = CONTINUE;

}

1-Visitor
November 9, 2011

Correction it is now saying:

symbol : class TeamManaged

location: class wt.workflow.expr.WfExpression8879749

Team team = TeamHelper.service.getTeam((TeamManaged)primaryBusinessObject);

^

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression8879749.java:34: package TeamHelper does not exist

Team team = TeamHelper.service.getTeam((TeamManaged)primaryBusinessObject);

^

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression8879749.java:37: cannot find symbol

symbol : class Role

location: class wt.workflow.expr.WfExpression8879749

Role role = Role.toRole("APPROVER");

^

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression8879749.java:37: cannot find symbol

symbol : variable Role

location: class wt.workflow.expr.WfExpression8879749

Role role = Role.toRole("APPROVER");

^

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression8879749.java:40: cannot find symbol

symbol : class Enumeration

location: class wt.workflow.expr.WfExpression8879749

Enumeration<?> principals = team.getPrincipalTarget(role);

^

/apps/ptc/Windchill_9.1/Windchill/tmp/WfExpression8879749.java:40: cannot find symbol

symbol : variable team

location: class wt.workflow.expr.WfExpression8879749

Enumeration<?> principalsSyntax check complete.

Even though it had previously said there was no problem other than the one mentioned above.

1-Visitor
November 9, 2011

You have to use full package names for anything outside of java.lang.

result = "ERROR";

try {

if (primaryBusinessObject instanceof wt.team.TeamManaged) {

wt.team.Team team = wt.team.TeamHelper.service.getTeam((wt.team.TeamManaged) primaryBusinessObject);

Role role = Role.toRole("APPROVER");

int count = 0;

Enumeration<?> principals = team.getPrincipalTarget(role);

while (principals.hasMoreElements()) {

wt.org.WTPrincipalReference next = (wt.org.WTPrincipalReference) principals.nextElement();

if (next != null) {

count++;

}

}

if (count > 1) {

result = "CONTINUE";

}

}

} catch (Exception e) {

e.printStackTrace();

}

1-Visitor
November 9, 2011

Works like a charm. Thank you very much!

1-Visitor
November 9, 2011

One last quick thing, do you know how I would apply the same code to the change admin III role? I've tried replacing "APPROVER" with "CHANGE ADMIN III" and "CHANGE_ADMIN_III". Neither worked..

1-Visitor
November 9, 2011

"CHANGE ADMINISTRATOR III"

Roles are defined in RoleRB.rbInfo. It should be under <wchome>/src/wt/project