Restarting a specific workflow for a record

tahnalos
Kilo Sage

I have heard that restartWorkflow () can restart the workflow for a record.  However, a client implementation has about 4 workflows running at the same time.  I need to restart one of the 4 workflows and do not want to restart the other three. Is there a method available that can do this?

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Yes, you can.

Try below in background script once.

var gr = new GlideRecord('sc_req_item'); //replace table name
gr.get('sys_id_of_record'); // replace sys_id of record

 var workflow = new Workflow();
      workflow.cancel(gr);

var newWorkflow = new Workflow();
newWorkflow.startFlow(new Workflow().getWorkflowFromName('Pass workflow name here'), gr, '');

We needed this for a Requested Item in our instance.  User in a Service Catalog Task marked the SCTASK record as Closed, instead of Cancelled.  Cancelled the entire workflow!  DOH!!

 

With the above script, I was able to salvage the RITM and it was able to get completed AND closed successfully.  Thanks @Jaspal Singh 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you explain the need to restart the workflow?

The reason being it would start from the beginning and if any approvals etc are present it would trigger that as well

If you still want to restart please try script mentioned by Jaspal

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

We have a table that has 25 different workflows running on it.  Each workflow corresponds to a stage of a record belonging to that table.  Multiple workflows for different other functions are also running at the same time.

The issue is we discovered that we need to revisit some of the workflow due to checks in later stages that require a revisit of one or more earlier stages.

The problem is that the workflow has already been completed for these earlier stages so we want to restart just the stages that were flagged by our code.

Most of the code that is being suggested is more of a "blunt force" method that handles all workflows and we need to deal with only that is affected.

Thanks