- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2015 08:54 AM
I feel as though this should be a simple task but I am not proficient at scripting yet. I've not found anything quite spelling it out for me as to how to script it well.
Background
I have a workflow running on the Incident table who's purpose is to stop the SLA clock from running until the due date is met; at which point it clears the due date and sets the INC back to active.
Issue
The problem comes in when someone changes the 'Due Date' value to something else. The workflow still waits for the original date to continue with the workflow.
Goal
I wanted put some script to look at the 'Due Date' with the OnChange condition. to fix the problem. Or possibly use the 'Wait for Condition' action instead of the regular timer, because "The workflow evaluates the wait for condition each time the current record is updated." which would make the Due Date field be checked to make sure it still has the same due date.
any help would be very much appreciated. let me know if you need more detail to understand the issue.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2015 12:08 PM
I've been able to piece together a solution, thanks to some code posted by Dan Patino here.
Hopefully someone else will find this helpful.
I setup a business rule to run on the Task table, on updates when the due_date changes.
The parts of this code to enter in your own data are on lines 3 and 35.
var PAtimer = retrieveTriggerRecord('NAME OF TIMER');
NAME OF TIMER is literally just the name given to the timer action you want to be updated.
var newDT = new GlideDateTime(DATE FIELD);
DATE FIELD - I just put in the date field from the task table; current.due_date.getDisplayValue()
Here's the code in its entirety:
function onAfter(current, previous) {
//Reschedule Pending to Active timer
var PAtimer = retrieveTriggerRecord('NAME OF TIMER');
if(PAtimer != 'none')
rescheduleNextAction(PAtimer);
function retrieveTriggerRecord(timer){
//Find the workflow context record
var grContext = new GlideRecord("wf_context");
grContext.addQuery('id',current.sys_id);
grContext.query();
if(!grContext.next())
return 'none';
//Find the Workflow Executing record
var grExecuting = new GlideRecord("wf_executing");
grExecuting.addQuery("context", grContext.sys_id);
grExecuting.addQuery("activity.name", timer);
grExecuting.addQuery("activity.activity_definition.name", 'Timer');
grExecuting.query();
if (!grExecuting.next())
return 'none';
//Find the sys_trigger record
var grTrigger = new GlideRecord("sys_trigger");
grTrigger.addQuery("name", 'WFTimer' + grExecuting.sys_id.toString());
grTrigger.query();
if (grTrigger.next())
return grTrigger;
else
return 'none';
}
function rescheduleNextAction(trigger){
var newDT = new GlideDateTime(DATE FIELD);
trigger.next_action = newDT.toString();
trigger.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2015 08:20 AM
Thanks again guys for the responses. Sorry about the late reply, I've been quite busy and haven't had a chance to run tests and implement your suggestions. I wasn't able to get either method to work. However, I went back to square one to take another swing at the issue, and think that I may have a solution (might need your help again)
From Mike's solution, I put this together the following BR:
function onBefore(current, previous) {
var dueDate = new GlideDateTime();
dueDate.setDisplayValue(current.due_date);
var gr = new GlideRecord("sys_trigger");
gr.name = current.number + " Due Date Adjustment";
gr.next_action = dueDate;
gr.trigger_type = 0; //Run Once
gr.job_context = "Auto scheduled job due to a due date adjustment of " + current.number;
gr.script = "current.state = 2; current.update();";
gr.insert();
}
Unfortunately it didn't create the trigger. Didn't dig in deep with it yet because I've had a thought on how to approach the workflow differently.
I rebuilt the WF to branch into the same 'due_date' timer, but also a 'wait for wf event' action. That event is triggered by a BR that waits for the conditions of 'Active=true' and 'due_date' is changed.
This is good because it kills the associated records on the 'sys_trigger' table. However, the WF doesn't start again as I thought it would, even though it matches the conditions in which the WF is supposed to start. I suppose it is maybe because a WF can only run once per record? I've started to look at script to restart a single WF. I found one about Resetting a single workflow, but the noted script didn't work (possibly b/c the post was two years old). Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2015 10:00 AM
Have the business rule as:
var wf = new Workflow().getRunningFlows(current);
var exeact = new GlideRecord('wf_executing');
exeact.addEncodedQuery('activity.activity_definition=3961a1da0a0a0b5c00ecd84822f70d85^context=' + wf.sys_id);
exeact.query();
if(exeact.next())
{
var trigger = new GlideRecord('sys_trigger');
trigger.addEncodedQuery('nameLIKE' + exeact.sys_id);
trigger.query();
if(trigger.next())
{
trigger.next_action = current.due_date;
trigger.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2015 12:49 PM
Hi again Mani (and anyone else who can help),
I've decided to take another swing at this problem of mine, and wondered if I might get some more helpful feedback.
I've attempted other ways to tackle this issue, and even successfully created triggers for the new date set on a given INC record. However, the issue circled back to the original sys_trigger record still meeting it's old "Next Action" date, and taking the record out of pending.
I tried what you had provided in August, but nothing seemed to happen. So I thought I'd try debugging it. Of course, right away I hit an issue that I don't know how to get around, and couldn't find anything on community. It might be some generally common knowledge that I am just missing because I'm still new to scripting...
I tried adding some gs.print lines into the code to return values at strategic points, in order to determine where the breakage is. In the function you provided, it doesn't seem to be able to pull anything from "wf.sys_id"; it says that it is undefined. With working with various other code, I've also been able to get an output of "[object GlideRecord]". What kind of tags can I put on a given glide variable to show me it's value or name? I've tried getDisplayValue() and some others, but I can't get anything to work.
function onAfter(current, previous) {
var wf = new Workflow().getRunningFlows(current);
var exeact = new GlideRecord('wf_executing');
exeact.addEncodedQuery('activity.activity_definition=3961a1da0a0a0b5c00ecd84822f70d85^context=' + wf.sys_id);
exeact.query();
gs.print("This is wf.sys_id: " +wf.sys_id);
if(exeact.next()) {
var trigger = new GlideRecord('sys_trigger');
trigger.addEncodedQuery('nameLIKE' + exeact.sys_id);
trigger.query();
if(trigger.next()) {
trigger.next_action = current.due_date;
trigger.update();
gs.addInfoMessage("Trigger Next Action:"+trigger.next_action);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2015 12:08 PM
I've been able to piece together a solution, thanks to some code posted by Dan Patino here.
Hopefully someone else will find this helpful.
I setup a business rule to run on the Task table, on updates when the due_date changes.
The parts of this code to enter in your own data are on lines 3 and 35.
var PAtimer = retrieveTriggerRecord('NAME OF TIMER');
NAME OF TIMER is literally just the name given to the timer action you want to be updated.
var newDT = new GlideDateTime(DATE FIELD);
DATE FIELD - I just put in the date field from the task table; current.due_date.getDisplayValue()
Here's the code in its entirety:
function onAfter(current, previous) {
//Reschedule Pending to Active timer
var PAtimer = retrieveTriggerRecord('NAME OF TIMER');
if(PAtimer != 'none')
rescheduleNextAction(PAtimer);
function retrieveTriggerRecord(timer){
//Find the workflow context record
var grContext = new GlideRecord("wf_context");
grContext.addQuery('id',current.sys_id);
grContext.query();
if(!grContext.next())
return 'none';
//Find the Workflow Executing record
var grExecuting = new GlideRecord("wf_executing");
grExecuting.addQuery("context", grContext.sys_id);
grExecuting.addQuery("activity.name", timer);
grExecuting.addQuery("activity.activity_definition.name", 'Timer');
grExecuting.query();
if (!grExecuting.next())
return 'none';
//Find the sys_trigger record
var grTrigger = new GlideRecord("sys_trigger");
grTrigger.addQuery("name", 'WFTimer' + grExecuting.sys_id.toString());
grTrigger.query();
if (grTrigger.next())
return grTrigger;
else
return 'none';
}
function rescheduleNextAction(trigger){
var newDT = new GlideDateTime(DATE FIELD);
trigger.next_action = newDT.toString();
trigger.update();
}
}