Ensure that the Start Date cannot be set to a past date.

Shreya3
Tera Contributor

Create Business Rules for Data Validation
Requirement:
This check must be validated on both for a new record and existing record.
If the date is in past, the user must not be able to save and proceed and get an error message displayed. Should be use business rule for this or client script? can anybody help me with the code for this as I am in learning period. 

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Shreya3 

 

No need to write any code, this can be achieved via UI policy.

 

Put condition in uiplocy and add error message in script part.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

but can we achieve this through client script? business rule should not be approached for this as far as i got to know right?

Hi @Shreya3

 

yes, you can do this via CS as well. but if the work can be done without code that will be better.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@Shreya3 

Which script to use depends on customer requirement

2 Ways

1) you should use onSubmit client script

function onSubmit() {
	//Type appropriate comment here, and begin script below

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = 'Please give valid start and end dates';
			g_form.showErrorBox('start_date', message);
			g_form.showErrorBox('end_date', message);
			return false;
		}

	}
}

OR

2) you can use before insert/update BR

Condition: Start Not Empty AND End Not Empty

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var start = new GlideDateTime(current.start_date);
    var end = new GlideDateTime(current.end_date);
    if (start.after(end)) {
        gs.addErrorMessage('Please give valid start and end dates');
        current.setAbortAction(true);
    }

})(current, previous);

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