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

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



ServiceNow Share


linbergh
Kilo Explorer

Hey Raymond,



I have tried this and it actually works! But i do have a question. I have almost 80 updates sets. And if i download them all at once using your script it downloads them all but it only has the size of 1KB each file. its like it is just print out the sys_id and nothing more.Image 1.PNG


what version of Service Now are you using? i have tried this on a clone that had 250+ update sets and didn't have an issue.



i haven't tested this on Helsinki but i know it works in Fuji.


i was using Chrome when i did this. it may just be an issue of waiting for it to download.



One thing to note, because you have 80 update sets it will be an issue when it comes time to re-upload them into Service Now. i also advise creating a complete   merged update set of all sets and downloading that so it becomes easier to manage.



Depending on the amount of update sets you will have to alter the transaction properties below to prevent the system from timing out.


  1. glide.quota.manager.heartbeat


The number of seconds between the start of each Quota Manager heartbeat. This value determines how often the Quota Manager checks for transactions that exceed a quota and how often it writes status in the log file.


Type: Integer


Default value: 1


Location: Add to the System Properties [sys_properties] table


  1. glide.quota.manager.minimum_transaction_time


The minimum number of seconds a transaction must run before the Quota Manager matches it to a transaction quota. ServiceNow recommends setting this value to at least 1 second to avoid performance issues. For optimal performance, set this value to the value of your most restrictive quota. For example, if your most restrictive quota cancels transactions longer than 1 minute, set the minimum transaction time to 60 seconds.


Type: Integer


Default value: 1


Location: Add to the System Properties [sys_properties] table


From <http://wiki.servicenow.com/index.php?title=Transaction_Quotas#gsc.tab=0>


The SN Nerd
Giga Sage
Giga Sage

Upload to ServiceNow Share when you are done!



This could be useful to people.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Here is an update set, we used all the INFO from the threads above ....



Enjoy.



Bulk_UpdateSet_export - Box