- 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:41 AM
Hi Rachel,
You may find link helpful.
Thanks,
Jaspal Singh
Hit Helpful or Correct on the impact of response.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2019 08:45 AM
Hi,
Here you go:
Client Script:
var cdt = g_form.getValue('start_date'); //First Date/Time field
var sdt = g_form.getValue('end_date'); //Second Date/Time field
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if(answer > 30){
alert('End Date can't be greater than 30 days from Start date');
g_form.setValue('end_date','');
}
}
Script Include:
getDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(firstDT, secondDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
},
//Private function to calculate the date difference return result in second, minute, hour, day.
_calcDateDiff: function(diffTYPE, seconds){
var thisdiff;
if (diffTYPE == "day"){thisdiff = seconds/86400;}
else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
else if (diffTYPE == "minute"){thisdiff = seconds/60;}
else if (diffTYPE == "second"){thisdiff = seconds;}
else {thisdiff = seconds;}
return thisdiff;
}
See this link for more scripts:
Thanks,
Ashutosh Munot
MVP 2019
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 03:51 AM
I tried this but am getting errors with the script - missing '}' and ';' - not sure where...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 03:42 AM
Hi there,
Have a look at the below topic from this morning. No need for large Client Script scripting. Just UI Policies and the Condition Builder. Only part to script: showing an error message, maybe clearing the field value.
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2019 03:49 AM
I like the idea of this however the catalog item has a start date and an end date.
The start date can be any date if the future (i have logic to not allow a past date). The end date cannot be greater than the start date.