how to exclude weekends from onchange client script date set

kevin_munguia
Tera Guru

I currently have an onchange client script that will add 3 days to a date field in my service portal. However, I want to make sure I exclude weekends. I currently have a schedule set in cmn_schedule_list.do so how do I point back to this table to look at the specific schedule that excludes weekends?

The scripts I'm currently using below. 

onchange client script: 

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

if (newValue == 'Letterhead') {

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
function calThreeDays(response){
var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('u_delivery_date', answer);
}
}

script include: 

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){

var date = new GlideDateTime(gs.now());
date.addDays(3);
return date.getDate();
},
type : 'ClientDateTimeUtils'

});

 

1 ACCEPTED SOLUTION

kevin_munguia
Tera Guru

CATALOG CLIENT SCRIPT:

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

if (newValue == 'Letterhead') {

var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.getXML(calThreeDays);
}
function calThreeDays(response){
var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('u_delivery_date', answer);
}
}

 

SCRIPT INCLUDE:

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){

var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekUTC() == 2){
date.addDays(2); // if MONDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 3){
date.addDays(2); // if TUESDAY then add 7 days to get Monday
}
else if(date.getDayOfWeekUTC() == 4){
date.addDays(4); // if WEDNESDAY then add 8 days to get Monday
}
else if(date.getDayOfWeekUTC() == 5){
date.addDays(3); // if THURSDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 6){
date.addDays(2); // if FRIDAY then add 3 days to get Monday
}
else if(date.getDayOfWeekUTC() == 7){
date.addDays(1); // if SATURDAY then add 6 days to get Monday
}
else if(date.getDayOfWeekUTC() == 1){
date.addDays(0); // if SUNDAY then add 6 days to get Monday
}
return date.getDate();
},
type : 'ClientDateTimeUtils'

});

View solution in original post

10 REPLIES 10

Jan Cernocky
Tera Guru

Hi Kevin,

do I understand well, that with adding 3 days you don't want to hit the weekend?

Meaning if you set Tuesday, the result is Friday but if you set Wednesday it will not be Saturday but Monday?

In this case you can utilize the 'getDayOfWeekLocalTime' method and add more days.

var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekLocalTime() == 3) {
  date.addDays(5);
}
else if (date.getDayOfWeekLocalTime() == 4) {
    date.addDays(4);
}
else {
  date.addDays(3);
}
return date.getDate();

 

 

I added the script as follows in my script include, but I still get the same result where it doesn't exclude the weekend. Am I missing something? 

 

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){

var date = new GlideDateTime(gs.now());
if(date.getDayOfWeekLocalTime() == 3) {
date.addDays(5);
}
else if (date.getDayOfWeekLocalTime() == 4) {
date.addDays(4);
}
else {
date.addDays(3);
}
return date.getDate();
},
type : 'ClientDateTimeUtils'

});

thanks for the support.

Subrahmanyam2
Giga Guru

Hi Shaan,

Create a 24X7 floating Weekday Schedule and then add below function to your script include.
In the function pass the sys_id of the cmn_schedule 24X7 schedule record to GlideSchedule API.
In the client script change the function name, and also pass another parameter "sysparm_days2add", and give desired number of days you want to add.

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    addDateAmount: function () {
        var date = new GlideDateTime(gs.now());
        date.addDays(3);
        return date.getDate();
    },
    addDays2Date: function () {
        var startDate = new GlideDateTime();
        var days = g_form.getParameter("sysparm_days2add");
        var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
        var schedule = new GlideSchedule("62fd68ff870201108bc2da873cbb35f8"); // Create a 24X7 Weekday schedule in cmn_schedule table and pass the sys_id of the cmn_schedule here
        var endDate = schedule.add(startDate, dur);
        return endDate.getDisplayValue();
    },
    type: 'ClientDateTimeUtils'
});

 

System will calculate future date excluding the weekends.

Please impersonate users who have different time zones set in their profile and test.

You may want to improve it as per your need.

 

Please check the product documentation link for GlideSchedule API here.

https://developer.servicenow.com/dev.do#!/reference/api/rome/server/no-namespace/c_GlideScheduleScopedAPI#r_ScopedGlideScheduleAdd_GlideDateTime_GlideDuration?navFilter=glideschedule

 

Thanks and regards,

Subrahmanyam Satti