- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-20-2023 10:39 AM - edited â07-20-2023 10:40 AM
We have a timer script that waits 7 days before a date variable to run:
Workflow Timer:
var number_of_days = -7;
var time = (parseInt(gs.dateDiff(gs.nowDateTime(), current.variables.planned_start_date.getDisplayValue(), true), 10) + (number_of_days * 86400));
// Set 'answer' to the number of seconds this timer should wait
answer = time;
When the date variable is updated (planned_start_date), I want the timer script to run again.. It is a long shot probably but I found this code that updates the next_activity for that timer when variable is updated however the problem is that it will update next_action to the date from the updated variable date. What i want to check is if there is any way to add the workflow timer script somewhere in this code below where it is updating the next_action , to maybe run this timer script again?
In the code below, where it says " schedule.next_action = ritm.variables.due_date;" , I want schedule.next_action to be the new date calculated so if date variable field is updated to 30/07/2023, next_action should be 7 days before this date but I'm not sure how to script this calculation?
(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 = 'wait_for_start';
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 = ritm.variables.due_date;
schedule.update();
}
//Success!
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-20-2023 10:59 AM
If you do not care about a schedule then this should be simple like the below. I did not test but it gives you the basic idea.
var dt = new GlideDateTime();
dt.setValue(current.getDisplayValue("variables.myVar"));
dt.addDaysUTC(-7);
schedule.next_action = dt;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â07-21-2023 03:47 AM
Thanks for reply. This is the right track.. I have an issue when I put it into my code, it seems to be deleting the sys_trigger entry / skipping timer when I make the update , when the business rule runs.
This is how I've added it, towards the end.
(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 = 'wait_for_start';
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();
var dt = new GlideDateTime();
while(schedule.next()) {
//and here comes the magic
dt.setValue(current.getDisplayValue("variables.planned_start_date"));
dt.addDaysUTC(-7);
schedule.next_action = dt;
schedule.update();
}
//Success!
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â08-08-2023 08:20 AM
Hi Drew,
We noticed after a week that this was also updating other records in sys_trigger table (385/720) were updating the next action field to match what the business rule specified and it wasn't just doing the workflow timer record in the sys_trigger table. Is there something in the code that is updating everything or I need to check?
(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 = 'wait_for_start';
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
var dt = new GlideDateTime();
dt.setDisplayValue(current.getDisplayValue("variables.planned_start_date"));
dt.addDaysUTC(-21);
schedule.next_action = dt;
schedule.update();
}
//Success!
})(current, previous);