Comparing 2 Dates: Client Script + Script Include

mark141230
Tera Expert

Good Day Everyone!

Need your expertise on my requirement. I have 2 date/time field in a record producer named "stored date" and "new wallet expiry" date. I read that you need to create a client script using glideajax and script include to run this on server side. I created the script but seems not working at all.

 

Client Script: ValidateNewExpiryDate

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

//g_form.hideFieldMsg('new_expiry_date'); // give your variable name here
	
	var newexpdate = g_form.getValue('new_expiry_date');
	var storeddate = g_form.getValue('stored_date');

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', 'chkCatEndDate');
	ga.addParam('sysparm_newexpdate',newexpdate);
	ga.addParam('sysparm_storeddate', storeddate);
	ga.getXML(SvcCatalogCheckEndDateParse);
	
	function SvcCatalogCheckEndDateParse(response){
    
		var answer = response.responseXML.documentElement.getAttribute('answer');
        
		if(answer == true){
			
			alert('New Expiry Date should not be earlier than Stored Date');
            //g_form.setValue('new_expiry_date', '');

}
  }
    }

 

Script Include: checkRecords

var checkRecords = Class.create();checkRecords.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	
	
	
	chkCatEndDate: function() {

	var start = new GlideDateTime(this.getParameter('sysparm_storeddate')); //Passing the start date from the client
	var end = new GlideDateTime(this.getParameter('sysparm_newexpdate')); //Passing the end date from the client

	var dif = GlideDateTime.subtract(start.getNumericValue(), end.getNumericValue());//Get the Different between dates.

    gs.debug("Hello DIF"+ dif);
	if (dif <= 0){


      return false;
	}
	else
		{

      return true;
}

	},
    type: 'checkRecords'
});

 

Not sure why it is not working. By the way this is a scoped application.

13 REPLIES 13

I mean the article you provided is good for comparing 1 field to a certain date and time but not good in comparing 2 fields.

Hi Pagar,

According to wiki, gs.datediff will not work on scoped application the reason why I used GlideDateTime.subtract.

Hi,

Try this updated script. make sure to add gs.info() or gs.debug() logs.


var start = new GlideDateTime(this.getParameter('sysparm_storeddate')); //Passing the start date from the client
var end = new GlideDateTime(this.getParameter('sysparm_newexpdate')); //Passing the end date from the client

var dif = GlideDateTime.subtract(start, end);//Get the Different between dates.

var duration_numeric = dif.getNumericValue();
var durationSeconds = (duration_numeric/1000);

gs.debug("Hello DIF"+ durationSeconds);
if (durationSeconds <= 0){

    return false;
}
else
    {
    return true;
}

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Jack
Tera Guru

Hi,

You can refer the OOB UI Script for validation Change (Start/End).

/sys_ui_script.do?sys_id=42518fd3672222004792adab9485ef76

Hi Jack,

How to use this? 

 

Thanks