Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Asynchronous GlideAjax in onSubmit not working

wayneryeo
Kilo Contributor

Hi,

I have a single line variable in sp which i need to validate whether this value exists in the table record. if it exists, it will prevent the form from submmiting and shows the error message below the variable.

I am using the following in catalog client script with script include using async glideajax. can anyone help to advise as i cant seem to get the answer to stop my form submission? Thank you!

g_form.hideFieldMsg('name');

var validate = false;

if(g_form.getValue('name') != '')

{

checkName();

}

if(validate == false)

{

g_form.submitted = false;

return false;

}

else {return true;}

}  

function checkName(){

var ga = new GlideAjax('nameAjax');

ga.addParam('sysparm_name','checkname');

ga.addParam('sysparm_namelist',g_form.getValue('name'));

validate = ga.getXML(serverResponse);

}

function serverResponse(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

if(answer == 'true')

{

g_form.showFieldMsg('name','name Exists','error');

validate = false;

return false;

}

else{

validate = true;

return true;}

14 REPLIES 14

Hi Surya,



I tried your code but not able to get answer from my script includes.



alert(ga.getAnswer());         returns null value.i did a gs.info and confirmed that i am returning true on my script includes.


Sorry. forgot to add that my script includes is client callable and was able to get answer from script includes using async ajax before i changed my code.


Can you provide your server side code?



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,



my script include as below. client callable is ticked and acessible from all application scope.



var nameAjax = Class.create();


nameAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {




checkname:function(){


gs.info('here');                             // this was printed in log.


var name = this.getParameter('sysparm_namelist');


var returnValue = '';



var gr = new GlideRecord('u_name_record');


gr.get(name);


gr.query();


if(gr.getRowCount() == 0)


{


returnValue = 'false';


}


else


{


returnValue = 'true';


}


gs.info(returnValue); // this returns true which means there is such record.


return returnValue;


},



      type: 'nameAjax'


});


I think it might be best to go with a simple solution first.


GlideAjax may not be the best choice for this scenario (onSubmit), especially if you're new to scripting.


Also, if this is for a scoped app, GlideAjax is off the table completely for this scenario.
Refactor this before going into production.



g_form.hideFieldMsg('name');


var validate = false;



if(g_form.getValue('name') != ''){


        validate = checkName();


}



if(validate == false) {


g_form.submitted = false;


        return false;


} else {


        return true;


}



function checkName() {


var gr = new GlideRecord('u_name_record');


return gr.get(g_form.getValue('name'));


}



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022