Script Include Date Validation

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-28-2023 03:19 PM
Hi Developers,
I have a script include to validate dates (start date and end date) on a catalog item. The end date should be bigger than (or the date after) the start date, however, if my end date is on month after my start date (eg 13-06-2023) but the date is smaller than the date of my start date (eg 29-05-2023). The script deems the end date smaller than my start date. Please check the scripts and the attachments below and help me fix the problem.
Script Include:
var EndDate = Class.create();
EndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
endDate: function(){
//GETTING DATES FROM THE UI
var endDate = this.getParameter('sysparm_end_date');
var startDate = this.getParameter('sysparm_start_date');
//GETTING TODAYS DATE
var _gdt = new GlideDateTime();
var gdt = _gdt.getByFormat('dd-MM-yyyy');
if (endDate == startDate){
return 'Delegate should be created at least for 1 day';
}else if (endDate < startDate){
return 'End date should be after the start date selected';
}
gdt.addDaysUTC(30); //Highest possible date
if(endDate < gdt){
return 'Delegate can be created for 30 days at max';
}
return 'valid';
},
type: 'EndDate'
});
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var gaStartDate = new GlideAjax('StartDate');
gaStartDate.addParam('sysparm_name', 'startDate');
gaStartDate.addParam('sysparm_start_date', g_form.getValue('starts'));
gaStartDate.getXML(startDateParse);
function startDateParse(response){
var ans = response.responseXML.documentElement.getAttribute('answer');
if (ans != 'valid'){
g_form.addErrorMessage(ans);
g_form.clearValue('starts');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-28-2023 08:58 PM
so if start date is 29th May then end date should be after it
I didn't get the extra validation
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-28-2023 11:58 PM
@Ankur Bawiskar it should be any date after 29 of May
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-29-2023 12:17 AM
simply use this in onChange client script of end date
something like this and enhance it furter
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(g_form.getValue('starts') != '' && newValue){
var start = new Date(g_form.getValue('starts')).getTime();
var end = new Date(newValue).getTime();
if(end < start){
var message = 'Please give valid start and end dates';
g_form.showErrorBox('ends', message);
}
}
}
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-28-2023 10:38 PM
Hello @Kamva ,
In the Catalog Client Script, I don't see any parameter for end date. GlideAjax has only Start date parameter passed.