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 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:
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 sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(current);
var encData = StringUtil.base64Encode(binData);
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 ???
That is funny. Out of curiosity, where do you get the code? It looks very similar to mine :).
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.
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.
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
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
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.
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 🙂
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!
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!
Until ServiceNow decides to make those methods publicly available, it isn’t possible.
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
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.
Hi Dheeraj,
I’d recommend that you post your question on the ServiceNow Commmunity Forum.