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

Sorry, I would need some hand holding on this one. I tried changing the client script but couldn't manage to make it work. What would the client script look like exactly?

Hi Kevin,

I had a minor mistake in my previous suggestion, provided g_form.getParameter instead of this.getParameter. g_form is only available on client side. Sorry for that.

Please check the full process below:

STEP1: Go to "System Scheduler" application -> "Schedules" module in the left navigation menu. It will open "cmn_schedule" table list view.

Create a new schedule as shown below and copy the sys_id of the record.

find_real_file.png

Then create a schedule entry as shown in above screenshot. Detailed view shown below.

find_real_file.png

STEP2: Modify your client script as shown below

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue == 'Letterhead') {
        var gAjax = new GlideAjax('ClientDateTimeUtils');
        gAjax.addParam('sysparm_name', 'addDays2Date');
        // gAjax.addParam('sysparm_start', 'set_this_as_per_your_requirement_if_needed'); //Especially useful in case if you want to provide start date based on another field rather than current time
        gAjax.addParam('sysparm_days2add', 3);
        gAjax.addParam('sysparm_returnType', 'date'); // Pass 'date' if you want only date to be returned. Pass 'date_time' if you want date and time to be returned.
        gAjax.getXML(setDeliveryDate);
    }
    function setDeliveryDate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_delivery_date', answer);
    }
}

STEP3: Implement your server-side client callable script include as below. Please make sure the pass the schedule sys_id to the GlideSchedule API in the below script.

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    addDays2Date: function() {
        var startDT = this.getParameter("sysparm_start"); 
		// Optionally pass above parameter a start date in ISO format YYYY-MM-DD (or) 
		// a start date time in ISO format YYYY-MM-DD hh:mm:ss 
		// if you want to calculate week days from certain date or date time
		// If you are passing this parameter from client
		// please make sure to pass "sysparm_returnType" parameter also as appropriate
        var days = this.getParameter("sysparm_days2add"); 
		// For above parameter pass the number of days to add to current date
        var returnType = this.getParameter("sysparm_returnType"); 
		// For above parameter pass either 'date' or 'date_time' based on target field you need to set
        var startDate = new GlideDateTime();
        if (startDT != "" && startDT != undefined) {
            startDate.setDisplayValue(startDT);
        }
        var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
        var schedule = new GlideSchedule("<sys_id_of_24X7_weekday_schedule_you_created_in_step_1_goes_here>");
        var end = schedule.add(startDate, dur);
        if (returnType == 'date') {
            return end.getDate();
        } else if (returnType == 'date_time') {
            return endDate.getDisplayValue();
        } else {
            return "";
        }
    },
    type: 'ClientDateTimeUtils'
});

Screenshot of script include below:

find_real_file.png

System will calculate future date excluding the weekends. If you want to exclude the holidays. Just edit your schedule and exclude holidays in your schedule, your script need not change.

You may use this with "DateTime" fields also as per your need with slight modifications, and everywhere you need to add a specific number of days you can just modify the client scripts to specify the number of days you want to add.

Please follow through the comments in script blocks above to unlock the functionality you need as per your need.

Hope this helps!


Thanks and regards,

Subrahmanyam Satti

Pankhuri Kushwa
Tera Contributor

No idea about that

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Kevin,

If it's just always skipping weekends, following client script will add 3 days excluding weekends.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'Letterhead') {
        var today = new Date();
        var dow = today.getDay();
        if (dow == 3 || dow == 4 || dow == 5) { // if Wed, Thur, or Fri add 5 days
            today.setDate(today.getDate() + 5);
        } else {  // otherwises, add 3 days
            today.setDate(today.getDate() + 3);
        }
        var fmt = g_user_date_format;  // get user's date format
        fmt = fmt.replace('yyyy', today.getFullYear()).replace('MM', padZero(today.getMonth() + 1)).replace('dd', padZero(today.getDate()));
        g_form.setValue('u_delivery_date', fmt);
    }
}

function padZero(num) {
    return ('00' + num).slice(-2);
}

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Kevin,

Following will get the date 3 business day excluding weekends using Script Include.

1. Set Schedule as below.

find_real_file.png

2. Create Script Include named ClientDateTimeUtil

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    addDateAmount: function() {
        var schedule = new GlideSchedule('b4560c4597d1011086d3b4b3f153afc1');
        var startDate = new GlideDateTime();
        var days = 3;
        var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
        var endDate = schedule.add(startDate, dur);
        return endDate.getDate();
    },
    type: 'ClientDateTimeUtils'
});

3. Create onChange Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'Letterhead') {
        var ajax = new GlideAjax('ClientDateTimeUtils');
        ajax.addParam('sysparm_name', 'addDateAmount');
        ajax.getXMLAnswer(function(answer) {
            if (answer.length > 0) {
                g_form.setValue('u_delivery_date', answer);
            }
        });
    }
}

4. Execution sample. (today is March 12, 2022. Since it's Saturdays, 3 days from Monday is March 16, 2022).

Delivery date is of type Date (not Date/Time).

find_real_file.png