Export to XML form script not woring

Abdul Musavvir
Kilo Guru

Hi Everyone

 

I'm trying to export list of records in XML, it is not working, showing the XML text in new tab.

However when I export in CSV, its working perfectly fine.

 

var url = 'sys_ui_bookmark_list.do?XML&sysparm_query=user=' + current.sys_id;
action.setRedirectURL(url);

 

Any Suggestions/comments?

 

Thanks in Advance

-Abdul Musavvir

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @Abdul Musavvir 

Referring the Servicenow document Export Directly from URL.

It didn't work for me as well.

However I have tried to create a new xml file with the values similar to the xml from link.

This xml is added to attachment table and can be downloaded.

Here is the script:

// Create a GlideRecord for the sys_ui_bookmark table with the desired query
var gr = new GlideRecord('sys_ui_bookmark');
gr.addQuery('user', gs.getUserID()); // Query based on the logged-in user's sys_id
gr.query();

// Create an XML document with a root element named 'sys_ui_bookmark'
var xmlDoc = new GlideXMLDocument('xml');

// Process each record and add it to the XML
while (gr.next()) {
    var recordNode = xmlDoc.createElement('sys_ui_bookmark');
    xmlDoc.getDocumentElement().appendChild(recordNode);

    // Get the fields, sort them, and add them to the record
    var fields = gr.getFields();
    var fieldNames = [];
    
    // Extract field names
    for (var i = 0; i < fields.size(); i++) {
        fieldNames.push(fields.get(i).getName());
    }

    // Sort field names in ascending order
    fieldNames.sort();

    // Add fields to the record in sorted order
    for (var j = 0; j < fieldNames.length; j++) {
        var fieldName = fieldNames[j];
        var fieldNode = xmlDoc.createElement(fieldName);
        
        // Get the field value
        var fieldValue = gr.getValue(fieldName);
       // Check the field type to handle different cases
        if (fieldName == 'sys_mod_count') {
            // Handle the sys_mod_count field as a number, not Boolean
            fieldValue = gr.getValue(fieldName); // Keep it as is
        } else if (fieldValue == '1') {
            fieldValue = 'true';  // Convert to 'true' if it's 1 (Boolean)
        } else if (fieldValue == '0') {
            fieldValue = 'false';  // Convert to 'false' if it's 0 (Boolean)
        }
        
        // Set the field value in the XML node
        fieldNode.setTextContent(fieldValue);
        recordNode.appendChild(fieldNode);
    }
}

// Serialize the XML content
var xmlContent = xmlDoc.toString();

// Save the XML content as an attachment
var attachment = new GlideSysAttachment();
var logGr = new GlideRecord('syslog'); // Use syslog or any table for storage
logGr.message = 'Bookmarks XML Export';
var logSysId = logGr.insert();
var attachmentSysId = attachment.write(logGr, 'bookmarks_export_sorted.xml', 'text/xml', xmlContent);

// Output the download link
if (attachmentSysId) {
    gs.print('Download the XML file from: /sys_attachment.do?sys_id=' + attachmentSysId);
} else {
    gs.print('Attachment creation failed.');
}

Tested this in background script. 

Result:

JuhiPoddar_0-1734973857528.png

With this link the xml file can be downloaded.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

9 REPLIES 9

@Abdul Musavvir 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Abdul Musavvir 

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Juhi Poddar
Kilo Patron

Hello @Abdul Musavvir 

Referring the Servicenow document Export Directly from URL.

It didn't work for me as well.

However I have tried to create a new xml file with the values similar to the xml from link.

This xml is added to attachment table and can be downloaded.

Here is the script:

// Create a GlideRecord for the sys_ui_bookmark table with the desired query
var gr = new GlideRecord('sys_ui_bookmark');
gr.addQuery('user', gs.getUserID()); // Query based on the logged-in user's sys_id
gr.query();

// Create an XML document with a root element named 'sys_ui_bookmark'
var xmlDoc = new GlideXMLDocument('xml');

// Process each record and add it to the XML
while (gr.next()) {
    var recordNode = xmlDoc.createElement('sys_ui_bookmark');
    xmlDoc.getDocumentElement().appendChild(recordNode);

    // Get the fields, sort them, and add them to the record
    var fields = gr.getFields();
    var fieldNames = [];
    
    // Extract field names
    for (var i = 0; i < fields.size(); i++) {
        fieldNames.push(fields.get(i).getName());
    }

    // Sort field names in ascending order
    fieldNames.sort();

    // Add fields to the record in sorted order
    for (var j = 0; j < fieldNames.length; j++) {
        var fieldName = fieldNames[j];
        var fieldNode = xmlDoc.createElement(fieldName);
        
        // Get the field value
        var fieldValue = gr.getValue(fieldName);
       // Check the field type to handle different cases
        if (fieldName == 'sys_mod_count') {
            // Handle the sys_mod_count field as a number, not Boolean
            fieldValue = gr.getValue(fieldName); // Keep it as is
        } else if (fieldValue == '1') {
            fieldValue = 'true';  // Convert to 'true' if it's 1 (Boolean)
        } else if (fieldValue == '0') {
            fieldValue = 'false';  // Convert to 'false' if it's 0 (Boolean)
        }
        
        // Set the field value in the XML node
        fieldNode.setTextContent(fieldValue);
        recordNode.appendChild(fieldNode);
    }
}

// Serialize the XML content
var xmlContent = xmlDoc.toString();

// Save the XML content as an attachment
var attachment = new GlideSysAttachment();
var logGr = new GlideRecord('syslog'); // Use syslog or any table for storage
logGr.message = 'Bookmarks XML Export';
var logSysId = logGr.insert();
var attachmentSysId = attachment.write(logGr, 'bookmarks_export_sorted.xml', 'text/xml', xmlContent);

// Output the download link
if (attachmentSysId) {
    gs.print('Download the XML file from: /sys_attachment.do?sys_id=' + attachmentSysId);
} else {
    gs.print('Attachment creation failed.');
}

Tested this in background script. 

Result:

JuhiPoddar_0-1734973857528.png

With this link the xml file can be downloaded.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

It worked, thank you Juhi.

 

I was wondering why it didn't work with whatever given in the documentation.

Hello @Abdul Musavvir 

Glad that the solution helped you to resolve the query.

 

Thank You

Juhi Poddar