Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Prevent user from entering an End Date that is 30 days greater than Start Date

rachelconstanti
Mega Sage

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!

1 ACCEPTED SOLUTION

rachelconstanti
Mega Sage

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.");

}

}

View solution in original post

12 REPLIES 12

Also that will perfectly fit in UI Policy 🙂

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Baala T
Mega Guru

Hi,

Refer this,

https://community.servicenow.com/community?id=community_question&sys_id=2a40b718db077348d58ea345ca96193b

 

Regards,
Bala T

rachelconstanti
Mega Sage

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.");

}

}