Comparing 2 Dates: Client Script + Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 07:46 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 08:00 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 08:03 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 08:05 PM
I already checked this article also but the example is good when you are comparing 1 date only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 08:13 PM
Can you explain comparing only 1 date field?
Here you are comparing two date fields/variable here. Expiry date & Store date.
Thanks,
Sagar Pagar