Restart a workflow context without affecting other workflows running on the record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-21-2013 02:53 AM
Hello there,
I have a requirement to selectively restart a workflow context associated to an Incident record. This can be done by either restarting the workflow context or even by deleting & creating the workflow again.
I can see from wiki and forums about methods such as restartWorkflow(), deleteWorkflow() which acts on all workflows associated to that incident record. However, there are few other workflows (unrelated to the workflow version being restarted) on this record, which should not be affected by this restart.
Can anyone please guide me on the ways to restart a workflow context without affecting other workflows?
Regards,
Praveen
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2013 09:06 AM
I just did this for one of my peers.
Basically, I took the OOB Workflow().restart() function and made it check for a specific context name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2013 03:28 AM
Thanks Valor for letting me know about restart() method. Unfortunately, as the requirement has been put on hold, I will not be able to test this now.
I was only aware of restartWorkflow() which restarted all workflows associated to the record, based on the API documentation in https://wiki.servicenow.com/index.php?title=Workflow_Script. I will let you know whenever I test this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2013 08:07 AM
I have an identical requirement and I was also only aware of restartWorkflow(). Could you possibly elaborate on your solution? Did you modify restartWorkflow() or is there a restart() function available out of the box that will act only on an individual workflow context?
Thanks
Jon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2013 03:52 PM
I guess it would be helpful if I posted the actual code.. 😉
I haven't tested this specifically, but my peer didn't complain about it not working!
restartSingleWorkflow(current, "WF Name to Restart");
function restartSingleWorkflow(current, wfName) {
if (!(current instanceof GlideRecord) || JSUtil.nil(wfName)){
return;
}
var maintainStateFlag = false;
var wf = new Workflow();
var gr = wf.getContexts(current);
while (gr.next()) {
if (gr.workflow_version.name == wfName){
gs.print("Restarting workflow " + wfName + " for " + current.getDisplayValue());
// delete the executing activities and start over at the begin
// activity
wf._deleteExecutingActivities(gr.sys_id + '');
wf._updateHistoryActivities(gr.sys_id + '');
wf._createBeginActivity(gr);
// tell workflow that we want to maintain our approval and task
// states
if (gr.scratchpad.maintainStateFlag != maintainStateFlag) {
gr.scratchpad.maintainStateFlag = maintainStateFlag;
}
gr.state = 'executing';
gr.ended = "";
gr.result = "";
gr.active = true;
gr.activity_count = "0";
gr.update();
break;
}
}
}