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

Sagar Pagar
Tera Patron

Hi,

Cliend script look good to me.

 

Just update you Script incliude function as -

   
chkCatEndDate: function() {
/* Returns: if boolean bnumericValue is true, the difference in number of seconds;
* if false, the difference in the format ddd hh:mm:ss 
*/

return gs.dateDiff(this.getParameter('sysparm_storeddate'), this.getParameter('sysparm_newexpdate'), true);  


}

Feel free to mark helpful & correct, if applicable.

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi,

Also as a best practice, I would suggest to use the Catalog UI policy instead of Catalog client scripts.

 

Take a look at article written by Mark -

No Code date validations through (Catalog) UI Policies

 

Thanks,

Sagar Pagar

The world works with ServiceNow

I already checked this article also but the example is good when you are comparing 1 date only.

Can you explain comparing only 1 date field?

Here you are comparing two date fields/variable here. Expiry date & Store date.

 

Thanks,
Sagar Pagar

The world works with ServiceNow