'Complete by Date' Validation in Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2024 05:43 AM - edited ‎07-01-2024 11:10 PM
Hi Community,
I'm currently working on a catalog item that includes a 'Complete by Date' field. I've implemented a validation script to ensure the selected date and time are not set to today, any past date, weekends, or Indian holidays. However, some users are still able to select today as the 'Complete by Date' when they are creating a request.
Here is my catalog client script and script include. Any guidance on resolving this issue would be greatly appreciated!
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Show error if selected date/time is not in schedule.
var ga = new GlideAjax("HARM_dateValidationUtilsAJAX");
ga.addParam("sysparm_name", "validateChoosenDate");
ga.addParam("sysparm_date", newValue);
var response = ga.getXMLAnswer(parseResponse);
function parseResponse(answer) {
if(answer == 'false') {
g_form.clearValue('complete_by_date');
g_form.showFieldMsg('complete_by_date','The selected date ' + newValue + ' is either on a weekend, an indian holiday, today or before today. Please choose a valid date to be able to submit your request.','error',true);
}
}
}
Script Include:
validateChoosenDate: function(){
var selectedDate = this.getParameter("sysparm_date");
var today = new GlideDateTime();
var gdt = new GlideDateTime();
gdt.setDisplayValue(selectedDate);
//Returns false if the selected date is equal to today or before today.
//if(gdt.getDate() <= today.getDate())
if (gdt.onOrBefore(today)) {
return false;
} else {
//returns true if the selected date is within working hours and not weekend or indian holiday, returns false otherwise
var scheduleID = gs.getProperty('harm.sys_id_of_24x7_excluding_indian_holidays_and_weekends_schedule');
var schedule = new GlideSchedule(scheduleID);
return schedule.isInSchedule(gdt);
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2024 09:10 PM
Hi @Golu_thrr
Catalog Client Script:
var ga = new GlideAjax('HARM_dateValidationUtilsAJAX');
ga.addParam('sysparm_name', 'validateChoosenDate');
ga.addParam('sysparm_date', newValue);
ga.getXMLAnswer(function(response) {
if (response === 'false') {
g_form.clearValue('complete_by_date');
g_form.showFieldMsg('complete_by_date', 'The selected date ' + newValue + ' is either on a weekend, an Indian holiday, today or before today. Please choose a valid date to be able to submit your request.', 'error', true);
}
});
Script Include:
var HARM_dateValidationUtilsAJAX = Class.create();
HARM_dateValidationUtilsAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateChoosenDate: function() {
var selectedDate = this.getParameter('sysparm_date');
// Remove the time part for comparison, ensuring only the date part is used
selectedDate = selectedDate.split(' ')[0];
var today = new GlideDate().getDisplayValue();
// Get today’s date only for comparison
var gdt = new GlideDateTime();
gdt.setDisplayValue(selectedDate + " 00:00:00");
// Returns false if the selected date is equal to today or before today.
if (new GlideDate(selectedDate) <= new GlideDate(today)) {
return 'false';
}
// Ensure avoiding weekends and holidays
var scheduleID = gs.getProperty('harm.sys_id_of_24x7_excluding_indian_holidays_and_weekends_schedule');
var schedule = new GlideSchedule(scheduleID);
if (schedule.isInSchedule(gdt)) {
return 'true';
}
return 'false';
},
type: 'HARM_dateValidationUtilsAJAX'
});
Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma