The CreatorCon Call for Content is officially open! Get started here.

The date should be within the last 29 days!! If customer provides invalid date, present an ERROR message "Please choose a date within the 30 days. I need to use the onChange client script.

sakshi22
Tera Contributor

i have a date type variable and  date should be within the last 29 days!! If customer provides invalid date, present an ERROR message "Please choose a date within the 30 days. I need to use the onChange client script. Can someone please help me with it?

1 ACCEPTED SOLUTION

you cannot restrict dates in date picker.

since yours is date variable; update as this

onChange is running on which variable? it should be sap_restore_point right since user is selecting that

you want to compare restore point date with (today date-29)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() - 29);
	var selected_dateNum = new Date(newValue);

	if(selected_dateNum.getTime() < todayDate.getTime() ) {
		g_form.addErrorMessage('Date should be 29 days from Expected date');
	}
}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

yeah in date picker it was there but the date was getting selected

you cannot restrict dates in date picker.

since yours is date variable; update as this

onChange is running on which variable? it should be sap_restore_point right since user is selecting that

you want to compare restore point date with (today date-29)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() - 29);
	var selected_dateNum = new Date(newValue);

	if(selected_dateNum.getTime() < todayDate.getTime() ) {
		g_form.addErrorMessage('Date should be 29 days from Expected date');
	}
}

Regards
Ankur

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

@sakshi 

Glad to know that my script worked.

Please mark response helpful as well.

Regards
Ankur

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