Trying to cancel Workflow Timer activity

Jim Coyne
Kilo Patron

I'm working on a workflow and the first part is a manager's approval. However, if there is no manager specified for the user's Profit Center (Cost Center), we notify the user and wait 5 days for the data to be fixed in ServiceNow. There is a Business Rule on the Profit Center table that nudges any workflows along when the Manager field is updated and that is working fine. As you can see in the Approval1.jpg attachment, the workflow breaks off into 2 different branches, 1 waiting for a manager to appear on a Profit Center record (BR nudges it on) and the other is a timer for 5 days that will reject the approval.

As you can see in the Approval2.jpg attachment, the Business Rule is able to nudge the workflow on when a manager is added, but it does not cancel the timer activity. I had tried cancelling the timer with a Run Script but that did not work either. Well, it cancelled the activity, but the Scheduled Job remained and cancelled the approval at the 5 day mark anyways 🙂 Here is the code:

runScript();

function runScript() {
   var gr = new GlideRecord('wf_executing');
   var strQuery = '^state=waiting';
   strQuery       += '^context.id=' + current.sys_id;
   strQuery       += '^activity.name=Cancel in 1 minute if no manager exists';
   gr.addEncodedQuery(strQuery);
   gr.query();
   while(gr.next()) {
       gr.state='cancelled';
       gr.update;
   }
}


I could add some more code to delete the job, but I figured there had to be an easier way. Any ideas?

Thanks
1 ACCEPTED SOLUTION

Valor1
Giga Guru

There's a MUCH easier (and safer!) way to do this.
Just duplicate your "if" block after the timer, and if the answer is still "No", reject. Otherwise, do nothing.

No coding required!

BTW, it is NEVER a good idea to modify the workflow support tables. You can get yourself into a lot of trouble!


View solution in original post

12 REPLIES 12

Workflow Scratchpad

Don't forget to use the workflow scratchpad to set and retrieve ad-hoc variables from activity to activity.
http://wiki.service-now.com/index.php?title=Using_the_Workflow_Scratchpad

In your case, MB, it would be easier to set a workflow scratchpad variable indicating which (if any) of the other 2 branches had completed.


MB26
ServiceNow Employee
ServiceNow Employee

Very true, thanks for the info.


Jim Coyne
Kilo Patron

Valor, you are correct - that is much easier and safer. Thanks

Had to do the same thing today and ran across my old post.


Hi Jim


I tried the script that you have mentioned above, it solved my issue. Did you get any better way of cancelling a timer?


I have not, but as Valor pointed out, it is always best to try to add something right in the workflow itself to work around it.