The Now Platform® Washington DC release is live. Watch now!
on 07-23-2020 05:27 PM
Hey Everyone, I thought it might be handy to share my Flow Designer action to add business days to a time. We use this to calculate due dates in the future, or SLA due times.
Here's the inputs I have:
And the script Step:
(Here's the script itself: )
(function execute(inputs, outputs) {
var newDateTime;
var timeToAdd;
var currentDateTime = new GlideDateTime();
var msInDay = 24 * 60 * 60 * 1000; //One day in Milliseconds as default day
var msInBusDay = 24 * 60 * 60 * 1000; //One day in Milliseconds as default business day;
if (!inputs.startTime) {
newDateTime = new GlideDateTime();
}
if (inputs.useSched == true && (inputs.sched)) {
// Here's a line of javascript that is terrible. It relies on a specific day I picked to be a "generic Business Day". Don't use it. I'm sorry.
//msInBusDay = new GlideDateTime(new GlideSchedule(inputs.sched.getUniqueValue()).duration(new GlideDateTime("2019-02-12 00:00:00"), new GlideDateTime("2019-02-13 00:00:00")).getValue()).getNumericValue();
msInBusDay = 8 * 60 * 60 * 1000; //SET THIS (8) TO YOUR BUSINESS DAY LENGTH IN HOURS, OR, USE MY SUPER LINE ABOVE TO CALCULATE ;)
var sched = new GlideSchedule();
sched.load(inputs.sched.getUniqueValue());
var durCalc = inputs.daysToAdd * msInBusDay;
var durToAdd = new GlideDuration(durCalc); // Translate into Duration Object.
var d = new GlideDateTime();
d.setDisplayValue(inputs.startTime.toString());
newDateTime = sched.add(d, durToAdd);
} else {
newDateTime = new GlideDateTime();
newDateTime.setDisplayValue(inputs.startTime.toString());
timeToAdd = msInDay * inputs.daysToAdd;
newDateTime.add(timeToAdd);
}
outputs.outDateTime = newDateTime;
})(inputs, outputs);
And pass the output back to the Action Outputs section, and you are done!
PLEASE NOTE: My script was built for my business, and you may have to change it for your days. The "Crazy" line of javascript was specifically to calculate a day based on multiple schedules
Take this as a guide, and go to town!
Special thanks to my friend Kevin from Australia Post for some duration logic!
Keep living that #Fladvoate life!
- Andrew
When I got to "Here's a line of javascript that is terrible. It relies on a specific day I picked to be a "generic Business Day". Don't use it. I'm sorry." I started laughing pretty hard.
Very cool -- thanks for sharing.
Haha, I was sharing to help someone and I saw that line (Which I remember writing, and being super proud/disgusted by) and I couldn't include it as a real thing, but couldn't bring myself to delete it. Glad I'm helping 😉
This is great! However, everyone please upvote the "add schedule to Flow Designer" idea.
https://community.servicenow.com/community?id=view_idea&sysparm_idea_id=c98e5f7fdb8f90909e691ea668961970&sysparm_idea_table=x_snc_com_ideation_idea&sysparm_module_id=enhancement_requests
Hi Andrew,
I have similar requirement. But the difference is I need to subtract the days instead of addition. I added minus sigh in this line>> newDateTime = sched.add(d, durToAdd) ----> newDateTime = sched.add(d, -durToAdd)
But not getting the past date. Need help on this.
Thanks in Advance.
Cool thing, Andrew!
@ All, in Paris the name "outDateTime" will be changed by the system to "outdatetime", so you need to change the 3rd last line in script accordingly.
Hi,
I am trying this but I am getting this error: Error: The undefined value has no properties.,Detail: The undefined value has no properties. The error isin the Step Output Data.
How do I resolve this one please?
Thanks.
Voted!
Encountered the same challenge now. Hope they implement it soon.
Little background: Our project started using Flow Designers for our new catalog items last year. Unfortunately, since expected delivery time is important to our client (for this year's targets), we have to start converting them back to workflow just so we can have accurate delivery date reflected in the Service Portal (delivery date that considers business hours and holiday)
Hi all,
any news from anyone on plans if the schedule function will be added to flow designer OOTB without the need to create custom action?
/Petr
I'm using Utah, for some reason I was getting an error:
"Flow Designer: Encountered undeclared output variable: outDateTime" I too saw the case change when setting up my output variable.
RESOLUTION:
Then the error went away and I got a result.!