The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to retrieve an attachment from service now via SOAP Web Service

sherman_1206
Tera Contributor

So after trial and error and some help from service now we are now able to successfully retrieve attachments from service now via SOAP Web Service. The basic idea is the following: attachments are stored in sys_attachment_doc table in 4k chunks, the attachment information is stored in sys_attachment table. The sys_attachment table stores file's meta information including, file name, file size, file type.

The sys_attachment_doc table stores files in the following way. When service now receives an attachment, they take it gzip it, split the gzip into 4k chunks and then do a base64 encoding of the 4k chunks. So to retrieve is as simple as finding the 4k chunks, base64 decoding, constructing the gzip archive and then extracting the file from the archive.

The trick is finding the attachment chunks. This is simple for example to find an attachment of an incident, we can look in sys_attachment table for table = "incident", table_sys_id = "". This type of query would return an attachment record, we can take the sys_id field of this record and then search sys_attachment_doc table for all records containing sys_attachment = "". This will return 1+ records and each of these records contains a field "position". Position defines ordering, and simply is 0,1,2,3,.. for reconstructing. So during your query in sys_attachment_doc table you can add "__order_by"=>"position" to retrieve the records in order.

And as mentioned before once we have the chunks, we base64_decode each chunk, then build a gzip archive and then inflate the archive and voila you have your attachment.

-Cheers

(Thanks to service now, and the wiki for finding a solution to this problem.)

Currently we have an enhancement request for an easier way to make one soap call and retrieve a file. I will update if this is implemented.

27 REPLIES 27

john_andersen
Giga Guru

Your post was very informative. There was some very good, new information there. Thanks for sharing.

Some people have also implemented this a bit differently in the community and posted their work. You may be interested in this link:
http://www.servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/

That link describes a way to have a business rule "Send" the attachment to a third party via SOAP, however, you could probably create a scripted web service to do the same thing as what was done in that post. Just another food for thought.

-John



Your welcome, Im happy to share anything I can with the community. And thanks for the link, I am going to take a look at this.


Jacob_Andersen
ServiceNow Employee
ServiceNow Employee

There is a much easier way to accomplish what you're trying to do using scripted web services. You can actually retrieve the entire attachment and return it as base64 encoded data in the soap message. There is an example of this here: http://www.servicenowguru.com/integration/scripted-web-services/


Thanks jacob, I will look into this. I appreciate all the help! What I do not understand about scripted web services is how exactly I query or make a request with one I create? I understand how to hit any given WSDL predefined such as incident.do?WSDL and pass a query to it. For the one you suggested, would it merely be defining my own WSDL location, perhaps getattachment.do?WSDL and then passing to it a sys_id for attachment (from sys_attachment) table?