Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
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.
Solved! Go to Solution.
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
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
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.
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
Hello @BrianToussaint
I got Idea.
What transition do you use?
I guess that if you chose own custom transition that user should select, that moment if a user click on a complete button the code if transition is not run if there are more not completed tasks with other users.
So I would advice to use Complete Task transition in a wf task.
Best Regards
PetrH
.
Hello @HelesicPetr
I do have it under a custom transition. This is because if a user decides to "cancel" the workflow, they do not have to assign users to the workflow. If it is under the Complete Task, then it will require users to be filled in even though there will be no routing to anyone. Users will complain about adding someone that isn't necessary.
I will look into that code as it would possibly lessen the number of characters that I have to use. However, when I had just one role in to check for, it ignored it and there was nothing in the logs. I wish that I just had an easy workflow, but I have 4 different paths based off of how attributes are chosen and within the 4 paths, there are 3 different role selections.
Thank you,
Brian
Understand. Sometimes writing custom code in wf is tricky 😄
Writing a code in onw class is better way how to debug some troubles with the coding in WF.
BR
PetrH
Thank you. This finally helped me get in the right direction but with changes for multi-role.
Can I offer a suggestion?
I’ve written code that checks to make sure roles set using Setup Participants have at least one user.
You might consider adding code to check that too.
In your code you are keying off the Principal's name rather than the Principal.
Rather than this, which calls two unnecessary getName() methods
if (assignedUser.getName().equals(activeUser.getName()))
Why not this which simply checks if the principals are equal. Which is the bottom line.
if (assignedUser.equals(activeUser))
You don't care about the names being equal. You care about the objects being equal.
If you key off name rather than the objects a group with the same name as the current user would throw the exception. Oops.
Think bombproof.
Also, what about if the current user is in a Group or a sub group that's in the role Approver. Shouldn't that also throw the exception. Doesn't the code need to be checked for too?
Hello @d_graham ,
You're right.
Thanks for suggestion.
Also there could be several other checks to avoid errors.
I used this code for specific case where is not allowed to assign different type of participant then user.
Best Regards.
PetrH
PetrH,
Are the additional checks you mentioned pertaining to Setup Participant checks or are these not related to that.
I’ve written a ton of code regarding workflow checks that check and if possible automatically fix errors but as for Setup Participant I’ve written code to check if a role has a WTUser in the role (even if the user is in a sub-group as that is good enough) and, as in the case of this thread, to prevent a user from being the checker/Approver of their own work.
Just curious, what other Setup Participant checks are you doing?
David
Hi @d_graham
Check the user if is member in a specific group which is not part of a wf role list.
I know this can be restricted by wf but in specific cases it wasn't ideal.
Another check can be if the selected user has correct ACL rights to the object for example. WTDocument
Best Regards
PetrH