getXMLAnswer vs getXML - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

While working on GlideAjax from the client, the often seen method is using getXML (hopefully not getXMLWait, though that's another topic). I'm actually wondering why?! I prefer using getXMLAnswer. You might ask yourself now, why?! The reason very simple: getXMLAnswer is slightly easier to write 🙂 and getXML returns a whole XML Document while getXMLAnswer only returns an Answer which is far more efficient.

Usage

Here is an example of how you would use getXMLAnswer Client-side:

 

find_real_file.png

 

Here is an example of how you would use getXML Client side:

 

find_real_file.png

Again, why getXMLAnswer vs getXML

In the above pictures, you might have already noticed: getXMLAnswer returns the Answer and that's it. getXML still the Answer needs to be parsed. getXMLAnswer is far more efficient looking at it this way.

Far more efficient

getXMLAnswer

To make it more visual. Let's open the JavaScript Executor Client-Side (in Chrome: CTRL + SHIFT + J). And execute below code: 

 

var ga = new GlideAjax('callerUtilsAjax');
ga.addParam('sysparm_name', 'getComputer');
ga.addParam('sysparm_caller', g_form.getValue('called_id'));
ga.getXMLAnswer(_handleResponse);

function _handleResponse(answer) {

	console.log(answer);

}

 

If we open the Chrome Console (F12), we would see:

 

find_real_file.png

getXML

Now let's perform the same, though for getXML. Execute below code in the JavaScript Executor:

 

var ga = new GlideAjax('callerUtilsAjax');
ga.addParam('sysparm_name', 'getComputer');
ga.addParam('sysparm_caller', g_form.getValue('called_id'));
ga.getXML(_handleResponse);

function _handleResponse(response) {

	console.log(response);

}

 

If we open the Chrome Console, we would see:

 

find_real_file.png

Result

As you can see in above result pictures, getXMLAnswer only retrieves the Answer which we are actually after. getXML retrieves the whole XML document. In most cases, we are not interested in the whole XML document, though only in the Answer.

 

Using getXMLAnswer, a little bit easier to write, and far more efficient in what is getting returned! So please when working with GlideAjax Client-side, reconsider using getXMLAnswer instead of getXML.

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
AbhishekGardade
Giga Sage

Great Job Mark!!!!

Berati Sahin
Tera Contributor

Hi Mark,

I have a question. In the change request form There are two assessments. One of them is risk assessment and another one is score assessment. I defined them in the same table. When I used getXMLAnswer for UI Action, response becomes same for both assessments. So I can not progress in order to calculate the assessment point. CAN YOU HELP ABOUT THAT TOPIC?

Mark Roethof
Tera Patron
Tera Patron

Hi Berati,

Not sure what you mean here. Can you open a new community question? And paste your question + examples code or screenshots of your situation and the UI Action? Then I can have a look or other Community members will spot your question also and can help/answer your question.

 

Kind regards,
Mark

---

LinkedIn
Community article list

Berati Sahin
Tera Contributor

In related list there are two assessment form links. find_real_file.png

When I clicked the above, this window opens in the change request

find_real_file.png

When I click the below, window opens in the change request.

find_real_file.png

The above works correctly. When I edit the UI action for the second one namely "analysis document notes", res parameter in the getXMLAnswer function returns as DS in other words, in spite of I chose the "analysis document notes", res parameter becomes "fill the risk assessment" XML response.

find_real_file.png

During the bug, I tried to print "res" parameter. But as a response the DS assessment returns.

find_real_file.png

Besides, there is a smashing situation for both asessments.

 

I will be so appreciated for your help...

Patrick Zumbrun
Tera Contributor

Hi Mark

Very helpful, thank you! Just one question:

shouldn't the function of your second usage example be called as
_handleResponse(response)
instead of
_handleResponse(answer)

Regards
Patrick

 

Mark Roethof
Tera Patron
Tera Patron

Hi Patrick,

Actually you could call the parameter anything you want. Only thing, it should be consistent. So if you would call it "abc", then for example:

function _handleResponse(abc) {

	console.log(abc);

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

Patrick Zumbrun
Tera Contributor

Hi Mark

What I was trying to write is that the parameter name in your second example is not consistant as far as I understand.

In the function header you call it "answer" and within the function you reference it as "response".

Or am I wrong here?

Regards
Patrick

Mark Roethof
Tera Patron
Tera Patron

Ah the getXML example? Yes indeed there's a slight error in the image I see 🙂 Also noticiable because response. on the next line is not highlighted blueish. Will make a new image and replace this.

Tnx.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

Thomas Wright-1
Mega Expert

Thanks Mark! I didn't even know getXMLAnswer existed as I always copy and paste the docs example using getXML. Great to squeeze every little bit of efficiency out as we can

Manoj Reddy 7
Tera Contributor

Thanks for sharing @Mark Roethof 

Version history
Last update:
‎08-10-2024 08:15 AM
Updated by:
Contributors