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

There's an error upon saving this new script : Could not save record because of a compile error: JavaScript parse error at line (2) column (13) problem = invalid return (<refname>; line 2)



Are you seeing the same thing?


If you copy-pasted it as is, you'd notice I missed the first line:


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



add it...


Let me know how it works for you.


harel


Whoops, sorry, still half asleep. Usually it throws a different error when its missing the function declaration



However it's throwing the 'End date cannot be later than 7 days after start date.' error regardless of the end date. I can choose 4/1 as the start date and anything after 4/1 will trigger the message.


Change line 40 from:


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


to


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



Let me know if that works for you.


harel


Another option is to use lavanya 's   scripts. They work as well



harel