Prevent submit form when using callback function in catalog client script

kristianhaahrde
Mega Contributor

I have a catalog client script to validate if the input from the form already exist in the cmdb, before submitting.

When i click submit, i get a alert "Database already exist", but the form still submit. I try many combination now, but no solution is working for me. Can you please give me a helpfull advice?

i want onSubmit function to return false if answer is true in my callback function.

function onSubmit() {
   //Type appropriate comment here, and begin script below
	var dbExist = "";
    var dbName = g_form.getValue('u_dbname');
	if(dbName.length > 20) {
		alert("Database name is too long, mac character is 20");
		return false;
	}
	
	var sqlServer = g_form.getValue('u_server');
	sqlServer = sqlServer + ".dac.local";
	var inst = g_form.getValue('u_environment');
	
	if(inst == "Production") { inst = "INST1"; }
	if(inst == "Pre-Production") { inst = "INST2"; }
	if(inst == "Test") { inst = "INST3"; }
	if(inst == "Development") { inst = "INST4"; }
	
	var ga = new GlideAjax('u_getData'); 
	
	ga.addParam('sysparm_name','DbExist');   
	ga.addParam('sysparm_db_name',dbName);  
	ga.addParam('sysparm_sql_server',sqlServer);
	ga.addParam('sysparm_inst',inst);
	
	ga.getXML(ajaxResponse);   
		
	alert(dbExist);
	
	function ajaxResponse(response) {  
	    var answer = response.responseXML.documentElement.getAttribute("answer");
	    if(answer == 'true') { 
			alert("Database already exist"); 
			//g_form.submitted = false;
			return false;
		}
    }   	
}

 

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

This is happening because you're using an asynchronous call to the server and the client script isn't waiting for a response to come back.  While this is best practice, it simply won't work in an 'onSubmit' script for validation because the submit happens and redirects without waiting.  You need to use a synchronous call whenever you need this type of response in an onSubmit script.

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

While this will work in standard catalog forms, it will not work in a scoped app or in the Service Portal because ServiceNow has removed the capability.  If you need it to work in those places you'll need to figure out how to do your validation in an 'onChange' script or a business rule.

View solution in original post

5 REPLIES 5

Mark Stanger
Giga Sage

This is happening because you're using an asynchronous call to the server and the client script isn't waiting for a response to come back.  While this is best practice, it simply won't work in an 'onSubmit' script for validation because the submit happens and redirects without waiting.  You need to use a synchronous call whenever you need this type of response in an onSubmit script.

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

While this will work in standard catalog forms, it will not work in a scoped app or in the Service Portal because ServiceNow has removed the capability.  If you need it to work in those places you'll need to figure out how to do your validation in an 'onChange' script or a business rule.

Hi 

try using getXMLWait() in this case; 

and before return false try printing an alert();

getXMLWait are NOT working in this case.

i make a onChange script instead and validate on the fly, thanks for the good answer, it was usefull

Naveen4
Kilo Guru

hi try with sysncronus call .

 

Like this :

ga.getXMLWait();


https://docs.servicenow.com/bundle/helsinki-application-development/page/script/server-scripting/reference/r_ExamplesOfSynchronousGlideAjax.html?cshalt=yes


https://community.servicenow.com/community?id=community_question&sys_id=ba074be5db1cdbc01dcaf3231f961997