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.

Limit Date selection between two Date Variables (60 Days) on Catalog Item

Dudesdog
Tera Contributor

Hello Community,

 

I have an ask to limit the time between two date variables, temp_date and temp_limit. The ask is that no more than 60 days be allowed to be selected on the catalog item (temp_date). I have the below client script however it is still allowing me to select more than 60 days on temp_limit. Can you please help?

function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading) {
if (newValue != '') {
// Current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);

// Get start date
var startDateStr = g_form.getValue('temp_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
 
// Get end date
var endDateStr = g_form.getValue('temp_limit');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
 
var diff = endDateNum - startDateNum;
var maxDiff = 60 * 24 * 60 * 60 * 1000; // 60 days * 24 hours * 60 minutes * 60 seconds * 1000 ms

if (endDateNum <= 0) {
alert('Please use the calendar icon to select a date.');
g_form.setValue('temp_limit', '');
} else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('temp_limit', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('temp_limit', '');
} else if (diff > maxDiff) {
alert('You cannot select a date more than 60 days after the start date.');
g_form.setValue('temp_limit', '');
}
}
}
}
 
Is there a way to do a UI policy to do this or is the client script the best practice? 

Thanks!
1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

Hi @Dudesdog 

 

Check following link once, this will resolve your issue and no code require to date validations through (Catalog) UI Policies.

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

Mark as correct and helpful if it solved your query.

Regards,

View solution in original post

3 REPLIES 3

Harish_K07
Giga Guru

Hi @Dudesdog 

 

Please check this article - https://www.servicenow.com/community/developer-forum/how-to-restrict-past-date-selection/m-p/1437062

 

Mark as correct and helpful if it solved your query.

Regards,

Harish

Rajesh Chopade1
Mega Sage

Hi @Dudesdog 

 

Check following link once, this will resolve your issue and no code require to date validations through (Catalog) UI Policies.

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

Mark as correct and helpful if it solved your query.

Regards,

Hello, I have set up a UI policy and this still allows the catalog item to be submitted. Is there anything else that needs to be done?