How to start, cancel & restart workflow with Business Rule script

michaelsharp
Mega Expert

I'm new to SN scripting and looking for a bit of help here.   I've setup an inactivity workflow to send alerts when there has not been any updates to an incident based on the priority.   I'm fine with the workflow portion, but need some help in the business rules to start, cancel and restart the workflow.

Using another article on here, I am borrowing the scripts from this link to cancel and restart the WF based on certain events/field updates:

Restart workflow when incident assignment group changes

However, I can't seem to get the business rule setup to start the workflow when an incident is created.

The business rule I have created is set to run on Insert (tried both before and after), but it doesn't want to fire off the workflow.  

Here is what I have on the script:   (Note, I re-used the code from the link, so there is probably a bit more than what is needed in here)

Any help is appreciated, thanks

(function executeRule(current, previous /*null when async*/) {

  //where current is a task record with a workflow context

  var wf = new Workflow().getContexts(current);

  while(wf.next()) {

  if (wf.hasWorkflow('4ad0eba0db436200675ef0c6ae96191a'));

  wf.deleteWorkflow(current);

  new Workflow().startWorkflow('4ad0eba0db436200675ef0c6ae96191a');

  current.work_notes = "The HL - IT Inactivity workflow has been started.";

  }

})(current, previous);

1 ACCEPTED SOLUTION

michaelsharp
Mega Expert

Sorry, late in responding or posting this as closed.   Here is the final script that works to restart the specific inactivity workflow that is in play.   FYI, inactivity monitors didn't work for our situation:



(function executeRule(current, previous /*null when async*/) {



//where current is a task record with a workflow context


var wf = new Workflow().getContexts(current);


while(wf.next()) {


if (wf.hasWorkflow('196797acdb036200675ef0c6ae96193c'));


wf.deleteWorkflow(current);


new Workflow().restartWorkflow(current);


}



})(current, previous);


View solution in original post

3 REPLIES 3

Michael Fry1
Kilo Patron

Is it too late to point you to something out of the box - Inactivity Monitors http://wiki.servicenow.com/index.php?title=Setting_Inactivity_Monitors#gsc.tab=0


michaelsharp
Mega Expert

Sorry, late in responding or posting this as closed.   Here is the final script that works to restart the specific inactivity workflow that is in play.   FYI, inactivity monitors didn't work for our situation:



(function executeRule(current, previous /*null when async*/) {



//where current is a task record with a workflow context


var wf = new Workflow().getContexts(current);


while(wf.next()) {


if (wf.hasWorkflow('196797acdb036200675ef0c6ae96193c'));


wf.deleteWorkflow(current);


new Workflow().restartWorkflow(current);


}



})(current, previous);


Hi @michaelsharp 

Here in this line of the code

if (wf.hasWorkflow('196797acdb036200675ef0c6ae96193c'));

 

what is the sys ID you mentioned here?