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

s_lefebvre
Kilo Expert

Hi,

Just for my information, have you try to create a business rule to verify this ?

Your script must be launch in service portal or just in back office ?

No I am not sure how business work will work for this. It make sense for client script because it validate in front-end of form before user submit it.

You can try advanced business rule with when field on before (insert or update).

I make this to test date in my scoped application and it's works perfectly.

 

On insert : 

find_real_file.png

 

On update : 

find_real_file.png

 

BR When to run : 

find_real_file.png

 

BR Actions : 

find_real_file.png

 

BR Advanced : 

find_real_file.png

How can I put in condition in Advanced if I use script to get yes or no or recognize message error from assignment group like this?

find_real_file.png