Service Now - FileAttachment field type

Wednesday May 20, 2020

The FileAttachment type is new to ServiceNow and it’s not really well documented (or well implmeneted, but that’s another story).

Let’s take a look at what happens when we have a FileAttachment on a record. Here we’re using a scoped app with a simple table called test_table.

We can use this in an ATF test step to set a value for the FileAttachment field - there is not currently a step to do that.

Code

First, we create a table with a FileAttachment type in our scoped app, the field is called design_document.

Then, we create a record and added an attachment to the record - this particular file is called RubeGoldberg.jpg.

Now it gets interesting, let’s look up the record from the table we created and get the value of the design_document field:

var tableName = 'test_table';
var sysId = '79ccc0a7dbfc5c102c071f83059619eb';
var gr = new GlideRecord(tableName);
gr.get(sysId);

var val = gr.getValue('design_document');

The value is 9c0d40a3dbfc5c102c071f83059619eb - looks like a sys_id - let’s check the attachment table for that:

var attachment = new GlideRecord('sys_attachment');
if(!attachment.get(val)){
    throw('Cannot find value in attachment table');
}


gs.debug("File name   : " + attachment.getValue('file_name'));
gs.debug("Table name   : " + attachment.getValue('table_name'));
gs.debug("Table sys_id : " + attachment.getValue('table_sys_id'));
    File name : RubeGoldberg.jpg
    Table name : ZZ_YYtest_table
    Table sys_id : 79ccc0a7dbfc5c102c071f83059619eb

Now, here’s one thing not to like about this field. We can see the table name is ZZ_YYtest_table instead of just test_table. This does not help us figure out which column that this attachment belongs to.

We’ll make a note of the table name since when we create an attachment for this field we’ll need to attach the file to the record itself and then update the table name to make it work.

Get attachment content stream

Now we can get a stream of the attachment content with GlideSysAttachment (we can also use getContentBase64 to get a base64 string and getContent to get a text string:

var gsa = new GlideSysAttachment();
var contentStream = gsa.getContentStream(attachment.getValue('sys_id'));