Sending attachments to a 3rd-party service desk

///Sending attachments to a 3rd-party service desk

Sending attachments to a 3rd-party service desk

L

ast week I wrote about how to send a file to Service-now via web services and attach it to a target record.  While that is the most common request for attachment handling in regards to an integration, 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 = Packages.com.glide.util.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');
gr.query();

if (gr.next()){
  var sa = new  Packages.com.glide.ui.SysAttachment();
  var binData =  sa.getBytes(gr);
  var encData =  StringUtil.base64Encode(binData);
}
else{
  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', 'http://www.abcCompany.com/soap');
envelope.setFunctionName('imp:insert');
envelope.addFunctionParameter('uid', current.correlation_id);
envelope.addFunctionParameter('content', encData);
envelope.addFunctionParameter('content_type', gr.content_type);
var request = new SOAPRequest('http://www.abcCompany.com/addAttachment', 'user','pass');
var res = request.post(envelope);

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 = Packages.com.glide.util.StringUtil;

var sa = new   Packages.com.glide.ui.SysAttachment();
var binData =   sa.getBytes(current);
var encData =  StringUtil.base64Encode(binData);

Related Links:

By | 2010-11-29T16:43:25-07:00 April 14th, 2010|Categories: Integration, Web Services|Tags: , , |14 Comments

About the Author:

Jacob is the co-founder of Crossfuze, a ServiceNow implementation product and services company. He is one of the foremost ServiceNow integration experts in the world and designed/wrote many of the plugins found in the ServiceNow product today. Prior to co-founding Crossfuze he worked at ServiceNow during its startup years to write the SSO solutions and 3rd-party integrations.

14 Comments

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

    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 - Reply

    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 - Reply

      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 java.util.zip but got a Java Constructor error. seems the Class is not available.

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

        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.

        Thanks.

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

    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.

    Habin

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

    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?

    Regards

    CC

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

      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 - Reply

    Hi,

    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 - Reply

      Hi,
      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.

      Thanks!

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

    Hey Mark,

    How can I view all the methods of this class Packages.com.glide.ui.SysAttachment();
    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 - Reply

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

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

    Hi,

    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?

    Thanks

    Jason

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

    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.

Leave A Comment