Restrict user to select date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 08:27 PM
Hi All,
How to restrict a user to select a date for a catalog variable which is less than 3 days from today and does not falls on weekends.
I tried to do that by creating a date type field (o_expected_delivery_date) and catalog client script and script include but this is not working. Please help me. PFB the scripts I have tried and help me to rectify this.
onChange Catalog Client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.getXMLWait();
var end_date = da.getAnswer();
alert ('abd'+ end_date);
if(g_form.getValue('o_expected_delivery_date') < end_date)
{
alert('Date should be After 3 days from current date time');
g_form.clearValue('o_expected_delivery_date');
return;
}
}
Script Include
ar ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDefaultEndDate: function() {
var startDate = gs.nowDateTime();
var addQuantity = this.getParameter('sysparm_quant');
var end_date = new GlideDateTime();
end_date.setDisplayValue(startDate);
end_date.addSeconds(259200);// Equivalent to 3 days
end_date = end_date.getDisplayValue();
return end_date;
},
});
onLoad client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.getXMLWait();
var end_date = da.getAnswer();
alert ('abd'+ end_date);
if(g_form.getValue('o_expected_delivery_date') < end_date)
{
alert('Date should be After 3 days from current date time');
g_form.clearValue('o_expected_delivery_date');
return;
}
}
Regards,
Evan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 11:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2022 11:56 PM
Hi,
Earlier you mentioned that it worked fine.
Did you check what came in script include logs?
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
01-04-2022 01:46 AM
Hi Ankur,
Sorry for that.
Yes I have checked and found that It is due to difference in formatting of date.
As in our system we are using DD-MMM-YY (06-Jan-22) and you are validating OOB format DD-MM-YYYY(06-01-2022).
So can you please help me to change the format first and do the necessary action post that.
Regards,
Evan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2022 01:54 AM
Hi,
what is your system date format?
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
01-04-2022 02:00 AM
Hi,
try this
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDefaultEndDate: function() {
var selectedDate = this.getParameter('sysparm_date') + ' 00:00:00';
var loggedInUserFormat = this.getParameter('sysparm_format');
var end_date = new GlideDateTime();
end_date.setDisplayValue(selectedDate, loggedInUserFormat);
var day = end_date.getDayOfWeekLocalTime();
if(day == 6 || day == 7){
return 'cannot select weekend';
}
else{
var nowTime = new GlideDateTime();
nowTime.addDaysUTC(3);
if(end_date.getNumericValue() < nowTime.getNumericValue()){
return 'Date should be After 3 days from current date time';
}
return '';
}
},
type: 'ClientDateTimeUtils'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(oldValue != newValue){
var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.addParam('sysparm_date', newValue);
da.addParam('sysparm_format', g_user_date_format); // send user format
da.getXMLAnswer(function(answer){
if(answer != '')
{
alert(answer);
g_form.clearValue('o_expected_delivery_date');
}
});
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader