Trigger Workflow Timer 5 days before a specific Date/Time given in RITM variable value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2024 07:21 PM
Hello Everyone,
Need one help.
I have a requirement to trigger a timer if current date is 5 days or less than estimated_end_date (which is a RITM variable).
Now this date is editable, that is, users can edit the variable value anytime.
In the workflow timer script, I have written the below code, but sadly the timer is not firing.
Can you please tell me where I am going wrong here.
Thanks in advance.
var diff = gs.dateDiff(gs.nowDateTime(), current.variables.estimated_end_date, true);
var days = diff / (24 * 60 * 60);
if (days <= 5) //if no. of days is 5 or less
answer = 0;
else
answer = 1000000; //an arbitrary large value if no. of days is more than 5
Please mark this post as a solution and also as helpful, if this resolves your issue or query.
Thanks,
Subhadeep Ghosh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-20-2024 12:13 AM
Exactly @Ankur Bawiskar that's what I figured; also explained to customer.
But as you know, customer is always right š
They want this date field to be editable in the RITM form after request is submitted.
So, have to do this somehow.
Please mark this post as a solution and also as helpful, if this resolves your issue or query.
Thanks,
Subhadeep Ghosh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-19-2024 08:20 PM
Your script appears to be in good shape.It's important to note that gs.nowDateTime() returns the current date time in the user-defined format while your current.variables.estimated_end_date returns UTC by default.
Regarding this point "Now this date is editable, that is, users can edit the variable value anytime."
When the workflow reaches the Timer activity, the Timer activity schedules a job that calls a script. The script calls fireEvent (wf_executing, timer).
var w = new Workflow(); w.fireEvent(current, 'timer');
The Next Action will be determined and set when the Timer activity is executed. Subsequent updates to the variable will not automatically update the Next Action unless custom measures are implemented to achieve this.
You can find the schedule item as follows:
- Open the Schedule [sys_trigger] table.
- Filter name by: Workflow<the_workflow_context_sys_id> (ex: Workflow5da1711c47ec4210ab9bb6bf016d438e)
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-20-2024 01:43 AM
So @Tai Vu , basically, when a variable's value is set at the time of RITM creation, that value remains fixed for the workflow context throughout the lifetime of the RITM.
That is, later if that variable's value is changed in RITM, then the workflow context fails to read the new value.
Is this understanding correct?
Please mark this post as a solution and also as helpful, if this resolves your issue or query.
Thanks,
Subhadeep Ghosh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-20-2024 02:14 AM
Indeed, the situation arises because the workflow has already executed the Timer step. Let me illustrate with an example:
1. Workflow execute the Timer step, and calculate the wait time.
2. Consequently, a schedule item is generated, and the Next Action is determined based on the outcome of step 1.
3. Subsequent updates to the Requested item will not automatically overwrite the Next Action of the schedule item. [2].
So now you can think of having a rule that triggers when the variable get changed, then we will query the schedule item with the workflow context sys_id and update the next action to the new date.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā02-20-2024 02:34 AM
Update sample rule.
When: Async/After
Conditions:
current.variables.estimated_end_date.changes();
Script:
(function executeRule(current, previous /*null when async*/) {
var scheduleItem = '';
var wkfw = new Workflow();
var context = wkfw.getContexts(current);
if(context.next()){
scheduleItem = 'Workflow' + context.getUniqueValue();
}
if(gs.nil(scheduleItem)){
return;
}
var grSchedule = new GlideRecord('sys_trigger');
grSchedule.addQuery('name', scheduleItem);
grSchedule.setLimit(1);
grSchedule.query();
if(grSchedule.next()){
grSchedule.setValue('next_action', current.variables.estimated_end_date);
grSchedule.update();
}
})(current, previous);
Cheers,
Tai Vu