Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
I am exploring option to complete work item using a Java or Inforengine tasks. Thingworx has out of the box CompleteWorkItems service. It does not take Voting ( Approve / Reject) as an input parameter. So I am wondering if anyone has explored completing the workitem with a Vote option in WIndchill.
Thanks for your help.
Preeti
Hi Preeti,
In Windchill there are few API's but unsupported to complete the task with voting:
wt.workflow.work.WorkflowHelper.service.workComplete
wt.workflow.engine.WfEventHelper.createVotingEvent
Regards,
Bhushan
thanks again Bhushan. I could complete the task using above workComplete method. The workflow moves forward to next activity depending on the routing choices.
I haven't got success yet for creating Voting event.
🙂
Preeti
This works Bhushan.
wt.workflow.engine.WfEventHelper.createVotingEvent
Hi Preeti,
i am also trying to complete Workflow activity and setting vote using below methods. Activity is getting completed. Voting event is set but process is not moving ahead to next activity as per the vote. Can you please add some light to this? do i need to have any additional method call?
WorkflowHelper.service.workComplete(workItem, principalRef, events);
WfEventHelper.createVotingEvent(null, wfActivity, workItem, principalRef, comment, events, isSigned, workItem.isRequired());
The following method works for me when called from an expression robot.
/** * * @param self * @param activityPartialName */ public static void completeAssignedActivity(wt.fc.ObjectReference self , String activityPartialName) { wt.workflow.engine.WfProcess wfProcess = (wt.workflow.engine.WfProcess) self.getObject(); wt.fc.QueryResult queryResult = new wt.fc.QueryResult(); try { queryResult = wfProcess.getContainerNodes(); } catch (wt.util.WTException ex) { System.out.println(ex); } while (queryResult.hasMoreElements()) { java.lang.Object obj = queryResult.nextElement(); if (obj instanceof wt.workflow.work.WfAssignedActivity) { wt.workflow.work.WfAssignedActivity wfAssignedActivity = (wt.workflow.work.WfAssignedActivity) obj; try { if (!wfAssignedActivity.isComplete()) { java.lang.String currentActivityName = wfAssignedActivity.getName(); if (currentActivityName.contains(activityPartialName)) { @SuppressWarnings("UseOfObsoleteCollectionType") java.util.Vector<String> v = new java.util.Vector<>(); v.addElement("<vote_name>"); // route with same name is required to be defined in the task wt.workflow.engine.WfEngineHelper.service.complete(wfAssignedActivity, v); System.out.println("Completed activity name: " + currentActivityName); } } } catch (wt.util.WTException ex) { System.out.println(ex); } } } }
Hi @pjoshi-2
In the workflow, there should be a code that solve the voting and go forward the process to the right routing.
A Vote and a route are two different things. Check example in the Promotion Request process where the code is used to chose right route.
PetrH
Thanks @BhushanNehe & @PreetiGupta
This post really helped me with the workitems task completion.
But I am unable to save the comments that I am passing from Thingworx to the windchill service.
The task completes with the appropriate Routing choice selected but on visiting the completed task from Windchill, cannot see the comments, so basically I am looking for a way to persist the comments when completing task
As of now I have used the below approach that is clearly not working for me (setting the comments to the activity process). Not sure is this is the right way. Any clue/suggestions will be appreciated. Please help
ProcessData data = activity.getContext();
data.setTaskComments("<Comments>"); // setting the updated comments
activity.setContext(data);
Thanks!
DId you ever find a way to do this? I'm also trying to set the task comments in code and can't get it to work
The following method works great for me.
/**
*
* @param wItem
* @param comment
*/
public static boolean setComment(WorkItem wItem, String comment) {
boolean failed = false;
ProcessData wItemContext = wItem.getContext();
Transaction trx = null;
try {
trx = new Transaction();
trx.start();
if (wItemContext == null) {
WfAssignedActivity wfAssignedActivity = (WfAssignedActivity) wItem.getSource().getObject();
ProcessData activityContext = wfAssignedActivity.getContext();
wItemContext = activityContext.copy();
}
if (comment == null) {
comment = "";
}
wItemContext.setTaskComments(comment);
wItem.setContext(wItemContext);
PersistenceHelper.manager.save(wItem);
trx.commit();
trx = null;
} catch (Exception e) {
System.out.println("Exception\n" + e);
} finally {
if (trx != null) {
trx.rollback();
failed = true;
System.out.println("Failed to update the comment");
}
}
return failed;
}
HI James,
Your code works for setting the comments which you can see within the Windchill PDMLink and they will propogate when completing the task through the Windchill interface. But if you complete the task from code using a line like
WorkflowHelper.service.workComplete(workitem, wt.org.WTPrincipalReference.newWTPrincipalReference(wt.session.SessionHelper.getPrincipal()), events);
the comments are not persisted and basically disappear. This is what sbegum-2 is also saying.
Hello All,
You need to do something like below
WorkItem workItem = ...;
Vector events = new Vector();
// add routing if applicable
// events.addElement("Reject");
WorkflowHelper.service.workComplete(workItem, workItem.getOwnership().getOwner(), events);
// -- start create Voting Event
WTPrincipalReference principalRef= WTPrincipalReference.newWTPrincipalReference(SessionHelper.manager.getPrincipal());
WfActivity myActivity = (WfActivity) workItem.getSource().getObject();
WfAssignedActivityTemplate wfAssignedActivityTemplate=(WfAssignedActivityTemplate) myActivity.getTemplateReference().getObject();
boolean isSigned=wfAssignedActivityTemplate.isSigningRequired();
String comment = "my_comment";
//unsupported API
WfEventHelper.createVotingEvent(null, myActivity, workItem, principalRef, comment, events, isSigned, workItem.isRequired());
// -- end create Voting Event
Also check this Article : https://www.ptc.com/en/support/article/cs234435
Thanks Srinivas. Worked like a charm. Great work.
Your code works perfectly when you execute using Admin login.. but it wont work if you try to execute with non-admin login.. For non admins.. we have to use below code.
WorkItem workItem = ...;
Vector events = new Vector();
// add routing if applicable
// events.addElement("Reject");
WorkflowHelper.service.workComplete(workItem, workItem.getOwnership().getOwner(), events);
// -- start create Voting Event
WTPrincipalReference principalRef= WTPrincipalReference.newWTPrincipalReference(SessionHelper.manager.getPrincipal());
WfActivity myActivity = (WfActivity) workItem.getSource().getObject();
WfAssignedActivityTemplate wfAssignedActivityTemplate=(WfAssignedActivityTemplate) myActivity.getTemplateReference().getObject();
boolean isSigned=wfAssignedActivityTemplate.isSigningRequired();
String comment = "my_comment";
//unsupported API
boolean accessEnforced = false;
try {
accessEnforced = SessionServerHelper.manager.setAccessEnforced(false);
WfEventHelper.createVotingEvent(null, myActivity, workItem, principalRef, comment, events, isSigned, workItem.isRequired());
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getLocalizedMessage(), e);
}
throw e;
} finally {
SessionServerHelper.manager.setAccessEnforced(accessEnforced);
}
// -- end create Voting Event
Thanks
Shyam.