- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2019 08:24 AM
I have a Service Catalog Item with a start date (start) and an end date (end).
I want to set it up so that when the end date is selected they cannot enter a date that is 30 days greater than the start date. If the end date is greater than 30 days from the start date, an alert should be prompted. I know I need an onChange Catalog Client Script but nothing I have seen in the community is working for me.
If someone could supply me with a script that would be much appreciated.
Thank you!
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 04:18 AM
Thank you all for your help
I was able to resolve by the below script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '' || newValue == oldValue) {
return;
}
g_form.hideFieldMsg("end", true);
/*
* Description: Each day is equal to 86,400,000 millisecond.
* Multiple this by 8 will give us the lower limit.
* Each year is 365 days 8 3 = 1095 8 each day in millisecond = upper limit
* to change the lower and upper limit simply change your days limit for each set
*/
//Set the lower and upper limit
//var lowerLimitDays = 8;
var upperLimitDays = 30; //(each year is 30 days)
//Get the new value of the Valid to date
var validTo = newValue;
//Get the today's date in millisecond
var startDate = new Date(g_form.getValue('start')).getTime();
//Convert valid to date to millisecond
var validToTime = new Date(validTo).getTime();
//Set the lower limit
//var lowerLimit = currDate + (86400000*lowerLimitDays);
//Set the higher limit
var upperLimit = startDate + (86400000*upperLimitDays);
/*
* Validate if the user entered date is within the lower and upper limit range
* If thats the case, then don't do anything
*/
if(validToTime <= upperLimit){
return true;
}else{
//If the date is not valid, then prompt the user to correct it
g_form.setValue('end','');
//g_form.showFieldMsg('end','The date you entered is greater than 1 year from the Start Date. End date should be less than 1 year from the start date.','error');
alert ("The date you entered is greater than 30 days from the Start Date. End Date should be less than 30 days from the Start Date.");
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2019 08:26 AM
Hi
I would recommend to use a Business Rule instead of a Client Script.
A Client Script is just making the UI experience better. You can do so as well, but NOT as the only option.
Instead: Create a "Before" Update&Insert Business rule, and check the condition inside the Business rule.
Then, you can abort the UPDATE, when invalid data was captured, and you can also show a message to the user, to inform about the situation.
Business Rules (executed on the Server) are always safer that using Client Scripts. The principle here ist "Never trust the client, as the scripts running on the client, could be tweaked".
Let me know, if that answers your question, and mark my answer as correct and helpful, please.
BR
Dirk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 03:33 AM
I only want this to apply to one catalog item.
If I go the route of the business rule - what is the logic I need to include in the condition?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2019 08:27 AM
try now.
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 > 30) { //if the date received is more than 7 days from the start date
alert("End date cannot be later than 30 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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 03:42 AM
This did not work for me - no error or anything...