Workflow Wait for Condition

jfarrer
Mega Guru

I have a workflow where I want to wait for a deployment date before continuing on with the workflow. The date is supplied by the person filling out the form. I have included a "Wait for Condition" activity with a script that compares the current date to the deployment date. The script is working fine but it only checks when something is updated on the Requested Item. The workflow is not waiting for anything else besides a date to pass.

In reading this:
Wait for Condition - Attachments
and this:
http://wiki.service-now.com/index.php?title=Force_Workflow_to_Wait_on_Non-Workflow_Task
it looks like I need to have a script to "nudge" the workflow to recheck for pending activities. I was surprised to see that a "nudge" was required in the first place.

I have set up the following script to run as a Scheduled Job every 10 minutes and it seems to be working, but it does go through and trigger every workflow to recheck, which seems like a lot of overhead.



forceReqItemUpdate();

function forceReqItemUpdate() {
var wf = new Workflow();
var gr = new GlideRecord("sc_req_item");
gr.addQuery("active", true);
gr.query();
while(gr.next()) {
//gs.log("Triggering workflow for: " + gr.number);
wf.runFlows(gr, 'update');
}
}


I don't want to be doing more than is necessary. I thought about trying to trigger for just this workflow, but I would like for the "Wait for Condition" to work more generally. I also thought about trying to check for just active workflows that have a "Wait for Condition" in them, but didn't see an easy way to do this.

Any ideas for a better way?

17 REPLIES 17

A few years experience makes a big difference it seems. I totally agree with you Martijn. In the vast majority of the circumstances it makes much more sense to put the trigger in specifically where the change is happening with a business rule as you suggest. The only way I could see it still making sense would be if you had no way to tie back to the item with the workflow or if you're doing a very large number of these in a variety of places and it would just be simpler to have the scheduled job. That would definitely be the exception.


Martijn - I know it's been awhile since you posted this but what would that look like?



I just noticed we're having this issue of hung workflows due to exactly what James originally posted about and though I'll probably need to run his script, I'd prefer something other than a Scheduled Job if possible.


To get the workflows to run you'll need a business rule or something similar that runs when the dependent value gets updated. Then you can use the Workflow API to call the runFlows function. It's documented here: https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=r_WF-runFlows_GR_S


So the script would be something like (borrowing heavily from yours obviously), run on Update after condition Catalog Task is closed?:



forceReqItemUpdate();  


 


function forceReqItemUpdate() {  


  var wf = new Workflow();  


  var gr = new GlideRecord("sc_req_item");  


  gr.addQuery("active", true);  


  gr.addQuery('sys_id', current.request_item);


  gr.query();  


  while(gr.next()) {  


      //gs.log("Triggering workflow for: " + gr.number);  


      wf.runFlows(gr, 'update');  


  }  


}  


There is already a default set of business rules that should handle running the workflows for when Catalog Tasks are closed. These are "Populate parent field in sc_task table" rule that sets the Parent field from the Requested Item field and then there's "SNC - Run parent workflows" that runs the workflows for the parent of any task. If you've customized the Catalog Task State or are doing things outside the normal catalog process then it might be preventing those from running properly. Usually it would take being dependent on some other non-task record to require a script like that.