- 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-12-2017 06:37 AM
Getting somewhere, but this is acting really odd. Also added clearValue('start_date'); to the client script to help prevent users from running into any odd issues with the date fields.
I'm getting some odd results. I'm able to select end dates greater than seven days in some situations, and others, I can't select and end date that's only two days away.
Threw an error:
Didn't throw and error:
And for what it's worth, it will throw an error if I select 4/20 as the end date, even if 4/19 is the start date. This only starts with 4/20.
I can select 4/12 as the start date and 4/19 as the end date and it works fine, no error message. But If I select 4/13 and 4/20, I get the error. Same if you select 4/14 - 4/20, 4/15 - 4/20, 4/16 - 4/20, etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 06:57 AM
My guess is that it has to do with the conversion.
Try Lavanya's scripts below and let me know, as they don't deal with the conversion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:05 AM
Lavanya's worked, I just need to add in an if block to make sure the end date isn't before the start date. Thanks again for all of your help and time, it's very appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:31 AM
I think you can add this:
//check if user put a date before the start date
if(start_date > end_date) {
alert('You cannot choose and end date that comes before the start date. Please change either start or end dates');
g_form.clearValue('end_date');
}
It should not be concerned with date conversions...
I am glad you got it solved.
harel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2017 07:38 AM
Yep exactly, I just had to add in a var for the end_date. Thank you again Harel!