start date end date

dmahendran
Tera Contributor

I would like to implement a restriction where the end date cannot be selected beyond one month from the start date

18 REPLIES 18

@dmahendran 

then store that in some hidden variable or create a date variable and store there and make it readonly

Then compare the newly selected end date with this and then add validation. 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@dmahendran 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @dmahendran, I think you can restrict user by creating a business rule to achieve it easily.

you can use following configuration for BR:

- should be before Update

- In the advanced section

write condition as below

!current.end_date.nil() &&  !previous.end_date.nil() &&  current.end_date.changes() && current.end_date > previous.end_date

Script as below

(function executeRule(current, previous /*null when async*/ ) {
    var newEndDate = new GlideDate(current.u_test_date);
    var oldEndDate = new GlideDate(previous.u_test_date);
    var diffInMilliseconds = newEndDate.getNumericValue() - oldEndDate.getNumericValue();
    var diffInDays = diffInMilliseconds / (1000 * 60 * 60 * 24);
    if ( diffInDays > 30 ){
		gs.addErrorMessage('Date range is not allowed');
		current.setAbortAction(true);
	}
})(current, previous);

 

Regards,

Nishant

 

 

@dmahendran 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader