onSubmit client script should not use asynchronous AJAX methods, why ?

Bindhu1
Tera Contributor

Hi Team ,

As part of Health scan initiative, we have received recommendations from ServiceNow for code improvement. i.e,

Recommendation : onSubmit client script should not use asynchronous AJAX methods

steps to resolve : Use a synchronous AJAX call (getXMLWait()) to make a trip to the server within an onSubmit client script.

Details: Occurred on Line # 38

find_real_file.png

here is a full code :

 

function onSubmit() {
//Type appropriate comment here, and begin script below
/*
OnSubmit logic for transaction type 'Issue(1)'
*/
if (g_form.getValue('u_state') == '1' || g_form.getValue('u_state') == 1) {
var ConsumableSysIds = getSelectedConsumables(); // getSelectedConsumables()--> will return the array of selected consumable records sys_id's to process
if (ConsumableSysIds.length > 0) {
var ArrayOfJson = [];
var subtype = g_form.getValue('u_sub_state');
var reqfor = g_form.getValue('u_req_for');
var stockroom = g_form.getValue('u_stockroom');
var wbs = g_form.getValue('wbs_code');
var consTableAllRows = $j('#consumablesTble').DataTable().rows().nodes(); /* This API method will fetch all table rows included in all pages(pagination data) also */
for (var i = 0; i < ConsumableSysIds.length; i++) {

var Quantity = $j('#quantity_' + ConsumableSysIds[i].toString(), consTableAllRows).val();

var JsonObject = {
"sys_id": ConsumableSysIds[i],
"quantity": Quantity
};
// var str=JSON.encode(obj);
ArrayOfJson.push(JsonObject);
}

var StringifyArrayOfJson = JSON.stringify(ArrayOfJson);
//alert(StringifyArrayOfJson);
// return false;
var CallScriptInclude = new GlideAjax('ConsumableUtil');
CallScriptInclude.addParam('sysparm_name', 'CreateConsumableTransaction_Issue');
//CallScriptInclude.addParam('sysparm_sysids', ConsumableSysIds);
CallScriptInclude.addParam('sysparm_requestedfor', reqfor);
CallScriptInclude.addParam('sysparm_stockroom', stockroom);
CallScriptInclude.addParam('sysparm_subtype', subtype);
CallScriptInclude.addParam('sysparm_Json', StringifyArrayOfJson);
CallScriptInclude.addParam('sysparm_wbs', wbs);
CallScriptInclude.getXML(CheckEmailID);
} else {
g_form.addErrorMessage('Please select a peripheral before submission');
return false;
}
}

function CheckEmailID(response) {
// var result = response.responseXML.documentElement.getAttribute("answer");

}

}

 

 

Regards 

brunda

1 REPLY 1

Pedro Grilo1
Mega Sage

Hi,

 

I believe the issue with async ajax onSubmit is that the submission process will not wait for the async response from ajax. Depending on the validation you are trying to do, it may not work as expected and data may get submitted before the ajax response gets in.

 

I hope it helps!

Pedro