Flow Designer Action to Add Business Days to a Time
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
https://www.servicenow.com/community/now-platform-articles/flow-designer-action-to-add-business-days-to-a-time/ta-p/2308631