When to use getXML vs getXMLAnswer in Glide Ajax
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 07:45 PM
Hello Experts,
Need help in Understanding GlideAjax , when to use getXML and when to use getXMLAnswer.
What I know is
when we use getXML we need to add the line, "var answer = response.responseXML.documentElement.getAttribute("answer")".
When we use getXMLAnswer we save our time by skipping the "var answer = response.responseXML.documentElement.getAttribute("answer")"
I wanted to know what is the best scenario where I can. Decide what to use based on the situation
Asynchronous Glide Ajax
var ga = new GlideAjax('ScriptInclude');
ga.addParam('sysparm_name','function_name');
ga.addParam('sysparm_user',"parameter1");
ga.getXML(HelloWorldParse);
A callback function :
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
Need help. Thanks in advance 😃

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 08:08 PM
Hi @anvitha ash ,
getXML(Function callback)
Sends the server a request to execute the method and parameters associated with this GlideAjax object.
The server processes the request asynchronously and -- when ready -- returns the results via the function specified as the callback_function.
Name | Type | Description |
---|---|---|
callback | Function |
The name of the callback function to process the results returned by the server.
|
getXMLAnswer(Function callback)
Call the processor asynchronously and get the answer element of the response in XML format.
Name | Type | Description |
---|---|---|
callback | Function |
The callback function. The function receives the answer element of the response in XML format as an argument. |
If you want to learn from usage point of view , then please refer to this article:
https://www.servicenow.com/community/developer-articles/getxmlanswer-vs-getxml/ta-p/2307589
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 08:19 PM
Hi @anvitha ash
Using getXML
- The getXML method is used when you need to handle the XML response directly. It is a lower-level approach that grants more control over the XML response.
- Use getXML when the server is returning complex XML data that you want to parse manually on the client side. This could be useful in scenarios where the server returns multiple pieces of information encapsulated in XML, and you need to extract multiple values from this response.
- Since you are directly dealing with the XML response, you can access other XML nodes or attributes that might be part of the response, not just an “answer”.
Example Scenario:
Imagine you have a Script Include that returns information about a user, including their name, department, and roles in an XML structure. Using getXML, you can parse this XML to extract these individual pieces of information for further processing or display on the client side.
2. Using getXMLAnswer
- getXMLAnswer is a convenience method that automatically extracts the “answer” attribute from the XML response. It simplifies the client-side code when you are only interested in a singular response value.
- Use getXMLAnswer when your server-side script (Script Include) is designed to return a single value (or a delimited string that you plan to parse client-side), and you are not interested in processing or handling XML directly.
Example Scenario:
Suppose you have a Script Include that calculates and returns the total number of open incidents for a specific user. Since the server returns a single value (the count of incidents), you can use getXMLAnswer for simplicity, avoiding manual XML parsing.
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning
Thanks & Regards
Deepak Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2024 09:12 PM
GlideAjax in ServiceNow is used for making AJAX (Asynchronous JavaScript and XML) requests to the server to execute server-side scripts and retrieve data. The choice between getXML and getXMLAnswer depends on the response structure you expect from the server-side script.
In terms of the scenario where you would decide which one to use, consider the following:
If you're working with an existing server-side script that already returns XML, or if the data you need is best represented in XML format (such as complex hierarchical data), then getXML is more appropriate.
If you're writing a new server-side script or updating an existing one to return a simple string or JSON object, and you only need a single value or a small set of data, then getXMLAnswer is more efficient and straightforward.
In your provided example, you're using getXML along with a callback function (HelloWorldParse). If the server-side script returns a response in XML format and you need to extract specific data from it, using getXML is appropriate. Your callback function (HelloWorldParse) then handles the response and extracts the desired information from the XML document.
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2024 05:21 AM
In Simple terms:
getXML > will get you the whole bunch of xml response in callback function (good, not perfomance friendly).
Eg: (In a bucket full of colorfull gems, you take a handfull of gems and then filter what color you need)
getXMLAnswer > Will get you only the parameter what you needed from server side (very good, and perfomance friendly.)
(form the bucket you only pick what color is needed rather than taking handfull and filtering)
i hope this helps...
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....