- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2017 08:21 AM
We have a request to configure date fields on a catalog item where users cannot enter an End Date that seven days greater than the Start Date.
We had to configure a date validation client script recently, so we've used that as a base but are having issues setting the Start Date and End Date within the script include.
Script include:
var CheckValidToOSS = Class.create();
CheckValidToOSS.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function(){
var gdt = new GlideDateTime();
gdt2.setDisplayValue(this.getParameter('start_date'));
gdt.addDaysLocalTime(7);
var gdt2 = new GlideDateTime();
gdt2.setDisplayValue(this.getParameter('end_date'));
if(gdt2.getDate()>=gdt.getDate()){
return true;
}
else{
return false;
}
},
type: 'CheckValidToOSS'
});
Can we set the parameters to catalog item variables this way, or is there another way to do it?
Catalog client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('CheckValidToOSS');
ga.addParam('start_date',g_form.getValue('start_date'));
ga.addParam('end_date',g_form.getValue('end_date'));
ga.getXML(callBack);
function callBack(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer=="false"){
alert("End Date cannot be greater than 7 days from the Start Date");
g_form.setValue('end_date','');
}
}
}
Thanks in advance
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2017 02:38 PM
Hi Alexander,
try this as well, this code will work irrespective of the date format
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var start_date = g_form.getValue('start_date');
var ga = new GlideAjax("calcDate"); //name of script include
ga.addParam("sysparm_name", "getDate"); //name of function in script include
ga.addParam("sysparm_start", start_date); //send start value to script
ga.addParam("sysparm_end", newValue);
ga.getXML(checkDate); //callback function
}
function checkDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script
if (answer > 7) { //if the date received is more than 7 days from the start date
alert("End date cannot be later than 7 days after start date.");
g_form.setValue('end_date', ''); //remove value from end date field
return false;
}
}
script include code-
var calcDate = Class.create();
calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{
getDate : function() {
var startDT = new GlideDate();
startDT.setDisplayValue(this.getParameter('sysparm_start'));
var endDT = new GlideDate();
endDT.setDisplayValue(this.getParameter('sysparm_end'));
var duration = new GlideDuration();
duration= GlideDate.subtract(startDT, endDT);
return duration.getDayPart();
},
type: 'calcDate'
});
Regards,
Lavanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2017 02:38 PM
Hi Alexander,
try this as well, this code will work irrespective of the date format
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var start_date = g_form.getValue('start_date');
var ga = new GlideAjax("calcDate"); //name of script include
ga.addParam("sysparm_name", "getDate"); //name of function in script include
ga.addParam("sysparm_start", start_date); //send start value to script
ga.addParam("sysparm_end", newValue);
ga.getXML(checkDate); //callback function
}
function checkDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //the response from the script
if (answer > 7) { //if the date received is more than 7 days from the start date
alert("End date cannot be later than 7 days after start date.");
g_form.setValue('end_date', ''); //remove value from end date field
return false;
}
}
script include code-
var calcDate = Class.create();
calcDate.prototype = Object.extendsObject(AbstractAjaxProcessor,{
getDate : function() {
var startDT = new GlideDate();
startDT.setDisplayValue(this.getParameter('sysparm_start'));
var endDT = new GlideDate();
endDT.setDisplayValue(this.getParameter('sysparm_end'));
var duration = new GlideDuration();
duration= GlideDate.subtract(startDT, endDT);
return duration.getDayPart();
},
type: 'calcDate'
});
Regards,
Lavanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2021 05:17 AM
Perfect! Works as expected.. Thanks