Catalog client script issue to replace getxmlwait
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 10:14 AM - edited 11-01-2023 11:14 AM
Hi Team
I have an Catalog Client Script (OnSubmit) where in recommendation is to replace getxmlwait function. Referred below 2 article but still am getting various java errors while submitting catalog.
Error; error there is a javascript error in your browser console servicenow
Appreciate if any suggestion on this please.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579
script:
- Labels:
-
Architect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 10:22 AM
@Willem Hi Willem if you can suggest to correct the script, would appreciate it. Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 10:26 AM
Hi @priceless,
Try below script
function onSubmit() {
if (g_scratchpad.isFormValid)
return true;
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sys_data_source");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
g_form.addErrorMessage("You must attach an Excel file to submit.");
return false;
} else {
var ga = new GlideAjax('DXC_ITAM_check_Excel_Row_count');
ga.addParam('sysparm_name', 'getRowCount');
ga.addParam('sysparm_xlssysid', gr.sys_id);
ga.getXML(handleResponse); // Make an asynchronous request
return false; // Prevent the form from submitting until the response is received
}
}
function handleResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue("qty", returneddata.counter);
g_form.setValue("guidcheck", returneddata.guidCheck);
g_form.setValue("hasGUID", returneddata.hasGUID);
g_form.setValue("hasIMEI", returneddata.hasIMEI);
g_form.setValue("imeicheck", returneddata.imeiCheck);
var model_category = g_form.getValue("model_category");
var hasGUID = g_form.getValue("hasGUID");
var hasIMEI = g_form.getValue("hasIMEI");
if ((model_category != "1233" && hasIMEI == "true")) {
g_form.addErrorMessage("xyz");
return false;
}
if ((model_category != "157575a" && hasGUID == "true")) {
g_form.addErrorMessage("xyz");
return false;
}
if ((model_category == "94ca" && hasGUID != "true")) {
g_form.addErrorMessage("xyz");
return false;
}
popupWait();
}
}
function popupWait() {
var gdw = new GlideDialogWindow('showLoadingDialogUI');
gdw.setTitle('Please wait until the ' + g_form.getValue("qty") + ' lines in the Excel are validated');
gdw.removeCloseDecoration();
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 11:15 AM
Hi Anand, receiving same error still ... java browser error etc..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2023 11:06 AM
Hello @priceless ,
The issue you're facing is due to the asynchronous nature of GlideAjax. The getXMLWait() function is synchronous and waits for the server to respond before proceeding, but it's deprecated. The getAnswer() function is asynchronous and doesn't wait for the server to respond before proceeding.
1. Replace ga.getAnswer() with ga.getXMLAnswer(callbackFunction). The callback function will be executed once the server responds.
2. Move the code that depends on the server's response into the callback function.
Please try below code i hope this will work for you.
function onSubmit() {
if (g_scratchpad.isFormValid) return true;
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sys_data_source");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
g_form.addErrorMessage("You must attach a Excel file to submit.");
return false;
} else {
var ga = new GlideAjax('DXC_ITAM_check_Excel_Row_count');
ga.addParam('sysparm_name', 'getRowCount');
ga.addParam('sysparm_xlssysid', gr.sys_id);
ga.getXMLAnswer(function(answer) {
if (answer) {
var returneddata = JSON.parse(answer);
g_form.setValue("qty", returneddata.counter);
g_form.setValue("guidcheck", returneddata.guidCheck);
g_form.setValue("hasGUID", returneddata.hasGUID);
g_form.setValue("hasIMEI", returneddata.hasIMEI);
g_form.setValue("imeicheck", returneddata.imeiCheck);
var model_category = g_form.getValue("model_category");
var hasGUID = g_form.getValue("hasGUID");
var hasIMEI = g_form.getValue("hasIMEI");
// rest of your code
popupWait();
}
});
}
}