I am unable to use getXMLWait in scoped application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 10:23 PM
Hi Team,
I have to restrict user to submit catalog item if has more then 3 asset .
I am calling Script include in onSubmit script. I am using getXML method with callback function(Asynchronous).
I have written return false in onSubmit client script. As scoped application doesn't support getXMLWait I am unable to make Synchronous
call and stop user from submitting the form.
Client script -
var requested_for = g_form.getValue('requested_for');
var user = '';
if (requested_for != '') {
user = requested_for;
} else
user = requester;
//alert(user);
var ga = new GlideAjax('Modeldetails');
ga.addParam('sysparm_name', 'returnDetails');
ga.addParam('sysparm_caller_id', user);
ga.addParam('sysparm_staus', NeworOld);
ga.getXML(function(response) {
var count = response.responseXML.documentElement.getAttribute("answer");
// var country = ga.getAnswer().toString();
//alert('country: ' + country);
if (count == "No") {
g_form.showFieldMsg("is_this_a_new_or_replacement", "Sorry, you have exceeded the limit of 3 model per user. Please contact your local IT Team for further instructions", 'error', true);
return false;
}
});
Script include-
returnDetails: function() {
var userId = this.getParameter('sysparm_caller_id');
//var NeworOld = this.getParameter('sysparm_staus');
var count = 0;
var gr = new GlideRecord("alm_hardware");
gr.addQuery("model", "41ce0cb2974abd584649b0d3f153af8b"); // sys id of asset
gr.addQuery("assigned_to", userId);
gr.query();
while (gr.next()) {
count++;
}
if (count >= 3) {
return "No";
} else {
return "Yes";
}
},
How can we achieve it please suggest me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 12:02 AM
Hello ,
You can replace it with getXML(callback) or getXMLAnswer(callback).
For further reference. please check:
Best regards,
Boyan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 12:32 AM - edited 11-03-2023 12:33 AM
Hi @lucky24
getXMLWait() is not supported by the Service Portal.
Let's try this approach as an alternative. => KB0783579
Your script will look like below.
//Put these lines to your scripts
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var requested_for = g_form.getValue('requested_for');
var user = '';
if (requested_for != '') {
user = requested_for;
} else
user = requester;
var ga = new GlideAjax('Modeldetails');
ga.addParam('sysparm_name', 'returnDetails');
ga.addParam('sysparm_caller_id', user);
ga.addParam('sysparm_staus', NeworOld);
ga.getXML(function(response) {
var count = response.responseXML.documentElement.getAttribute("answer");
if (count == "No") {
g_form.showFieldMsg("is_this_a_new_or_replacement", "Sorry, you have exceeded the limit of 3 model per user. Please contact your local IT Team for further instructions", 'error', true);
return false;
}
//Put these lines to your scripts
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
});
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 12:35 AM
remember these points:
1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted
2) Synchronous GlideAjax is not allowed in portal and scoped application
Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit
How To: Async GlideAjax in an onSubmit script
Asynchronous onSubmit Catalog/Client Scripts in ServiceNow
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 12:35 AM
Hi @lucky24
getXMLWait() - will not work in scoped application.
Instead of calling glideAjax in onSubmit client script, use onChange client script to get the result from script include using getXMLAnswer() method and use that result in onSubmit script to stop/allow the submission.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates