0

Client Script :

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
  return;
 }
  var gaDate = new GlideAjax('CalculateDateTime');
 gaDate.addParam('sysparm_name', "CalculateDate");
 gaDate.addParam('sysparm_start', g_form.getValue('get_start_date')); // start variable
 gaDate.addParam('sysparm_end', g_form.getValue('get_end_date')); // end variable
 gaDate.getXMLAnswer(function(answer){
  if(answer != ''){
                        alert('End date should be within 6 months from start date');
   g_form.clearValue('get_end_date');	
  }
 });
 //Type appropriate comment here, and begin script below
 }

Script Include:

var CalculateDateTime = Class.create();
CalculateDateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  CalculateDate: function(){
   var start = new GlideDateTime(this.getParameter('sysparm_start'));
  var end = new GlideDateTime(this.getParameter('sysparm_end'));
  start.addMonthsUTC(6);
   if(end.getNumericValue() > start.getNumericValue()){
   return 'End Date should be withing 6 months from start';
  }
  return '';
 },
  type: 'CalculateDateTime'
});
End date should not be more than 6 months from start date ServiceNow
Expert Asked question July 17, 2023