Date Validation + Record Producer + Client Script + Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 12:51 AM
Hi Everyone,
I have a requirement related to Date Validation, where I have a Date Field name (New Expiry Date) which should not be less than the valid until date of a project.
So far I have this record producer where I have a "project_name" reference field and if you dot walk to this field, you will get the "valid until" date field which I need to compare if the "new expiry date" date field in my record producer is less than the "valid until" date.
As researched, you need to use GlideAJAX, client script and script include to it so you will be able to run server side script with your client script.
Acceptance Criteria:
New Expiry Date !< project_name.valid_until
Hope you can help me.
Thanks in Advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2022 02:03 AM
Hi,
so what did you start with and where are you stuck?
Should be simple enough
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
‎08-29-2022 02:09 AM
Hi,
Script Include: It should be client callable
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkRecordPresent: function(){
var id = this.getParameter('sysparm_userID');
var userGivenDate = new GlideDateTim(this.getParameter('sysparm_date'));
var gr = new GlideRecord('pm_project'); // use correct table name
gr.addQuery('sys_id', id);
if(gr.next()){
if(userGivenDate.getNumericValue() > new GlideDateTime(gr.valid_until).getNumericValue()) // use correct field name here
return 'valid';
else
return 'invalid';
}
},
type: 'checkRecords'
});
Client Script: onChange on New Expiry Date
UI Type - ALL
Applies to Catalog Item - True
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('variableName'); // give your variable name here
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_date', newValue);
ga.addParam('sysparm_project', g_form.getValue('project_name'));
ga.getXMLAnswer(function(answer){
if(answer == 'invalid'){
var message = 'The new date should not be less than the valid until date of a project.';
g_form.showFieldMsg('variableName',message,'error', true); // give your variable name here
}
});
//Type appropriate comment here, and begin script below
}
You might require something similar onChange on the project_name variable if user changes this variable as well; accordingly enhance the script include and 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
‎08-29-2022 07:43 AM
Did you get a chance to check on above scripts I shared?
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
‎08-31-2022 10:06 AM
Hi Ankur,
First of all thank you for showing interest in my question, this helps me a lot in terms of understanding how GlideAjax works even though the requirement has changed but still this is very helpful and everyone can use this on their requirement.