Cup of coffee over some ServiceNow Dev

Reading Attachments Using ServiceNow GlideSysAttachment API

The problem may come up where you need to read data from an attachment file in your ServiceNow environment. This is made pretty simple by utilizing the ServiceNow GlideSysAttachment API. Let’s crack open a workflow and create a run script, or maybe even a “Fix Script” for testing.

The Code

var sys_id = "sys_id of the desired record"
//You can find attachment sys_id's on the 'sys_attachment' table
var attachment = new GlideSysAttachment();

//Grab the bytes data from the attachment
var fileBytes = attachment.getBytes("some_table", sys_id);

//Convert 'Bytes' to a string!
var contents = String(Packages.java.lang.String(fileBytes));

This is great for fetching the contents of the file. However, what if the file contents are in JSON and you’d like to use the great features of JSON from your file attachment.

Try adding this on the following lines of your code:

var jsondata = JSON.parse(contents.replace(/"/g, '"'));
gs.debug(jsondata);

What is possible with ServiceNow GlideSysAttachment API?

After perousing the internet for ahwile in attempts to find documentation for the GlideSysAttachment API, I haven’t discovered too much. Instead I decided to take a hack at finding what methods are even available in the API. Luckily, javascript is awesome and so I parsed the methods from the following code.

In a new System Definitions -> Fix Scripts. Create one and write the following code.

for (var methods in new GlideSysAttachment()) {
    gs.log(methods);
}

Selecting “Run Fix Script”, select “OK” and run in the background. Then show your progress workers and check out the log!

You should see the following methods for the GlideSysAttachment API come up.

setTableName, getClass, fileName, streamData, getBytes, tableName, setFileName, getBytesUnsafe, getFileDescriptor, setContentType, getAsStream, getAttachmentsForExtensions, write, getFileName, contentType, getTableSysID, attachmentsForExtensions, renameAttachment, getParameter, processRequest, replaceContent, read, setStreamCipher, sysID, getTableName, changeEncryptionContext, bytesUnsafe, inputStream, asStream, addEncryptionContexts, getInputStream, getAttachments, getContentType, errorFileName, fixImageDimensions, getDBImageAttachmentRecord, parameters, wait, attachments, imageDimensions, readToFile, setImageDimensions, getSizeBytes, notifyAll, delete, notify, getErrorFileName, allAttachments, deleteAllAttachments, setInputStream, hashCode, get, tableSysID, parameter, getSysID, streamMonitor, readByName, DBImageAttachmentRecord, class, loadParameters, setStreamMonitor, writeFromRequest, streamCipher, deleteAll, errorMessage, setTableSysID, sizeBytes, writeInvisible, getAllAttachments, fileDescriptor, bytes, equals, writeParts, getParameters, exists, toString, deleteAttachment, getErrorMessage

As you can see there are a lot of valuable functions associated with the GlideSysAttachment object in ServiceNow. In the coming weeks I’ll be delving into some of the functionality of these specific methods, and how we can leverage them for our needs!

If you have any questions or how-to’s about ServiceNow, comment below or use my contact form to ask me.

Good luck!

2 comments on “Reading Attachments Using ServiceNow GlideSysAttachment API”

  1. Hi Matt, were you yet able to figure out some more functionalities?
    My actual Problem is, that I am receiving multiple Attachments
    and .getBytes just takes the most recent one –
    I need to to read the attachment with content type text/xml 😉
    so a specific Attachment on the record ….
    Any experiences on that issue.
    many thanks in advance
    Matthias

    1. Ha – shortly before beeing desperate – and therefore seeking for help in various blogs – finally the solution comes to you …

      in order to share my information:

      function getAttachmentData(record) {
      var gr, bytes;
      gr = new GlideRecord(‘sys_attachment’);
      gr.addQuery(‘table_name’, ‘=’, record.getTableName());
      gr.addQuery(‘table_sys_id’, ‘=’, record.sys_id);
      //gr.addQuery(‘file_name’, ‘=’, ‘OebbFormular.xml’);
      gr.addQuery(‘content_type’, ‘=’, ‘text/xml’);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
      bytes = new GlideSysAttachment().getBytes(gr);
      return Packages.java.lang.String(bytes);
      }
      return ”;
      }
      var gr = new GlideRecord(‘sys_email’);
      gr.get(‘4600e2404f8dba0090e4e3518110c77f’);
      var ret = this.getAttachmentData(gr);
      gs.info(‘Return: ‘ + ret);

      //Re-convert to a string in Javascript, cause we don’t trust Rhino.
      ret= String(ret);

Leave a Reply

Your email address will not be published. Required fields are marked *