Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Prevent user from entering an End Date that is 7 days greater than Start Date

galavodasal
Giga Expert

We have a request to configure date fields on a catalog item where users cannot enter an End Date that seven days greater than the Start Date.

We had to configure a date validation client script recently, so we've used that as a base but are having issues setting the Start Date and End Date within the script include.

Script include:

var CheckValidToOSS = Class.create();  

CheckValidToOSS.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

  validateDate: function(){  

  var gdt = new GlideDateTime();  

  gdt2.setDisplayValue(this.getParameter('start_date'));  

  gdt.addDaysLocalTime(7);  

  var gdt2 = new GlideDateTime();  

  gdt2.setDisplayValue(this.getParameter('end_date'));  

  if(gdt2.getDate()>=gdt.getDate()){  

  return true;  

  }  

  else{  

  return false;  

  }  

  },  

  type: 'CheckValidToOSS'  

});  

Can we set the parameters to catalog item variables this way, or is there another way to do it?

Catalog client script:

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

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

  return;  

  }  

  var ga = new GlideAjax('CheckValidToOSS');  

  ga.addParam('start_date',g_form.getValue('start_date'));  

  ga.addParam('end_date',g_form.getValue('end_date'));  

  ga.getXML(callBack);  

  function callBack(response){  

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

  if(answer=="false"){  

  alert("End Date cannot be greater than 7 days from the Start Date");  

  g_form.setValue('end_date','');  

  }  

  }  

}  

Thanks in advance

1 ACCEPTED SOLUTION

lavanya27
Kilo Expert

Hi Alexander,



try this as well, this code will work irrespective of the date format



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


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


                  return;    


            }    


           


            //Type appropriate comment here, and begin script below    


          var start_date = g_form.getValue('start_date');    


          var ga = new GlideAjax("calcDate"); //name of script include    


          ga.addParam("sysparm_name", "getDate"); //name of function in script include    


          ga.addParam("sysparm_start", start_date); //send start value to script    


          ga.addParam("sysparm_end", newValue);


          ga.getXML(checkDate); //callback function    


      }    


           


      function checkDate(response) {          


          var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script    


           


          if (answer > 7) { //if the date received is more than 7 days from the start date    


          alert("End date cannot be later than 7 days after start date.");    


          g_form.setValue('end_date', ''); //remove value from end date field    


          return false;    


          }    


      }



script include code-



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'      


      });  



Regards,


Lavanya


View solution in original post

26 REPLIES 26

I think it is because your date is in the format of mm/dd/yyyy and not yyyy-mm-dd


Is it possible to change the date format?


We could give that a shot.



Wouldn't other date based script includes also throw errors though? Thanks again for your time and help.


To be honest, I don't know. I am not sure it is the reason you are getting the error...


Give it a shot and we'll see.


Changing the time format to the default yyyy-MM-dd did the trick, but we'd prefer to keep the date format as we had it, MM-dd-yyyy. I may make another post tomorrow in case this one gets buried.



Thanks again Harel.


OK, so try this - it worked for me in a personal dev istanbul instance, where I set the date format to match yours:


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


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


          return;


  }



  //Type appropriate comment here, and begin script below



  var start_date = g_form.getValue('start_date');


  var end_date = g_form.getValue('end_date');


  var allowed_days = 7;//maximum days to add


  var addtype = 'day'; //The time type.



  //check if user put a date before the start date


  if(start_date > end_date) {


  alert('You cannot choose and end date that comes before the start date. Please change either start or end dates');


  g_form.clearValue('end_date');


  }



  //check maximum 7 days


  var astrt = start_date.toString().split("-"); //this takes the date and splits it in the - sign,


  var strt = astrt[2] +'-'+ astrt[0] +'-'+ astrt[1]; //it then rearranges the order of the split parts,


//like so: the original date format is mm-dd-yyyy, or 0-1-2 . Now we rearrange them to match yyyy-mm-dd, so they are now in the above order 2-0-1



  var ga = new GlideAjax("calcDate"); //name of script include


  ga.addParam("sysparm_name", "getDate"); //name of function in script include


  ga.addParam("sysparm_start", strt); //send start value to script


  ga.addParam('sysparm_addtime', allowed_days);   //send days to add value to script


  ga.addParam('sysparm_addtype', addtype);   //send the typf of time to the script, in this case - days


  ga.getXML(checkDate); //callback function


}



function checkDate(response) {


  var end_date = g_form.getValue('end_date');


  var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script



//we do with the answer the same thing that we did to the start_date - rearrange the answer.


//Again, splitting it and reordering, now from yyyy-mm-dd to mm-dd-yyyy.



  var astrt1 = answer.toString().split("-");


  var strtb = astrt1[1] +'-'+ astrt1[2] +'-'+ astrt1[0];



  if (strtb < end_date) { //if the date received is more than 7 days from the start date


  alert("End date cannot be later than 7 days after start date.");


  g_form.clearValue('end_date'); //remove value from end date field


  return false;


  }


}


harel


Please mark as correct or helpful based on impact


EDIT: added the first line which was missing from the script