- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2011 12:54 PM
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
Solved! Go to Solution.
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2012 03:40 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2011 08:56 PM
Well, getting closer. I changed the script to the following:
runScript();
function runScript() {
var gr = new GlideRecord('wf_executing');
var strQuery = '^state=waiting';
strQuery += '^context.id=' + current.sys_id;
strQuery += '^activity.name=Cancel in 5 days if no manager exists';
gr.addEncodedQuery(strQuery);
gr.query();
if (gr.next()) {
var job = new GlideRecord('sys_trigger');
strQuery = 'state=0';
strQuery += '^document=wf_executing';
strQuery += '^document_key=' + gr.sys_id;
strQuery += '^name=WFTimer' + gr.sys_id;
job.addEncodedQuery(strQuery);
job.query();
if (job.next()) {
job.deleteRecord();
}
gr.state='cancelled';
gr.update();
}
}
As you can see in Approval3.jpg, the Timer is now bypassed, it just does not show as Cancelled because it is not in the Workflow Activity History table. At least it is doing what I need.
Workflow Activity History
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2012 02:09 PM
I just came across a situation where I needed to do what you scripted. This saved me lots of time.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2012 03:40 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2012 03:58 PM
For me I was branching from one activity into 3 validation activities. One of which waited for a certain state, another an escalation, and the third a timer. The user would have 3 days to validate. If they did not it would utilize the timer to close the record. If they did it would change the state and close. However if they escalated, it would rollback to repeat some tasks. Once the tasks were completed I wanted the 3 validation activities to commence again. I did not want the first timer job to run. If I did not get rid of it, I would actually have two timers set to run at different times. I do see what you are saying about doing another if check after the timer. That would be easier. I will have to see how I could do something like this to make it work.