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 09:40 PM
I mean you can refer this logic code to reuse for your validation, this is only client side without ajax call.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 10:01 PM
this can be achieved using UI policy and no GlideAjax required
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 10:14 PM
you can achieve this without GlideAjax as well and only client script
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-05-2022 10:09 PM
Follow this
https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3