Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Date Field Validation-Journey date cannot be before start date & cannot be after end date

rishabh31
Mega Sage

Hi Community Team,

I am new in scripting part of ServiceNow platform & stuck with the requirement -  Journey date field should be in between Start date & End date (i.e., Journey date cannot be before start date & cannot be after end date).

I tried to achieve this by Script Include (client callable) called on Client Script Include, I achieved its associated requirements of Start Date cannot be the past to Current/Now date followed by End Date cannot be the past to Start date, but for Journey Date field requirement, its not working properly like when I select a date in between of start Date & end date, its still showing alert 'Journey date cannot be after the end date' & cleared the value (screenshot attached)

find_real_file.png

While, yes its working when I select any journey date past to start date & also working for any journey date later to end date, but its not working when I select any journey date in between start date & end date field, ideally that should be taken.

Script Include-

var setDateFields = Class.create();
setDateFields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    nowDateTime: function() {
        var startDate = this.getParameter('sysparm_u_start_date');
        //gs.log('start date time' + startDate);
        return gs.dateDiff(gs.nowDateTime(), startDate, true) / 86400;
        //gs.log("now date time" + gs.dateDiff(gs.nowDateTime(), startDate, true)/ 86400) ;
    },

    compareenddatenowdate: function() {
        var endDate = this.getParameter('sysparm_u_end_date');
        return gs.dateDiff(gs.nowDateTime(), endDate, true) / 86400;
    },

    compareedatsdate: function() { 
        var startDate = this.getParameter('sysparm_u_start_date');
        var endDate = this.getParameter('sysparm_u_end_date');
        return gs.dateDiff(startDate, endDate, true) / 86400;
    },

    comparejdatenowdate: function() { 
         var journeyDate = this.getParameter('sysparm_u_journey_date');
         return gs.dateDiff(gs.nowDateTime(), journeyDate, true) / 86400;
     },

     comparejdatesdate: function() {
         var startDate = this.getParameter('sysparm_u_start_date');
         var journeyDate = this.getParameter('sysparm_u_journey_date');
         return gs.dateDiff(startDate, journeyDate, true) / 86400;
     },

     comparejdateenddate: function() {
         var journeyDate = this.getParameter('sysparm_u_journey_date');
         gs.log('journey date time ' + journeyDate);
         var endDate = this.getParameter('sysparm_u_end_date');
         return gs.dateDiff(journeyDate, endDate, true) / 86400;
     },
     type: 'setDateFields'
 });

--------------------------------------------------------------

1-Client Script (Compare Journey date with end date, applied OnChange Client Script on Field- Journey Date )

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }


        var ga3 = new GlideAjax('setDateFields');
        ga3.addParam('sysparm_name', 'comparejdateenddate');
        ga3.addParam('sysparm_u_journey_date', g_form.getValue('u_journey_date'));
        ga3.getXML(comparejourneydatewithenddate);

        function comparejourneydatewithenddate(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");

            if (answer < 0) {
                alert('Sorry! Journey Date cannot be after the End Date');
                g_form.clearValue('u_journey_date');
            } else {
                alert(" Valid Journey Date!");
            }

        }
}

2-Client Script (Compare Journey date with Now date, applied OnChange Client Script on Field- Journey Date)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var ga4 = new GlideAjax('setDateFields'); //script include name
    ga4.addParam('sysparm_name', 'comparejdatenowdate');
    ga4.addParam('sysparm_u_journey_date', g_form.getValue('u_journey_date'));
    ga4.getXML(comparejourneydatewithnowdate);

    function comparejourneydatewithnowdate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer < 0) {
            alert('Sorry! Journey Date cannot be before the Current Date');
            g_form.clearValue('u_journey_date');
        } else {
            alert("Correct Journey Date!");
        }

    } 
}

3-Client Script (Compare Journey date with Start date, applied OnChange Client Script on Field- Journey Date)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga2 = new GlideAjax('setDateFields');
    ga2.addParam('sysparm_name', 'comparejdatesdate');
    ga2.addParam('sysparm_u_journey_date', g_form.getValue('u_journey_date'));
    ga2.getXML(comparejourneydatewithstartdate);

    function comparejourneydatewithstartdate(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer <= 0) {
            alert('Sorry! Journey Date cannot be before the Start Date');
            g_form.clearValue('u_journey_date');
        } else {
            alert("Perfect Journey Date!");
        }

    }

}

---------------------------------------------------------------------------------

Please take your valuable time out & help me with this, I shall be truly grateful.

Thanks in advance.

1 ACCEPTED SOLUTION

Muhammad Khan
Mega Sage

Hi rishabh,

I have not gone into deep debugging but just found the following missing statements in 1st and 3rd client scripts respectively..

ga3.addParam('sysparm_u_end_date', g_form.getValue('<end_date_field_name>')); // 1 Client-Script

ga2.addParam('sysparm_u_start_date', g_form.getValue('<start_date_field_name>')); // 3 Client Script

 

Hopefully, this will do the trick for you.

View solution in original post

2 REPLIES 2

Muhammad Khan
Mega Sage

Hi rishabh,

I have not gone into deep debugging but just found the following missing statements in 1st and 3rd client scripts respectively..

ga3.addParam('sysparm_u_end_date', g_form.getValue('<end_date_field_name>')); // 1 Client-Script

ga2.addParam('sysparm_u_start_date', g_form.getValue('<start_date_field_name>')); // 3 Client Script

 

Hopefully, this will do the trick for you.

Thanks Mr. Muhammad, it works!