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-26-2023 08:02 AM
Hi @EH Desev
There's no addDays/Weeks/Months/Years in the object GlideDate.
Just changes to GlideDateTime, it should do the trick.
From
var day = GlideDate();
day.setValue(firstDT);
To
var day = new GlideDateTime(firstDT);
Let me know if it works for you.
Cheers,
Tai Vu