- 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:34 AM
The solution was to change to onChange script and validate on the fly, instead of validate when i click submit. My solution looks like this and thanks to Mark Stanger for the helpfull advice
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue == 'DKCLUDSQL02' || newValue == 'DKCLUDSQL08' || newValue == 'DKCLUDSQL14' || newValue == 'DKCLUDSQL17'){
g_form.setDisplay('u_aris','true');
} else {
g_form.setDisplay('u_aris','false');
g_form.setValue('u_aris', 'false');
}
var sqlServer = newValue;
var dbName = g_form.getValue('u_dbname');
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);
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true') {
alert("Database already exist");
g_form.setValue("u_dbname","");
g_form.setValue("u_environment","");
g_form.setValue("u_server","");
}
}
}