- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2020 12:08 PM
I have a task on a workflow which will disable monitoring for a CI at a set appointment time. This works for a straight flow, however I'm trying to capture edge cases where the original appointment time needs to be changed.
I've added an option to catch a time update on the Request Item, which triggers an event, and this works.
I'm having two problems:
1. Completion of one of the timers and execution of the script task doesn't cancel the other branch, since a Join wasn't used to bring both of these branches together I thought that the first one to reach the script would cancel the other. You can see in the attached screenshot the "wait for event" is still running, even though the subsequent "Run Script" action has already executed. Maybe the Join later in the flow is impacting this, but I don't understand why it would.
2. The event trigger upon appointment change will work once, but how can I ensure that subsequent changes to the appointment time will also be caught, and update the timer with the new time selected.
Very much appreciate any tips and insights if someone else has run through a similar scenario.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2020 05:23 PM
Well...
When a workflow timer activity is triggered, the result is that a scheduled job is created in the sys_trigger table. The challenge is finding the right entry to update - but it can be done.
I am assuming that your business rule runs against the sc_req_item table. For this example, I'll also assume that the variable on the RITM that you are using in the timer is called appointment_time.
The following should give you what you are looking for.
Business Rule: Advanced, runs before, on update.
Condition is item = your catalog item.
Script:
(function executeRule(current, previous /*null when async*/) {
//First, we have to get the context for the workflow running against current.
var context = '';
var wf = new GlideRecord('wf_context');
wf.addQuery('id', current.sys_id);
wf.query();
while(wf.next()) {
context = wf.getValue('sys_id');
}
//this is the name of your timer activity in the worklow - since you can have more than one.
var timer_name = 'This is my timer!';
var doc_id = '';
//Now we have to get the executing workflow activity records associated to the context.
var wf_executing = new GlideRecord('wf_executing');
wf_executing.addQuery('context', context);
wf_executing.query();
//Now we cycle through the activities to find the one we have named.
while(wf_executing.next()){
if(wf_executing.activity.name == timer_name) {
doc_id = wf_executing.getValue('sys_id');
}
}
//Now, we use everything we have learned to find the right entry in the scheduled job table.
var schedule = new GlideRecord('sys_trigger');
schedule.addQuery('document_key', doc_id );
schedule.query();
while(schedule.next()) {
//and here comes the magic
//update the next_action field using the value in the appointment_time variable.
schedule.next_action = current.variables.appointment_time;
schedule.update();
}
//Success!
})(current, previous);
If this was helpful, or correct, please be kind and remember to mark appropriately!
Michael Jones - Cloudpires
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2020 09:57 AM
Hi Michael, I ran into one pretty big bug depending on the scenario triggering this script to run.
If the criteria being used to trigger the business rule takes place and the RITM does not yet have a scheduled job associated with it, this will result in an update to the next run time for all sys_triggers which have an empty doc_id.
This includes quite a few system triggers such as the email read processor, SMTP sender, and event processing - about 200 in total.
In case anyone else comes across this post and uses your script, can you update the content of your original post to place the final section of the script within the If statement?
if(wf_executing.activity.name == timer_name)
Based on my testing, this should account for any scenarios where this script gets run against a record which doesn't have the targeted "Timer Name".
This will also prevent it from updating a number of system triggers if the timer name is ever changed in the workflow. It will just stop working altogether, instead of changing the next run time of the system triggers)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2021 03:30 AM
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2021 10:11 AM
Michael, thank you very much! Great code!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 06:32 AM
Michael,
This works like a champ I was able to use this example to my use case on canceling a timer. Great job!