A

common Graphical Workflow requirement in ServiceNow is to tell the workflow to wait for some trigger before continuing. The ‘Wait For condition’ activity is available out-of-box and is very simple to configure. Usually when working with Service Requests or Change Requests I am asked how you can set up the workflow to wait for completion of all associated tasks before closing the Request Item or Change Request ticket. This can be easily accomplished by using a script in your ‘Wait For condition’ activity to query for any associated tasks that are still marked as ‘Active’. If the task is marked as ‘Active’ then it hasn’t been closed yet and the workflow should wait for that closure. Here are some sample scripts that I’ve used before to wait for task completion on both Request Items and Change Requests.


The scripts below can be pasted into the ‘Wait For condition’ activity script field as shown here…

Request Item ‘Wait For’ Scripts
Request Item – Wait for Closure of All Tasks:

//Query for all tasks to see if they are inactive
var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}

Request Item – Wait for Closure of Workflow-generated Tasks:

//Query for all tasks to see if they are inactive
var rec = new GlideRecord('sc_task');
rec.addQuery('request_item', current.sys_id);
rec.addNotNullQuery('wf_activity');
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
Change Request ‘Wait For’ Scripts
Change Request – Wait for Closure of All Tasks:

//Query for all tasks to see if they are inactive
var rec = new GlideRecord('change_task');
rec.addQuery('change_request', current.sys_id);
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}

Change Request – Wait for Closure of Workflow-generated Tasks:

//Query for all tasks to see if they are inactive
var rec = new GlideRecord('change_task');
rec.addQuery('change_request', current.sys_id);
rec.addNotNullQuery('wf_activity');
rec.addQuery('active', true);
rec.query();
if(rec.hasNext()){
answer = false;
}
else{
//Continue
answer = true;
}
Whether or not a task is checked in a workflow depends on the ‘Parent’ association as defined in the ‘SNC – Run parent workflows’ business rule on the task table. You may have to modify that business rule or create a modified version of it for the tables you’re working with. The logic behind the business rule is that it waits for certain closure conditions on child tasks and then runs workflows on any associated parents to check for things like ‘Wait for’ conditions. If your child tasks don’t meet these conditions then the parent task workflow will not be triggered when updates to the tasks are made.