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

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

Riya,

Thank you for this great start, but as I worked with this I found that the sys_id is dropped and a new one is assigned during import.  This causes an issue because it breaks some of the relationships.  Is there a way to ensure that related records remain related? 

Note: sys_ids stored in a reference field remain the same, so Assigned to for example is not affected since that related record already exists.  But where the related record does not exist, the relationship is broken.

sys_id isn't part of the gr.getFields().  I added it manually by including this under recordNode, at the top of the while:

var sys_id_node = xmlDoc.createElement('sys_id', gr.getValue('sys_id'));
recordNode.appendChild(sys_id_node);

 

Hi George

Did you get a script working to pickup the sys_id in the end?  

Best regards