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

We are happy to announce the new Windchill Customization board! Learn more.

About the role assignment code in Expression

smcvr
14-Alexandrite

About the role assignment code in Expression

 

wt.org.DirectoryContextProvider contextProvider = wt.inf.container.WTContainerHelper.service.getExchangeContainer().getContextProvider();
      wt.org.WTGroup wgroup = wt.org.OrganizationServicesHelper.manager.getGroup("YONETICI", contextProvider);
	 wt.query.QuerySpec qs = null;
      qs = new wt.query.QuerySpec(wt.org.WTGroup.class);
      wt.query.SearchCondition sc = new wt.query.SearchCondition(wt.org.WTGroup.class, wt.org.WTGroup.NAME, wt.query.SearchCondition.EQUAL, "YONETICI");
      qs.appendWhere(sc, new int[] { 0 });

  for(wt.fc.QueryResult queryResult = wt.fc.PersistenceHelper.manager.find(qs); queryResult != null && queryResult.hasMoreElements();)
     wgroup = (wt.org.WTGroup)queryResult.nextElement();

     java.util.ArrayList<WTUser> members = new java.util.ArrayList<WTUser>();
     java.util.Enumeration<wt.org.WTPrincipal> member = wgroup.members();

  while (member.hasMoreElements()) {
     wt.org.WTPrincipal principal = (wt.org.WTPrincipal) member.nextElement();
  if (principal instanceof WTUser){
     members.add((WTUser) principal);}
   }

     wt.team.Team team = new wt.team.Team();
	 team.setName("My Team");
	 wt.org.WTPrincipal wtprincial = members.get(0);
        wt.project.Role role = wt.project.Role.toRole("Submitter");

      if (role == null) { 
      team.addPrincipal(role, wtprincial);  
      team.addPrincipal(wt.project.Role.toRole("Submitter"), wtprincial); 
      wt.fc.PersistenceHelper.manager.save(team);
      team = (wt.team.Team)PersistenceHelper.manager.refresh(team);  
     }

 

 

The code above does not give an error, but when I run the flow, it does not return a user to me. There is a registered user in the "YONETICI" group, I want to assign that user to the Submitter role and send a task, but the task does not go to the user.

1 ACCEPTED SOLUTION

Accepted Solutions
HelesicPetr
21-Topaz II
(To:smcvr)

@smcvr 

Additional information.

 

if you use a Variable to assign a participant do not use wt.project.Role. It can not work this way, Role as a object do not contains members it is just Enumeration. Use WTParticipant type or WTUser. 

 

Also the variable can not be used as you use it with wt.team.TeamHelper.service.addRolePrincipalMap. 

The assignee is a role from a team connected with primaryBusinessObject not with Variable of the task. 

 

PetrH

View solution in original post

15 REPLIES 15
HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

Show screen shot of workflow that others can imagine where the code and task are run

 

Check BacgroudnMethodServer.log if there is any error from WF

 

Last point the method Role.toRole("NAMEOFROLE") has to use internal name of the ROLE so Submitter I guess is SUBMITTER

 

PetrH

avillanueva
22-Sapphire I
(To:smcvr)

Looks like you are creating a new team, not modifying the existing team of the workflow or PBO.  There is no tying the team you created to the workflow. Did I miss something?

smcvr
14-Alexandrite
(To:avillanueva)

how can i add to existing team?

 

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

Get the team from the object where the workflow is running on by following code

 

wt.team.Team team = wt.team.TeamHelper.service.getTeam(ci); // CI is a object Change Issue, WTPart, ChangeRequest, PromotionRequest what ever. 

 

 

