Introduction

ServiceNow update sets are a great feature for moving changes or development efforts from one instance to another. The nature of the Update Set also makes it a convenient way to share applications, integrations, solutions, etc with the ServiceNow development community.

One major limitation of update sets, however, is the fact that they do not capture the changes of some tables in the system, nor do they capture file attachments. While, this is done on purpose by ServiceNow, there are some legitimate cases where you need to capture a file attachment within your update set.

Here are some examples: Certificates for specific integrations, icon images for custom applications, small JAR files to be distributed by MID Servers, etc.

While the ServiceNow Guru blog offers a great way to force-capture a specific record to the current update set, it does not handle file attachments on that record.

The Code

I have taken the background-script solution offered on the Guru site and have enhanced it to include any file attachments on the record as well.

WARNING: use caution when including files into an update set. These files will greatly enlarge the size of the update set file and could make the resulting update set too large to import into another instance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* EDIT THIS PORTION OF THE SCRIPT WITH YOUR INFORMATION */
var tableName = "TABLE_NAME_GOES_HERE";
var recordID = "SYS_ID_OF_RECORD_GOES_HERE";
/*   END OF EDIT SECTION   */


addRecordAndAttachmentsToCurrentUpdateSet(tableName, recordID);

function addRecordAndAttachmentsToCurrentUpdateSet(tableName, recordID) {
    //Get the main record
    var rec = new GlideRecord(tableName);
    rec.get(recordID);

    //Push the record into the current update set  
    var um = new Packages.com.glide.update.UpdateManager2();
    um.saveRecord(rec);

    //Add each attachment to Update Set
    var att = new GlideRecord("sys_attachment");
    att.addQuery("table_name", tableName);
    att.addQuery("table_sys_id", recordID);
    att.query();
    while (att.next()) {
        addAttachmentToUpdateSet(att);
    }
}

function addAttachmentToUpdateSet(attachmentGR) {
    var um = new Packages.com.glide.update.UpdateManager2();
    um.saveRecord(attachmentGR);

    var attdoc = new GlideRecord("sys_attachment_doc");
    attdoc.addQuery("sys_attachment", attachmentGR.sys_id);
    attdoc.orderBy("position");
    attdoc.query();
    while (attdoc.next()) {
        um.saveRecord(attdoc);
    }
}

Example of using this code

Let’s say you have a Sample Knowledge Base record with two sample attachments that you want captured into the Update Set as it is critical to your new ServiceNow application.

The record is on the “kb_knowledge” table and the sys_id of that record is: “1c0552c3c3543000fb47df384aba8f5e”.

Simply open up the “Scripts – Background” module in the instance (You may have to have additional rights to see the module) and paste in the code. Edit the “EDIT” section of the code as seen below:

If you view the update set, you should see entries for the Knowledge Base Record, the two sys_attachment records, and all of the sys_attachment_doc records that make up the two attachments.