Start date has to be at least 2 weeks in the future
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2023 05:39 AM
Hi guys,
I have a requirement to set catalog variable start date to be at least 2 weeks in the future e.g. if today is 01.01.2024 the first possible date which the user can choose should be 15.01.2024. How to achieve that through clinet script? I have found below clinet script and script include and tried to adapt it but I get null as an answer when testing it. How to fix this or my approach is totally wrong?
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt = g_form.getValue('start_date'); //Choose the field to add time from
var addtime = 14; //The amount of time to add
var addtype = 'day'; //The time type. Can be day, week, month, year.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(ClientDateTimeUtilsParse);
function ClientDateTimeUtilsParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
Script include
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setValue(firstDT);
if(addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}
//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},
});
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2023 10:03 AM
This is a check the system has to make sure the due_date is after today.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
g_form.hideFieldMsg('due_date');
return;
}
var dueDate = new Date(getDateFromFormat(g_form.getValue('due_date'), g_user_date_time_format));
var now = new Date();
if (dueDate.valueOf() < now.valueOf()) {
g_form.showFieldMsg('due_date', getMessage('Due date cannot be in the past.'), 'error');
return false;
}
g_form.hideFieldMsg('due_date');
return true;
}
You can use that and update it for your start date, then update the var "now" to be 2 weeks in the future and then it will make sure that they cannot submit until they pick a date after the one you specify.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 07:13 AM
Hi @EH Desev, use this script
Client Script OnChange
Field: Start Date Field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var start_date = new Date(getDateFromFormat(newValue, g_user_date_time_format)),
minimum_date = new Date(),
minimum_days = 14;
minimum_date.setDate(minimum_date.getDate() + minimum_days)
if(start_date < minimum_date){
g_form.addErrorMessage("The start date must be two weeks from today");
g_form.clearValue("start_date")
}
}
If my answer helped you, please mark my answer as helpful.
Vanderlei Catione Junior | LinkedIn
Senior ServicePortal Developer / TechLead at The Cloud People
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 01:16 AM
Hi when I try your code I always receive the error message even If I pick a date after 15 days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 07:17 AM
You can try a no code date validation like this
In the script part you can throw the error/alert etc.