Skip to main content
19-Tanzanite
December 14, 2021
Solved

Stop user from adding self to Setup Participant role

Does anyone have simple code that can go in the Transitions portion that will stop the activity to be completed if a user selects themselves as an approver for a role?  I have a simple workflow but I do not want users choosing themselves as the checker of the work.  Currently I am on Windchill 11.1.  A quick search didn't turn up anything on this subject, but I thought that there have been previous posts.

HelesicPetr
22-Sapphire II
December 17, 2021

Hello,

 

This code will work >D

Change name of role as you need

this example use "APPROVER" as checked role.

 


wt.workflow.engine.WfProcess wfprocess = (((wt.workflow.work.WfAssignedActivity) self.getObject()).getParentProcess());
java.util.Enumeration roles = wfprocess.getRoles();
while (roles.hasMoreElements())
{
wt.project.Role role = (wt.project.Role) roles.nextElement();
java.util.Enumeration participants = wfprocess.getPrincipals(role);

// kontrola zobrazovaného názvu role
if (role.getDisplay().equalsIgnoreCase("APPROVER"))
{
wt.org.WTPrincipal activeUser = wt.session.SessionHelper.getPrincipal();
if (!participants.hasMoreElements())
{
String msg = "Approver MUST be assigned";
java.lang.Exception exp = new java.lang.Exception(msg);
throw exp;
}
while (participants.hasMoreElements())
{
wt.org.WTPrincipalReference principal = (wt.org.WTPrincipalReference)participants.nextElement();
wt.org.WTPrincipal assignedUser = principal.getPrincipal();
if (assignedUser.getName().equals(activeUser.getName()))
{
String msg = "Can not set yorself as Approver.";
java.lang.Exception exp = new java.lang.Exception(msg);
throw exp;
}
}
}
}

 PetrH

11 replies

HelesicPetr
22-Sapphire II
December 17, 2021

Hello,

 

This code will work >D

Change name of role as you need

this example use "APPROVER" as checked role.

 


wt.workflow.engine.WfProcess wfprocess = (((wt.workflow.work.WfAssignedActivity) self.getObject()).getParentProcess());
java.util.Enumeration roles = wfprocess.getRoles();
while (roles.hasMoreElements())
{
wt.project.Role role = (wt.project.Role) roles.nextElement();
java.util.Enumeration participants = wfprocess.getPrincipals(role);

// kontrola zobrazovaného názvu role
if (role.getDisplay().equalsIgnoreCase("APPROVER"))
{
wt.org.WTPrincipal activeUser = wt.session.SessionHelper.getPrincipal();
if (!participants.hasMoreElements())
{
String msg = "Approver MUST be assigned";
java.lang.Exception exp = new java.lang.Exception(msg);
throw exp;
}
while (participants.hasMoreElements())
{
wt.org.WTPrincipalReference principal = (wt.org.WTPrincipalReference)participants.nextElement();
wt.org.WTPrincipal assignedUser = principal.getPrincipal();
if (assignedUser.getName().equals(activeUser.getName()))
{
String msg = "Can not set yorself as Approver.";
java.lang.Exception exp = new java.lang.Exception(msg);
throw exp;
}
}
}
}

 PetrH

Translate
19-Tanzanite
January 14, 2022

Hello @HelesicPetr 

I replaced my role in your code and then placed it in my transition and it doesn't catch anything. I don't know if it is because of having multiple roles to check against or not. Any suggestions would be helpful. I am going to try and see if I can get it in my current code that works with multi-role object catching.

Translate
HelesicPetr
22-Sapphire II
January 17, 2022

Hello @BrianToussaint ,

I would check a log if there is an error from a workflow.

 

If there are more roles it's ok.

the while loop checks all the roles in the workflow.

java.util.Enumeration roles = wfprocess.getRoles();
while (roles.hasMoreElements())
{----------

 the if condition needs to be updated if you need to check more roles.

if (role.getDisplay().equalsIgnoreCase("APPROVER")|| role.getDisplay().equalsIgnoreCase("REVIEWER"))
{------

 

Best Regards.

PetrH

Translate