Use Script to export records to XML

George P
Tera Guru

I have a need to export records to XML from multiple tables.  I need to do this on a fairly regular schedule and would like to use a script, but I cannot locate any examples to use a script to export records to XML.  Has anyone does this or have an idea of how I can?

1 ACCEPTED SOLUTION

Riya Verma
Kilo Sage
Kilo Sage

Hi @George P ,

 

Hope you are doing great.

 

we can achieve this by leveraging scripting capabilities. Below is an example script that demonstrates how you can export records to XML:

// Define the tables you want to export records from
var tablesToExport = ['table1', 'table2', 'table3'];

// Loop through each table
for (var i = 0; i < tablesToExport.length; i++) {
  var tableName = tablesToExport[i];
  
  // Query records from the table
  var gr = new GlideRecord(tableName);
  gr.query();
  
  // Create XML document
  var xmlDoc = new XMLDocument();
  xmlDoc.createElement(tableName + 's'); // Root element
  
  // Loop through the records
  while (gr.next()) {
    var recordNode = xmlDoc.createElement(tableName);
    
    // Add fields as child elements
    var fields = gr.getFields();
    for (var j = 0; j < fields.size(); j++) {
      var field = fields.get(j);
      var fieldName = field.getName();
      var fieldValue = gr.getValue(fieldName);
      
      var fieldNode = xmlDoc.createElement(fieldName);
      fieldNode.setTextContent(fieldValue);
      recordNode.appendChild(fieldNode);
    }
    
    xmlDoc.appendChild(recordNode);
  }
  
  // Export XML to a file
  var xmlContent = xmlDoc.toString();
  
  // Save the xmlContent to a file using appropriate methods for your environment
  
  // Log the export details
  gs.info('Exported ' + gr.getRowCount() + ' records from ' + tableName + ' to XML.');
}

ensure that you have the necessary permissions and consider any performance implications when exporting a large number of records.

I hope this script provides you with a starting point for exporting records to XML from multiple tables in ServiceNow

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

7 REPLIES 7

Thank you for the prodding, I should have provided an update earlier.

 

We wound up using Glide Update Manager2 to create an Update Set and looped through each required table and then each record on the table and add them to the Update Set.  This created a Local Update Set, but due to the size the export timed-out, so we then created a second script to perform the export which creates the XML file.  We then simply export the file.

This works great! 

In case anyone was wondering, here is how you can save your XML and plop it on your user record to retrieve later:

var rec = new GlideRecord("sys_user");
rec.get(gs.getUserID());
rec.addQuery("sys_id", gs.getUserID());
GlideSysAttachment().write(rec, "filename.xml", "text/xml", xmlContent);

One issue I'm running into is character escaping. 

I'm getting this in my output:

error on line 1874 at column 10: xmlParseCharRef: invalid xmlChar value 55349

 

How do you handle character escaping characters when needed?  I'm not finding anything on Docs.

Community Alums
Not applicable

Where is it exported to? How can I get the xml file?