If end date is more than 10 business days alert should populate

Servicenow de11
Tera Contributor

Hello,

 

I have a requirement that is In the catalog item the user select 'Date Needed by' and 'Date Needed Until', if the Date Needed Until is selected more that 10 business days alert should populate. Attached screenshot of the variables.

Servicenowde11_0-1709723506646.png

 

I have tried the below script but it is not working. Can anyone help to achieve this.

 

Script Include:

 

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);
 
 var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');//Sys id of 8-5 weekdays excludingholidays
 var gdt = new GlideDateTime();
 schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Sys id of 8-5 weekdays
 var days = 11;
 var dur = new GlideDuration(60*60*9*1000*days);
 gs.info('Duration is'+dur);
 var end = schedule.add(d, dur);
 gs.info('end'+end);
 var value = end.getDate();
 gs.info('value'+value);

return value;

},
 type: 'CheckSchedule'
});  
 
Client Script on 'Date Needed Until' variable
 
function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
 return;
 }

var dateNeeded = g_form.getValue("DateNeededBy");
var dateUntill = g_form.getValue("DateNeededUntil");

if (dateNeeded == "" || dateUntill == "") {
        return;
    }

 var ga = new GlideAjax("CheckSchedule");
 ga.addParam('sysparm_name', 'isInSchedule');
 ga.addParam('sysparm_strt',dateNeeded);
 ga.addParam('sysparm_date', dateUntill);
 ga.getXML(parseResponse);

function parseResponse(response) {
 var answer =response.responseXML.documentElement.getAttribute("answer");
 var diffInDays = answer/(60*60*24);
 if(diffInDays > 11){
        alert("Loaner equipment maximum is 10 Business days. Please select the date accordingly");
        g_form.setValue("DateNeededUntil", "");
        return false;  
    }
}
}
 
Thanks in Advance
1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @Servicenow de11,

 

Working with dates and times is fun at times, especially when you throw schedule in ; )

Use the below update tried and tested onChange Catalog Client Script and Script Include below that caters for business days (Based on a schedule). Please take a look at the comments in the Script Include for any adjustments based on schedule you may need to make. This is currently based on the baseline 8-5 M-F schedule.

 

Note - Make sure the Script Include has the checkbox 'Client callable' checked (set to true)

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

 

onChange Catalog Client Script: (Make sure you have this set for onChange of the 'End Date' field)

 

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

    if (isLoading || newValue == '') {
        return;
    }

    var start_date = g_form.getValue('start_date');
    var dateGA = new GlideAjax("calcDate"); //name of script include    
    dateGA.addParam("sysparm_name", "getDate"); //name of function in script include    
    dateGA.addParam("sysparm_start", start_date); //send start value to script    
    dateGA.addParam("sysparm_end", newValue);
    dateGA.getXMLAnswer(checkDate); //callback function    
}


function checkDate(response) {

    var answer = response; //the response from the script  
    if (answer == 'false') { //if the date received is more than 10 days from the start date    
        g_form.setValue('end_date', ''); //remove value from end date field    
        g_form.showFieldMsg('end_date', 'Date cannot be greater than 10 business days from the Start Date')
        return false;
    }
}

 

 

Script Include: (Make sure the checkbox 'Client callable' is checked). The name of the Script Include should be: calcDate.

 

var calcDate = Class.create();

calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDate: function() {
        var startDT = new GlideDateTime(this.getParameter('sysparm_start'));
        var endDT = new GlideDateTime(this.getParameter('sysparm_end'));

        //This is hard-code to 10 here but could and should be set as a system property so it can be adjusted accordingly
        var days = 10;
        //Please note the 3rd parameter here of 9. 9 is the number of hours in the scheduled of 8-5. Adjust this per your schedule

        var duration = new GlideDuration(60 * 60 * 9 * 1000 * days);
        var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //pass the sys_id of your schedule and second parameter can be time zone which is optional - This is the sys_id of the OOB 8-5 weekdays excluding holidays

        var allowedEndDate = schedule.add(startDT, duration);

        if (endDT.before(allowedEndDate)) {
            return true;
        } else {
            return false;
        }
    },

    type: 'calcDate'
});

 

 

View solution in original post

15 REPLIES 15

Robbie
Kilo Patron
Kilo Patron

Hi @Servicenow de11,

 

Use the following tried and tested onChange Catalog Client Script and Script Include below. 

Note - Make sure the Script Include has the checkbox 'Client callable' checked (set to true)

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

 

onChange Catalog Client Script: (Make sure you have this set for onChange of the 'End Date' field)

 

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

    if (isLoading || newValue == '') {
	    return;
    }


    var start_date = g_form.getValue('start_date');
    var dateGA = new GlideAjax("calcDate"); //name of script include    
    dateGA.addParam("sysparm_name", "getDate"); //name of function in script include    
    dateGA.addParam("sysparm_start", start_date); //send start value to script    
    dateGA.addParam("sysparm_end", newValue);
    dateGA.getXMLAnswer(checkDate); //callback function    
}


function checkDate(response) {

    var answer = response; //the response from the script  
    if (answer > 10) { //if the date received is more than 10 days from the start date    
        g_form.setValue('end_date', ''); //remove value from end date field    
		g_form.showFieldMsg('end_date', 'Date cannot be greater than 10 days from the Start Date')
        return false;
    }
}

 

 

Script Include: (Make sure the checkbox 'Client callable' is checked). The name of the Script Include should be: calcDate.

 

var calcDate = Class.create();      

      calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{      
          getDate : function() {      
              var startDT = new GlideDate();
              startDT.setDisplayValue(this.getParameter('sysparm_start'));
              var endDT = new GlideDate();
              endDT.setDisplayValue(this.getParameter('sysparm_end'));
              var duration = new GlideDuration();
              duration= GlideDate.subtract(startDT, endDT);
              return duration.getDayPart();  
          },      

          type: 'calcDate'      
      });  

 

 

Hello Robbie,

 

The given script is working only for 10 days but i need 10 business days script

 

Thanks

Hi @Servicenow de11,

 

Ok - Let me update the script provided accordingly for you. (That wasn't made clear in the original ask)

Give me a few mins. 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

Thankyou