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

LaurentChicoine
Tera Guru

This is an old post but I had a similar scenario (branch with a timer on one of the branches and a condition on the other) recently and I wanted to share my solution.

At first I went with the solution of having an IF condition following the timer, however this caused issue in the Stage field of the Requested item. Because the timer was still active the stage was still computed to the stage of the timer activity. And using the stage on the timer was required in my case.

So what I did is create a subflow with that branch with both branches connected to the End activity of the workflow. That way when one of the 2 conditions (timer or the other condition) is completed, the subflow is ended and that cancels the timer properly.

It adds another workflow into the mix but at least the Timer activity is cancelled properly.

I hope this can help others with the same issue of cancelling a Timer activity when using branch (or any other type of activity).

Any way you can share a screenshot of your workflow, Laurent? I'm not quite following but I think it may help me in what I am trying to do. 

Here is an example of the subflow. In this use case, we want to wait for an asset to be created in our list of assets, after 2 business days if the asset has not been created, we create a catalog task. Therefore whichever comes first will execute and reach the end node therefore cancelling the other wait condition.

find_real_file.png

This subflow is called by a main workflow which wait for the subflow completion before creating the next task:

find_real_file.png