@avillanueva good point with the new Team I didn't check the code so carefully 😄

 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

     wt.org.DirectoryContextProvider contextProvider = wt.inf.container.WTContainerHelper.service.getExchangeContainer().getContextProvider();
     wt.org.WTGroup wgroup = wt.org.OrganizationServicesHelper.manager.getGroup("YONETICI", contextProvider);
	 wt.query.QuerySpec qs = new wt.query.QuerySpec(wt.org.WTGroup.class);
     wt.query.SearchCondition sc = new wt.query.SearchCondition(wt.org.WTGroup.class, wt.org.WTGroup.NAME, wt.query.SearchCondition.EQUAL, "YONETICI");
     qs.appendWhere(sc, new int[] { 0 });

  for(wt.fc.QueryResult queryResult = wt.fc.PersistenceHelper.manager.find(qs); queryResult != null && queryResult.hasMoreElements();)
     wgroup = (wt.org.WTGroup)queryResult.nextElement();
     
     java.util.ArrayList<WTUser> members = new java.util.ArrayList<WTUser>();
     java.util.Enumeration<wt.org.WTPrincipal> member = wgroup.members();

  while (member.hasMoreElements()) {
     wt.org.WTPrincipal principal = (wt.org.WTPrincipal) member.nextElement();
  if (principal instanceof WTUser){
     members.add((WTUser) principal);}
   }

     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 team = wt.team.TeamHelper.service.getTeam(pro);
	 wt.org.WTPrincipal wtprincial = members.get(0);
        wt.project.Role role = wt.project.Role.toRole("Submitter");

      if (role == null) { 
      team.addPrincipal(role, wtprincial);  
      team.addPrincipal(wt.project.Role.toRole("Submitter"), wtprincial); 
      wt.fc.PersistenceHelper.manager.save(team);
      team = (wt.team.Team)PersistenceHelper.manager.refresh(team);  
     }

 

I edited the code, I don't get an error in the expression, but when I run the workflow, it stops in the expression where the code is written and I get the following error

 

wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
wt.workflow.engine.FailedExpressionException: wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: wt.util.WTException: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
Nested exception is: java.lang.ClassCastException: wt.workflow.engine.WfProcess cannot be cast to wt.workflow.work.WfAssignedActivity
at wt.workflow.expr.WfExpression111135098.executemethod_1(WfExpression111135098.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at wt.workflow.definer.WfExpression.invokeByReflection(WfExpression.java:1465)
at wt.workflow.definer.WfExpression.executeTransition(WfExpression.java:1408)
at wt.workflow.definer.WfExpression.execute(WfExpression.java:477)
at wt.workflow.robots.WfExpressionRobot.run(WfExpressionRobot.java:162)
at wt.workflow.engine.StandardWfEngineService.runRobot(StandardWfEngineService.java:1187)
at sun.reflect.GeneratedMethodAccessor942.invoke(Unk

 

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

The error tells you that object WfProcess can not be used in variable type WfAssignedActivity

 

So you need to work correctly with WFProcess, 

 

if the code is run by robot directly in workflow then self.getObject(); is the object type WfProcess 

if the code is run from workflow activity then self.getObject(); is the object type WfAssignedActivity and you need to get the parent object that is WfProcess 

 

PS: you need to get the team from primaryBussinessObject not from workflow.

PS2: for better imagination 😄 Show screen shot of workflow that others can imagine where the code and task are run

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Workflow:

akış.png

 

Open Process Manager:

smcvr_0-1674460296916.png

 

Code:

wt.org.DirectoryContextProvider contextProvider = wt.inf.container.WTContainerHelper.service.getExchangeContainer().getContextProvider();
     wt.org.WTGroup wgroup = wt.org.OrganizationServicesHelper.manager.getGroup(katilimci, contextProvider);
	 wt.query.QuerySpec qs = new wt.query.QuerySpec(wt.org.WTGroup.class);
     wt.query.SearchCondition sc = new wt.query.SearchCondition(wt.org.WTGroup.class, wt.org.WTGroup.NAME, wt.query.SearchCondition.EQUAL, katilimci);
     qs.appendWhere(sc, new int[] { 0 });

  for(wt.fc.QueryResult queryResult = wt.fc.PersistenceHelper.manager.find(qs); queryResult != null && queryResult.hasMoreElements();)
     wgroup = (wt.org.WTGroup)queryResult.nextElement();
     
     java.util.ArrayList<WTUser> members = new java.util.ArrayList<WTUser>();
     java.util.Enumeration<wt.org.WTPrincipal> member = wgroup.members();

  while (member.hasMoreElements()) {
     wt.org.WTPrincipal principal = (wt.org.WTPrincipal) member.nextElement();
  if (principal instanceof WTUser){
     members.add((WTUser) principal);}
   }
    wt.change2.ChangeIssue ci =( wt.change2.ChangeIssue)primaryBusinessObject;
    wt.team.Team team = wt.team.TeamHelper.service.getTeam(ci);
	 wt.org.WTPrincipal wtprincial = members.get(0);
        wt.project.Role role = wt.project.Role.toRole("Submitter");

      if (role == null) { 
      team.addPrincipal(role, wtprincial);  
      team.addPrincipal(wt.project.Role.toRole("Submitter"), wtprincial); 
      wt.fc.PersistenceHelper.manager.save(team);
      team = (wt.team.Team)PersistenceHelper.manager.refresh(team);  
     }

 

I got the variable "kilimci" as a string from another expression. I changed the code as you said, it does not show an error in the code. There is no error in open process manager, but when it comes to the activity, it shows as completed and does not send me a task.

  

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

You should use refresh to the saved team so

team  = (wt.team.Team)wt.fc.PersistenceHelper.manager.save(team);
team = (wt.team.Team)PersistenceHelper.manager.refresh(team);  

 

I would check an task action if the role submitter is used in the task.

I guess there can be different role.

 

Also check the Member Participant list in a detail page of the primary object at a process tab

HelesicPetr_0-1674462294986.png

 

 

PetrH

Hi @smcvr 

I checked my examples of the assigning the participants to roles so, try it

 

wt.org.WTPrincipal principal = ((wt.org.WTPrincipalReference)principals.nextElement()).getPrincipal();	
wt.team.TeamHelper.service.addRolePrincipalMap(wt.project.Role.toRole("ASSIGNEE"), principal, teamActivity);  

 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

I did not understand what you said. there is no error in my code but it doesn't work. Thanks for be interested.

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

I asked you to check if your role has been updated by your participant. 

I'm trying to go step by step to show you why it does not work.

 

Additionally I said that you should use different function to set the participant to a role

So try it.

wt.team.TeamHelper.service.addRolePrincipalMap(wt.project.Role.toRole("ASSIGNEE"), principal, teamActivity);

 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

smcvr_0-1674565040081.png

wt.org.DirectoryContextProvider contextProvider = wt.inf.container.WTContainerHelper.service.getExchangeContainer().getContextProvider();
     wt.org.WTGroup wgroup = wt.org.OrganizationServicesHelper.manager.getGroup(katilimci, contextProvider);
	 wt.query.QuerySpec qs = new wt.query.QuerySpec(wt.org.WTGroup.class);
     wt.query.SearchCondition sc = new wt.query.SearchCondition(wt.org.WTGroup.class, wt.org.WTGroup.NAME, wt.query.SearchCondition.EQUAL, katilimci);
     qs.appendWhere(sc, new int[] { 0 });

  for(wt.fc.QueryResult queryResult = wt.fc.PersistenceHelper.manager.find(qs); queryResult != null && queryResult.hasMoreElements();)
     wgroup = (wt.org.WTGroup)queryResult.nextElement();
     
     java.util.ArrayList<WTUser> members = new java.util.ArrayList<WTUser>();
     java.util.Enumeration<wt.org.WTPrincipal> member = wgroup.members();
  while (member.hasMoreElements()) {
     wt.org.WTPrincipal principal = (wt.org.WTPrincipal) member.nextElement();
	 		
  if (principal instanceof WTUser){
     members.add((WTUser) principal);}
   }
    wt.change2.ChangeIssue ci =( wt.change2.ChangeIssue)primaryBusinessObject;
    team = wt.team.TeamHelper.service.getTeam(ci);
	  wtprincial = members.get(0);
      role = wt.project.Role.toRole("ASSIGNEE");
    

The above part works. I pull the user from the group and assign it to the wtprincial variable but I can't pass it to the role variable

  if (role == null) {   
	 
      team.addPrincipal(role, wtprincial);  
	  wt.team.TeamHelper.service.addRolePrincipalMap(wt.project.Role.toRole("ASSIGNEE"), wtprincial, team);
      wt.fc.PersistenceHelper.manager.save(team);
     }
	 

 

HelesicPetr
21-Topaz II
(To:smcvr)

Hi @smcvr 

Do not use a Variable use a Role 

HelesicPetr_0-1674566080790.png

 

What does condition if (role == null) means? if role exists it is never null. 

 

PetrH

HelesicPetr
21-Topaz II
(To:smcvr)

@smcvr 

Additional information.

 

if you use a Variable to assign a participant do not use wt.project.Role. It can not work this way, Role as a object do not contains members it is just Enumeration. Use WTParticipant type or WTUser. 

 

Also the variable can not be used as you use it with wt.team.TeamHelper.service.addRolePrincipalMap. 

The assignee is a role from a team connected with primaryBusinessObject not with Variable of the task. 

 

PetrH

smcvr
14-Alexandrite
(To:HelesicPetr)

Hi @HelesicPetr ;

Thank you for the help. Workflow is running.

Top Tags