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

Nate23
Mega Guru

Hello,



Have you tried looking at this thread? Client Script Date/Time Functions



I am pretty sure you cannot set variables from within the script include because that is client side code and the script include is server side. However, you can do all the logic in the server side and return an array. You can structure the array to return [true/false(wether it met the condition), what to set the start date, what to set the end date ] and in your client script when you receive the array as the answer you can use it as you wish.



Let me know if I can be of any further assistance.


oharel
Kilo Sage

Hi Alexander.


This one is very similar (only for 10 days): What's the best way to place a time constraint on a catalog item?



And so, the client script:


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 allowed_days = 7;//maximum days to add  


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


 


  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_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  


 


  if (answer < 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.setValue('end_date', ''); //remove value from end date field  


  return false;  


  }  


}



and script include:


var calcDate = Class.create();  


calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{  


 


  getDate : function() {  


 


  var firstDT = this.getParameter('sysparm_start'); //Start Date Field  


  var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day  


  var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  


  var day = GlideDate();  


  day.setValue(firstDT);  


 


  if(addTYPE == 'day') {  


  day.addDays(addTIME);  


  }  


  return day;  


  },  


  type: 'calcDate'  


});


*based off of MB article Client Script Date/Time Functions



harel


Please mark as correct or helpful based on impact


Wow, thanks, that should be exactly what we need.



I just updated the client script and script include but nothing is happening. Did a quick check for typos but I don't see anything.



The catalog item variables are definitely 'start_date' and 'end_date' and the field types are 'date', not 'date/time'. I don't think that makes a difference but I figured I'd mention it.


Make sure that the onChange should be on the end_date field.


harel