- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:05 AM
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;
}
}
}
Solved! Go to Solution.
- Labels:
-
Service Portal Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:14 AM
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.
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:14 AM
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.
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:17 AM
Hi
try using getXMLWait() in this case;
and before return false try printing an alert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:20 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:16 AM
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