Sending attachments to a 3rd-party service desk

I often hear requests for this to be bidirectional. Sending attachments from Service-now to a third-party system isn’t something that we’ve actually implemented in the past. However, with the number of people asking for it lately, I decided to write up a solution.

Fetch Attachment as Base64 Encoded Data

The following code is assumed to be within a business rule on the task table (or a table that inherits from task, such as incident, problem, change, etc).

var StringUtil =;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.addQuery('table_name', current.getTableName());
gr.addQuery('file_name', &'truck7.jpg');

if ({
var sa = new;
var binData = sa.getBytes(gr);
var encData = StringUtil.base64Encode(binData);
gs.log('record not found');

The above code is where the magic happens. The attachment to the incident is retrieved from the Service-now database and is now in base64 encoded data, ready to be sent to the integrating party via web services.

Send Attachment Data via SOAP

The code for sending the encoded data to a 3rd-party system might look something like this:

var envelope = new SOAPEnvelope();
envelope.createNameSpace('xmlns:imp', '');
envelope.addFunctionParameter('uid', current.correlation_id);
envelope.addFunctionParameter('content', encData);
envelope.addFunctionParameter('content_type', gr.content_type);
var request = new SOAPRequest('', 'user','pass');
var res =;

Fetch Attachment Data – Alternate Method

You very well may want to have the business rule run on the sys_attachment table instead of the TASK table. If this is the case, your business rule will look like the following:

var StringUtil =;

var sa = new;
var binData = sa.getBytes(current);
var encData = StringUtil.base64Encode(binData);

Date Posted:

April 14, 2010

Share This:


  1. ElPunto April 14, 2010 at 9:25 pm

    Is this a coincidence or did we post the solution for this problem at almost the same time ? (See Send Attachments using Web Services).

    How would you solve the additional functionality:

    Get all attachments and zip them to 1 file and send it as Base64 ???

  2. Jacob Andersen April 15, 2010 at 1:46 am

    That is funny. Out of curiosity, where do you get the code? It looks very similar to mine :).

    • ElPunto April 15, 2010 at 10:36 pm

      I used the Inbound code as starter and tried all kind of methods: read, get, getData, getAll until I struck at getBytes, however I used 2 arguments: tablename and sys_id.

      Can you give any help on the Zip part ? I tried but got a Java Constructor error. seems the Class is not available.

      • Jacob Andersen April 16, 2010 at 1:51 am

        Doing as you request for the ZIP file is difficult to explain via a comment as the best way to do this would be to create a new processor to handle this. I’ll see if I can write up a new post on how to do as you request so that you can come up with a solution that works for you.

        Expect that post within a week.


  3. Habin May 5, 2010 at 2:50 am

    Hi Jacob,

    Could you please provide the sample SOAP Messages and the operation of web service that we need to give to get the attachment back.

    Thanks in advance.


  4. Lee April 20, 2011 at 12:55 pm

    Hi Jacob

    I have few questions.

    1. The sys_attachment contains few fields. May I know which fields contains the binary data? Are we need to encode the whole record or only target to the field which contains binary data?

    2. May I know how to decode it in third party service desk? Can we decode it from Microsoft .NET program?



    • Mark Stanger April 21, 2011 at 3:31 am

      Hi Lee, As shown in the examples above, you encode the entire attachment record by referencing the GlideRecord object. Decoding in the 3rd party service desk obviously depends on the 3rd party system. If you’re using .NET, there are a ton of examples of how to decode Base64 encoded data out there if you Google for them.

  5. Abhiram Bharadwaj Diddigi October 18, 2011 at 10:21 pm


    Instead of sending it using Web service, Can we extend this method so that we can write this attachment to a shared folder?
    I have seen that link ‘exporting to CSV’ in wiki,but I am not sure if its the path to shared folder or a service-now folder :)

    Thanks :)

    • Mark Stanger October 19, 2011 at 4:22 am

      It’s probably possible but we don’t have a solution like that at the moment. We’ll try to look into this if we get time. In the mean time you should try posting this to the ServiceNow forums.


  6. Abhiram Bharadwaj Diddigi January 8, 2012 at 11:56 pm

    Hey Mark,

    How can I view all the methods of this class;
    I want to get the data( I mean the contents) of the attachment and display them rather than to convert them into bytes.

    Thanks again!

    • Mark Stanger January 11, 2012 at 1:03 pm

      Until ServiceNow decides to make those methods publicly available, it isn’t possible.

  7. Jason Wright October 5, 2012 at 3:50 am


    I’ve used the code as the starting point for requesting attachments using a Scripted Web Service, I receive the attachment data in a .net Client. It all works fine for text files, however for PDFs and Excel it doesn’t seem able to create a valid file. Does anyone else experienced this? Is there an extra step required?



  8. dheeraj kumar July 6, 2017 at 8:58 am

    Hi SN Guru,

    Could you please let me know how we can extract image and attachment data from java api . Any example will be grateful.

Comments are closed.




Fresh Content
Direct to Your Inbox

Just add your email and hit subscribe to stay informed.