- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2022 10:14 PM
Can anyone please help on the below requirement. I have date field variable named assignment date. Another date field variable is expiration date.
Users should be able to select the expiration date between today's date to assignment date + 6months.They should not be able to select past dates or more than 6 months.
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2022 10:29 PM
Hi,
you can use onChange client script + GlideAjax to validate that
Something like this
Client Script:
Note: Ensure you give valid variable names for start and end dates
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// check if in past
var today = new Date().getTime();
var selectedDate = new Date(newValue).getTime();
if(selectedDate < today){
alert('Past date not allowed');
g_form.clearValue('end_date');
}
// check for 6 months
else if(oldValue != newValue){
var ga = new GlideAjax('DateTimeAjax');
ga.addParam('sysparm_name', "compareDates");
ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
ga.getXMLAnswer(function(answer){
if(answer != ''){
alert('End date should be within 6 months from start date');
g_form.clearValue('end_date');
}
});
//Type appropriate comment here, and begin script below
}
}
Script Include: It should be client callable
var DateTimeAjax = Class.create();
DateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDates: function(){
var start = new GlideDateTime(this.getParameter('sysparm_start'));
var end = new GlideDateTime(this.getParameter('sysparm_end'));
start.addMonthsUTC(6);
if(end.getNumericValue() > start.getNumericValue()){
return 'End Date should be withing 6 months from start';
}
return '';
},
type: 'DateTimeAjax'
});
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
‎04-18-2022 10:29 PM
Hi,
you can use onChange client script + GlideAjax to validate that
Something like this
Client Script:
Note: Ensure you give valid variable names for start and end dates
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// check if in past
var today = new Date().getTime();
var selectedDate = new Date(newValue).getTime();
if(selectedDate < today){
alert('Past date not allowed');
g_form.clearValue('end_date');
}
// check for 6 months
else if(oldValue != newValue){
var ga = new GlideAjax('DateTimeAjax');
ga.addParam('sysparm_name', "compareDates");
ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
ga.getXMLAnswer(function(answer){
if(answer != ''){
alert('End date should be within 6 months from start date');
g_form.clearValue('end_date');
}
});
//Type appropriate comment here, and begin script below
}
}
Script Include: It should be client callable
var DateTimeAjax = Class.create();
DateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDates: function(){
var start = new GlideDateTime(this.getParameter('sysparm_start'));
var end = new GlideDateTime(this.getParameter('sysparm_end'));
start.addMonthsUTC(6);
if(end.getNumericValue() > start.getNumericValue()){
return 'End Date should be withing 6 months from start';
}
return '';
},
type: 'DateTimeAjax'
});
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
‎04-18-2022 11:03 PM
Glad to know that my script worked.
Please mark response helpful as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader