- 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
08-02-2019 04:22 AM
Also that will perfectly fit in UI Policy 🙂
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 04:00 AM
Hi,
Refer this,
https://community.servicenow.com/community?id=community_question&sys_id=2a40b718db077348d58ea345ca96193b
Regards,
Bala T
- 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.");
}
}