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

I haven't found a better way to do this yet. I haven't run into too many places where this was really necessary so if you're worried about the overhead you can either have it run less often or filter more specifically on the items that are affected by it.


alhicks
Tera Guru

Hey James, I realize this is an old post but I'm having trouble getting my timer to wait on my variable date. Did you ever get your timer to work?


Here's my post: Need help with Workflow Timer
Trying to setup a workflow timer to look at the date/time that is keyed into a variable. We want the 2nd Task to create in the workflow, once we reach the date/time that is keyed into the variable. I found this script on the forum but it doesn't seem to look at my variable, it's only looking at answer = 20;.

var theEnd = current.variable_pool.changepricedate;
var waitSecs = gs.dateDiff(gs.nowDateTime(), theEnd, true);
answer = 20;


The case that this thread is referring to uses Wait for Condition activities instead of Timers. This allows for the date to be changed while it's waiting since the timer option doesn't allow this.

If using the Wait for Condition activity then you have to make the logic check for which date is greater and act accordingly.

If you're using the Timer activity then you need to make sure answer is set to the number of seconds (answer = waitSecs) so it knows how long to wait. Something to keep in mind is that the dateDiff function expects the date in the current users date format, so current.variable_pool.changepricedate would actually need to be current.variable_pool.changepricedate.getDisplayValue(). Depending on the system and user date settings that may or may not be an issue.

Hope that helps.


Thank you James, I'll give that a try.


odijk
Giga Expert

A couple of years late, but this question still pops up from time to time. A workflow is only evaluated when the actual record that the workflow belongs to is updated.



What you're asking seems a bit dodgy. It seems that you don't exactly know what is exactly going on in your system that could change the conditions that you are waiting for. If we are looking at a realistic example where you are waiting for all Catalog Tasks to close before you continue with the Requested Item workflow, you actually do know when to check the workflows: each time a task is closed.



This means that your nudge script is fine, but the place where you fire it isn't: Don't use a scheduled job, but use an after / async business rule on the Catalog Task table.