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

20 REPLIES 20

@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

 

 

The better way to handle this used onChange client script:

 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var oldDate = new Date(oldValue);
    var newDate = new Date(newValue);

    var maxDiff = 30 * 24 * 60 * 60 * 1000;
    var diff = newDate - oldDate;
    if (diff > maxDiff) {
        g_form.addErrorMessage("You cannot extend the End Date by more than 30 days from the previously set date");
        g_form.setValue("u_end_date", oldValue);
    } else {
        return;
    }
}

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for.

 

Thank You !

 

@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