Scripting Help- weekend and bank holidays

gbade
Kilo Contributor

Hi SNC,

I need some assistance please regarding the below script. It's for a five days validation , it works for five days but does not take into account weekends or bank holidays.

Here's the script below and the script include . Please note,not great with scripting, I had help with this script. Appreciate your assistance.

Client Script

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

    //Type appropriate comment here, and begin script below

var mydate = newValue;

var dtvalid;

var ga = new GlideAjax('daysdiff');

ga.addParam('sysparm_name','date_selection');

ga.addParam('sysparm_sdt',mydate);

ga.getXML(UserDateParse);

function UserDateParse(response)

{

dtvalid = response.responseXML.documentElement.getAttribute("answer");

if (dtvalid < 5) {

g_form.setValue('date',' ');

alert("The   date should be atleast 5 days from today ");

}

}

Script Include

var daysdiff= Class.create();

var rec2 = new ArrayUtil();

daysdiff.prototype = Object.extendsObject(AbstractAjaxProcessor, {

date_selection: function() {

var sec1 = this.getParameter('sysparm_sdt');

var gdt = new GlideDateTime();

gdt.addSeconds(gdt.getTZOffset() / 1000);

var sec2 = gdt.getDate();

return DateSelect(sec2, sec1);

}

});

function DateSelect(rec3, rec4)

{

var dif = gs.dateDiff(rec3, rec4, true);

var time = (parseFloat(dif)*1000)/86400000;

gs.log('Today Date>>>>' + rec3);

gs.log('Planned Date>>>>' + rec4);

gs.log('Time Difference>>>' + time);

return time;

}

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I would use the DurationCalculator that exists to do the serverside calculation. Take a look here: https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/script/server-scripting/concept...


And if you run into trouble, just ask.


View solution in original post

8 REPLIES 8

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

I would use the DurationCalculator that exists to do the serverside calculation. Take a look here: https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/script/server-scripting/concept...


And if you run into trouble, just ask.


Thanks, will give it a go.


is a script needed for the duration calculator ? if so is it a business rule.


how do i incorporate it into the client script or ui policy on the date variable pls


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Why not use a Schedule and include holidays:


Schedules



Then in your script you can check to see if the end time is within the schedule and if not add another day.