The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Understanding getXML() and getXMLAnswer() in ServiceNow — Complete Guide with Use Cases

chandu kodurupa
Tera Contributor

 

Detailed Explanation of getXML() and getXMLAnswer() in ServiceNow

Hello ServiceNow Community! 👋

Introduction

In ServiceNow, working with records programmatically is mostly done via GlideRecord. Sometimes, you need the entire record’s data in a structured format, such as XML. ServiceNow provides two related methods for this purpose:

  • getXML()

  • getXMLAnswer()

Though they sound similar, they serve slightly different purposes and are used in different contexts. Understanding these methods thoroughly helps in writing efficient server and client-server scripts, especially when working with synchronous vs asynchronous operations.


What is getXML()?

  • getXML() is a server-side GlideRecord method.

  • It returns the full XML representation of a single record.

  • This XML string contains all the fields and their values of the current GlideRecord row.

  • The method works only when the GlideRecord is positioned on a valid record, e.g., after a .get() or .next() call.

How does it work internally?

  • Once a record is fetched, getXML() converts all field data into an XML format string, structured with field tags and values.

  • This XML can be used for:

    • Debugging (see all fields in one output)

    • Data transfer

    • Auditing and logging snapshots of records

    • Comparing records by XML content

Important notes:

  • Since it returns an XML string, it can be large for records with many fields.

  • It’s synchronous — meaning the script waits until the data is fetched and XML is generated.

  • Only available in server-side scripts — cannot be used on the client directly.


Example usage of getXML():

 

 
var gr = new GlideRecord('incident');
if (gr.get('number', 'INC0010001')) {
var xmlData = gr.getXML();
gs.info('Incident record XML:\n' + xmlData);
}

 

What is getXMLAnswer()?

  • getXMLAnswer() is a method on the GlideRecord query result object used for asynchronous queries.

  • When you execute an asynchronous GlideRecord query (using .query(function(result){})), getXMLAnswer() is called inside the callback to get the XML string of the record data.

  • It essentially serves the same purpose as getXML() but for async query results.

Why async?

  • Sometimes you want your server-side script to run non-blocking queries that don’t halt execution while waiting for data.

  • Async queries are common in GlideAjax calls or long-running server scripts.

  • getXMLAnswer() lets you access the XML inside the async callback when data is ready.

Important notes:

  • getXMLAnswer() can only be called inside the callback function after the async query completes.

  • The callback gives you a query result object, which has this method.


Example usage of getXMLAnswer():

 

 
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query(function(result) {
var xmlData = result.getXMLAnswer();
gs.info('Async Incident XML:\n' + xmlData);
});

 


Real-World Use Case Examples

1. Fetch and log record XML synchronously (using getXML())

 

 
var gr = new GlideRecord('incident');
if (gr.get('sys_id', 'some_sys_id')) {
var recordXml = gr.getXML();
gs.info('Record XML:\n' + recordXml);
}

 

2. Use async query with getXMLAnswer() inside GlideAjax Script Include

 

 
var IncidentXMLAjax = Class.create();
IncidentXMLAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getIncidentXMLAsync: function() { var incNumber = this.getParameter('sysparm_inc_number');
var gr = new GlideRecord('incident');
gr.addQuery('number', incNumber);
gr.query(function(result) {
var xml = result.getXMLAnswer();
gs.info('Async XML inside Script Include: ' + xml); // return or process XML here if needed }); } });

 

3. Client-side GlideAjax call triggering above Script Include

 

 

var ga = new GlideAjax('IncidentXMLAjax');

ga.addParam('sysparm_name', 'getIncidentXMLAsync');

ga.addParam('sysparm_inc_number', 'INC0010001');

ga.getXMLAnswer(function(response) {

alert('Received XML from server:', response);

});

 

Important Considerations

  • Performance: Calling getXML() on large records or many records can impact performance due to large XML string size.

  • Security: Be careful exposing sensitive fields in XML when sending data back to client.

  • Use Async Where Possible: For long queries or calls from client-side, use asynchronous GlideRecord queries with getXMLAnswer() inside the callback to keep the system responsive.

  • Parsing XML: On the client side, XML strings received via GlideAjax can be parsed using standard JavaScript DOMParser or libraries for further processing.

0 REPLIES 0