Issue with Catalog Item Client Script – Inconsistent Date Difference Validation

SakshyaR
Tera Contributor

Dear Community,

I am reaching out regarding a catalog item that includes a client-side script used to validate the difference between two date fields. The script is intended to ensure that the difference between these dates does not exceed 30 days.

However, we are currently facing an inconsistent behavior. At times, the validation works correctly, while at other times, it does not trigger or allows date differences greater than 30 days without any error message. This issue occurs randomly, making it difficult to identify a clear pattern.

We have checked the script logic, and it appears to be correct.

Below is the client script that is on change

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   
    if(g_form.getValue('date_that_the_server') !='')
        {
    g_form.hideFieldMsg('back_to_sleep_status');
   
    var gaDate = new GlideAjax('ANCatalogItemServiceAjax');
    gaDate.addParam('sysparm_name','dateValidationMothBalledSleeperService');
    gaDate.addParam('sysparm_date', newValue);
    gaDate.addParam('sysparm_compare_date', g_form.getValue('date_that_the_server'));
    gaDate.addParam('sysparm_days_to_add', 30);
    gaDate.getXMLAnswer(dateValidation);
}
function dateValidation(answer) {
    if(answer !='validdate'){
        g_form.clearValue('back_to_sleep_status');
        g_form.showFieldMsg('back_to_sleep_status',answer);
    }
}
}
 
Any insights or recommendations would be greatly appreciated.
11 REPLIES 11

SakshyaR
Tera Contributor

Hi @Muhammad Salar @Brad Bowman  please find below script include

 

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

    /**
     * @description validate if date is not before today not before restart date and not after restart date +30 days.
     * @param  {string} sysparm_days_to_add amount of days to add (30).
     * @param  {date} sysparm_date is the value of the sleep date.
     * @param  {date} sysparm_compare_date is the value of the restart date.
     * @return {string} returns the message based upon the validations.
     */

    dateValidationMothBalledSleeperService: function() {

        var answer;
        var message = 'validdate';
        var daysToAdd = this.getParameter('sysparm_days_to_add');
        var date = this.getParameter('sysparm_date');
        var compareDate = this.getParameter('sysparm_compare_date');

        answer = new PL4DateTimeUtils().isBeforeToday(date);
        if (answer == true) {
            //message = 'You cannot select a date in the past.';
            return gs.getMessage('You cannot select a date in the past.');
        } else {
            answer = new PL4DateTimeUtils().isAfterDate(date, compareDate);
            if (answer == false) {
                //message = 'You cannot select sleep date prior to the restart date.';
                return gs.getMessage('You cannot select sleep date prior to the restart date.');
            } else {
                var addedDate = new PL4DateTimeUtils().addDays(compareDate, daysToAdd);
                answer = new PL4DateTimeUtils().isAfterDate(date, addedDate);
                if (answer == true) {
                    //message = 'You cannot select a date more than 30 days after the restart date.';
                    return gs.getMessage('You cannot select a date more than 30 days after the restart date.');
                }
            }
        }

        return message;

    }

 

@SakshyaR 

you are using other script include in your main script include.

Did you try the approach shared by @J Siva using UI policy and no scripting will be required?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  below is the script include used in above script include

 

var PL4DateTimeUtils = Class.create();
PL4DateTimeUtils.prototype = {
initialize: function() {
},



/**
* @description add days to a date field.
* @param {string} date to add days to.
* @param {string} daysToAdd is the amount of days to add.
* @param {SysId} scheduleSysId is sys_id of the schedule.
* @return {date} new date after adding days.
*/
addDays: function(date, daysToAdd,scheduleSysId) {

date = new GlideDateTime(date);
if(gs.nil(scheduleSysId)){
date.addDaysLocalTime(daysToAdd);
}
else{
var schedule = new GlideSchedule(scheduleSysId);
date = schedule.add(date, daysToAdd);
}

return date;
},

/**
* @description validate if date to compare is before the selected date.
* @param {string} date is the date.
* @param {string} compareDate is the date to be compared to selected date field.
* @return {boolean} return true or false.
*/
isAfterDate: function(date, compareDate) {

var dateGdt = new GlideDateTime();
dateGdt.setDisplayValue(date);

var compareDateGdt = new GlideDateTime();
compareDateGdt.setDisplayValue(compareDate);

return compareDateGdt.before(dateGdt);

},



/**
* @description check if date is not before current date.
* @param {string} date is the date to compare to maximumdate.
* @return {boolean} return true or false.
*/
isBeforeToday: function(date){
var compareDate = date;
var currentDate = new GlideDateTime();
var answer = this.isAfterDate(currentDate,compareDate);

return answer;
},

type: 'PL4DateTimeUtils'
};

@SakshyaR 

any reason you are not using UI policy?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

We need to display the error message below the variable. This configuration is already in use and is working as expected; however, we have been facing issues for the past few days. Specifically, when we select June 2nd, 3rd, 4th, and 5th, as well as the same dates for the following month, we encounter problems. For other dates, the system is functioning as expected.