Anyone can give me example of synchronous GlideAjax for onSubmit script?

bbf3562
Kilo Guru

Apparently I have use asynchronous GlideAjax in onSubmit client script to validate the fields before submit the form which doesn't work right. Keep in mind that I am creating in scoped application, not global. If the box variable is missing, it trigger alert and prevent user from submit by using return false. However in this case, when I click submit form, it does trigger alert but then it close the form(it doesn't submit form at all) instead of staying in same form to fix it and resubmit it. Can anyone give me sample codes of how to do synchronous GlideAjax in onSubmit script? This is what I got for asynchronous GlideAjax,

in client script,

function onSubmit() {
var getExtReqBy = g_form.getValue('u_ext_reqby_lookup');

var ga = new GlideAjax('VerifyExternalCustomer');
ga.addParam('sysparm_name','checkCompany');
ga.addParam('sysparm_item',getExtReqBy);
ga.getXML(showResponse);

function showResponse(response) {
var typeOfRequest=response.responseXML.getElementsByTagName("Type");
var getAnswer = typeOfRequest[0].getAttribute('getAnswer');

if(getAnswer == 'no'){
alert('External customer does not have company name. Please update the company name before submit this form');
return false;
}
}
}

 

In Script Include,

var VerifyExternalCustomer = Class.create();
VerifyExternalCustomer.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkCompany: function(){
var requestedBy = this.getParameter('sysparm_item');
var getAnswer;
var gr = new GlideRecord('x_ibmdc_case_manag_ext_users');
gr.addQuery('sys_id', requestedBy);
gr.query();

if(gr.next()){
if(gr.u_company == ''){
getAnswer = 'no';
}else{
getAnswer = 'yes';

}
}


var typeOfRequest = this.newItem("Type");
typeOfRequest.setAttribute("getAnswer", getAnswer);

},
type: 'VerifyExternalCustomer'
});

7 REPLIES 7

hugogomes
Giga Expert

Hi there,

 

a synchronous ajax requires for you to put a "wait for reply" condition or it will proceed without doing what you intended.

In DOCS you can find an example of how to do that.

var ga = new GlideAjax('HelloWorld') ;
ga.addParam('sysparm_name','helloWorld');
ga.addParam('sysparm_user_name',"Bob");
ga.getXMLWait(); 
alert(ga.getAnswer());

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

I have mentioned above that I am creating a script in scoped application. ga.getXMLWait() does not support in scoped application.

In that case you will need to do it in a Business Rule, since you cannot do it in scoped applications.

There are some treads about the same issue in community, but there is no straight answer to this