i want to validate date is past or not if past want to display error msg using g_form.showfieldmessage

gomathysanjana
Mega Expert

i   want to validate date is past or not if past want to display error msg using g_form.showfieldmessage

1 ACCEPTED SOLUTION

thameem1
Kilo Expert

Hi


You can check using Client Script On Change calling a script include where u can check the date with current date


Example Code:


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


              if (isLoading || newValue === '') {


                    return;


              }


        var ga = new GlideAjax('DateandTIme');


            ga.addParam('sysparm_name', 'getcurrentdate');


            ga.addParam('sysparm_Publised', newValue);


            ga.getXML(UpdatePublished);


     


        }


function UpdatePublished(response, newValue){


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


            if(answer){


          // g_form_show field msg


            g_form.clearValue('published');


            return;


            }


}



Script include :



var DateandTIme = Class.create();


DateandTIme.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getcurrentdate : function(){


  var publisheddate = this.getParameter('sysparm_Publised');


if(publisheddate < gs.nowDateTime()){


  return true;


  }


  else


  return false;


  },


      type: 'DateandTIme'


});


View solution in original post

9 REPLIES 9

gomathysanjana
Mega Expert

Actually im getting some error like on script error


thameem1
Kilo Expert

Hi


You can check using Client Script On Change calling a script include where u can check the date with current date


Example Code:


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


              if (isLoading || newValue === '') {


                    return;


              }


        var ga = new GlideAjax('DateandTIme');


            ga.addParam('sysparm_name', 'getcurrentdate');


            ga.addParam('sysparm_Publised', newValue);


            ga.getXML(UpdatePublished);


     


        }


function UpdatePublished(response, newValue){


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


            if(answer){


          // g_form_show field msg


            g_form.clearValue('published');


            return;


            }


}



Script include :



var DateandTIme = Class.create();


DateandTIme.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getcurrentdate : function(){


  var publisheddate = this.getParameter('sysparm_Publised');


if(publisheddate < gs.nowDateTime()){


  return true;


  }


  else


  return false;


  },


      type: 'DateandTIme'


});


Thank U its working


Dave Smith1
ServiceNow Employee
ServiceNow Employee

You could use a UI Policy for this - there's a "on or before today" condition that can be placed against date fields, rather than script it.


gomathysanjana
Mega Expert

Thank you David