Export all Update sets to XML - UI Action

restevao
Giga Expert

This code is still in an early access state, unclean but still works. Go ahead and update/improve.

Mainly designed for dev instances, not recommended for prod instances unless you can justify its need.

It works by using out of the box functionality for exporting an update set, just multiplies it for each update set.

 

WHAT

This exports all update sets in one go with the use of a UI Action

Currently it is possible to export all update sets, including ones that aren't in the 'completed' state. This can be adjusted in the code.

 

HOW

Client UI Action calls an ajax Script Include that does all the processing and returns values to be redirected in a URL

XML update sets are downloaded by multiple redirections to a page with the export update set URL.

 

 

FUTURE

It would be nice to have the Update Set download and save as the name of the update set and not the default id it is given. Currently the name is stored in the file — Some code for this is commented out, but still needs work - this may not be possible.

 

Also to have it only export updates sets that are selected in the update set list — code for this exists is commented out below but still needs some work.

 

SET UP

Names are note compulsory- feel free to adjust as you like, changes will have to be made in the code.

Create a UI Action and Script Include with the following

 

 

UI ACTION

Name: Export All Update Sets to XML

Table: Update Set [sys_update_set]

Order: 200

Active: TRUE

Show update: TRUE

Client: TRUE

List link: True

Onclick: AJAXCallUpdateXMLs()

Script:

 

functionAJAXCallUpdateXMLs()

{

                //maybe export selected list items in list view only

                /* var entries = g_list.getChecked().split(",");

                      if (!entries || entries.length == 0)

                      return;

                      alert(entries);

                                      */

/*       for (var i = 0 ; i < entries.length ; i++)

      {

      window.open("https://dev12053.service-now.com/export_update_set.do?sysparm_sys_id=" + entries[i] + "&sysparm_delete_when_done=false");    

      }*/

                            //ga.addParam('sysparm_entry_ids', entries.join(','));

                            // new GlideAjax object referencing name of AJAX script include

                      var ga = new GlideAjax("exportListUpdateSetXML");

                      // add name parameter to define which function we want to call

                      // method name in script include will be getFavorites

                      ga.addParam("sysparm_name", "getXMLs");

                     

                      // submit request to server, call ajaxResponse function with server response

     

                      ga.getXML(ajaxResponse);

                       

                      function ajaxResponse(serverResponse) {

                              // get result element and attributes

                       

                              var result = serverResponse.responseXML.getElementsByTagName("result");

                              var message = result[0].getAttribute("message");          

                              //check for message attribute and alert user

                              if(message)

                                      alert(message);

                       

                              //build output to display on client for testing

                              var output = "";

                       

                              // get favorite elements

                              var favorites = serverResponse.responseXML.getElementsByTagName("sysID");

                              for(var i = 0; i < favorites.length; i++)

                              {

                                      var name = favorites[i].getAttribute("name");

                                      var value = favorites[i].getAttribute("value");

                                      output += name + " = " + value + "\n";                     

                                      window.open("https://dev12053.service-now.com/export_update_set.do?sysparm_sys_id=" + value + "&sysparam_name=" + name +"&sysparm_delete_when_done=true");                                                               

                              }

                            alert(output);

                            return true;

              }

}

 

//downloadURI("https://dev12053.service-now.com/export_update_set.do?sysparm_sys_id=" + value + "&sysparam_name=" + name +"&sysparm_delete_when_done=true", name + ".xml");

                                      //pausecomp(1000);

/*

function downloadURI(uri, name) {

  var link = document.createElement("a");

  link.download = name;

  link.href = uri;

  document.body.appendChild(link);

  link.click();

  // Cleanup the DOM

  document.body.removeChild(link);

  delete link;

}*/

function pausecomp(ms) {

 

ms += new Date().getTime();

while (new Date() < ms){}

}

 

 

 

SCRIPT INCLUDE:

 

Name: exportListUpdateSetXML

API Name: global.exportListUpdateSetXML

Client callable: TRUE

Accessible from: All application Scopes

Active: TRUE

Script:

 

varexportListUpdateSetXML = Class.create();

  1. exportListUpdateSetXML.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

      getXMLs : function() {   

                              var result = this.newItem("result");

                              result.setAttribute("message", "returning");

             

                              //var index = 0;

                              var updateSet = new GlideRecord('sys_update_set');

                              //updateSet.addQuery('state','complete');                       // adjust here to select completed updates only

                              updateSet.query();

                              //gs.log("count us = " + updateSet.getRowCount());

                              while(updateSet.next())

                              {

                                              var retrievedUpdateSet = new GlideRecord('sys_remote_update_set');

                                              retrievedUpdateSet.initialize();

                                             

                                              retrievedUpdateSet.description = updateSet.description;

                                              retrievedUpdateSet.name = updateSet.name;

                                              retrievedUpdateSet.release_date = updateSet.release_date;

                                              retrievedUpdateSet.remote_sys_id = updateSet.sys_id;

                                              retrievedUpdateSet.application = updateSet.application;

                                                                                    

                                              var scopeGr = new GlideRecord('sys_scope');

 

                                              scopeGr.get(updateSet.application);

                                              if (scopeGr.isValid()) {

                                                      retrievedUpdateSet.application_name = scopeGr.name;

                                                      retrievedUpdateSet.application_scope = scopeGr.scope;

                                                      retrievedUpdateSet.application_version = scopeGr.version;

                                              }                                     

                                              retrievedUpdateSet.state = "loaded";                                   

                                              var sysid = retrievedUpdateSet.insert();                              

                                              var update = new GlideRecord('sys_update_xml');

                                              update.addQuery('update_set', updateSet.sys_id);

                                              update.query();

                                             

                                              while(update.next()) {

                                                    update.remote_update_set = retrievedUpdateSet.sys_id;

                                                    update.update_set = '';

                                                    update.insert();

                                              }

                                              //gs.log("Export # = " + index++);                                      

                                              this._addValue(updateSet.name, sysid);

                                              //gs.log("https://dev12053.service-now.com/export_update_set.do?sysparm_sys_id=" + sysid + "&sysparm_delete_when_done=true");

                                              //action.setRedirectURL("export_update_set.do?sysparm_sys_id=" + sysid + "&sysparm_delete_when_done=true");

                                              //redirect("export_update_set.do?sysparm_sys_id=" + sysid + "&sysparm_delete_when_done=true");      

                              }        

              // all items are returned to the client through the inherited methods of AbstractAjaxProcessor

      },

      _addValue : function(name, value) {

              var sysID = this.newItem("sysID");

              sysID.setAttribute("name", name);

              sysID.setAttribute("value", value);

             

      },

 

type: 'exportListUpdateSetXML'

});

 

you should end up with a button like this

 

Untitled.png

 

 

Please feel free to add any improvements and re-post

19 REPLIES 19

HI Jason



I tried above update set but it is exporting only one updateset rather bulk


Yes, I'm getting the same problem.   It's only exporting one and not the rest.


Posted to Share



ServiceNow Share


jason_siegrist1
Kilo Contributor

We have built this and added it to Share...



ServiceNow Share


Dear Jason,



can you share the updateset as an attachment to this post. I cant access the app.box its blocked from my office n/w and the servicenow share doesnt have an udpateset xml, it just has the pdf.



Thanks


Anil