- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-06-2024 03:19 AM - edited 03-06-2024 03:20 AM
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.
I have tried the below script but it is not working. Can anyone help to achieve this.
Script Include:
Solved! Go to Solution.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-07-2024 02:27 AM
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'
});
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-06-2024 05:29 AM
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'      
      });  
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-06-2024 06:36 AM
Hello Robbie,
The given script is working only for 10 days but i need 10 business days script
Thanks
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-06-2024 06:51 AM
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
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
03-06-2024 06:52 AM
Thankyou
