logo

NJP

Select only Business Days excluding holidays

Import · Jan 14, 2020 · article

Hi,

Earlier in my article, i have shown you how we can restrict the user to select only weekdays. In general, even in weekdays, there could be specific business times (such as 9-5) and also holidays which we want to exclude from the selection.

In this article, i will show you how we can use the schedule feature of SN and can restrict user to not select any date/time which is not in the schedule.

For this, we will use the GlideSchedule API of ServiceNow. Using Schedules, you can add your own list of holidays, mention the timings of your working environment.

SN by default provides a list of standard schedules. you can either modify them or create a new one.

For this we need 2 scripts.

1. Client Script: To call the SI to check if the selected date is in scheudle or not

2. Script Include: Which captures the date and check if the date is in schedule and return true/false.

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Show error if selected date/time is not in schedule.
   var ga = new GlideAjax("CheckSchedule");
   ga.addParam("sysparm_name","isInSchedule");
   ga.addParam("sysparm_date",newValue);
   var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
    if(answer == 'false') {
        g_form.showFieldMsg('expected_start','Select the date/time which is in schedule','error',true);
    }
    }
}

Script Include

Name: CheckSchedule

ClientCallable: True

var CheckSchedule = Class.create();
CheckSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isInSchedule : function() {
        var selected_date = this.getParameter("sysparm_date");
        gs.log("Date is "+selected_date);
        var d = new GlideDateTime();
        d.setDisplayValue(selected_date);
                //mention your schedule sys_id here.
        var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); 
        if(schedule.isInSchedule(d)) {
            return true;
        } else {
            return false;
        }
    },
    type: 'CheckSchedule'
});

Let me know if you have any questions in the comments below.

Mark the article as helpful and bookmark if you found it useful.

View original source

https://www.servicenow.com/community/developer-articles/select-only-business-days-excluding-holidays/ta-p/2315876