in custom table i want to prevent sumbit if start date(custom field) is > end date(custom field)

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

   var s=g_form.getValue('u_sdate');
   var e=g_form.getValue('u_edate');
   var flag=0;
   if(e<s)
   {
    g_form.showFieldMsg('u_sdate','Start date must be greate than start date');
    flag=1;
   }
   if(flag==1)
   {
    g_form.submit(false);
   }
   else{
    g_form.submit();
   }


   } i want to cancel submission until start date must less than end date using  single client script 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@aditya2byte 

you can use single onSubmit client script for this

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

	g_form.hideErrorBox('u_sdate');
	g_form.hideErrorBox('u_edate');

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

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

View solution in original post

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

You can do this via a UI Policy as well—no need to write a large script.

*************************************************************************************************************
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]

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

Shashank_Jain
Kilo Sage

@aditya2byte ,

 

If you use only onChange, then:

  • You can show the error message immediately when the user selects wrong dates.

  • BUT  it cannot block the form from being submitted — because onChange runs only when a field changes, not at submission time.

So if you rely only on onChange, the user could still click Submit and save the record even if the dates are invalid.

 

Hope it helps!

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain – Software Engineer | Turning issues into insights

Bhimashankar H
Mega Sage

Hi @aditya2byte ,

 

You can easily do it via UI policy. 

 

Please refer this post Solved: Re: Date/time variable validation selected date sh... - ServiceNow Community

 

In your case you just need to change the condition "start Date is greater than End date"

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

@aditya2byte 

you can use single onSubmit client script for this

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

	g_form.hideErrorBox('u_sdate');
	g_form.hideErrorBox('u_edate');

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

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