The CreatorCon Call for Content is officially open! Get started here.

souren0071
Tera Expert

Introduction:

Hope we all are aware that the attachment details of a corresponding record gets stored in “sys_attachment” table in ServiceNow. There is another table where we can find the base64 encoded contents in the form of multiple chunks that is “sys_attachment_doc” for a particular attachment file.

Have you ever thought of getting the content of an attachment in string format and use that content for some specific task in service now?

 

 

Procedure:

We can use the chunks in “sys_attachment_doc” table and decode it to get the contents in string format. But the string format can contain unnecessary html tags used in different format of files (ex: .pdf, .xlsx, .doc etc). There is one more difficulty in identifying those attachment records having readable contents. There are be image, picture type of attachments also.

Here comes the unique feature of servicenow in “sys_attachment” table itself.

The below operations happen in the platform when any readable file gets attached:

  1. Any readable files attached in any types of records (i.e: incident, change_request, kb_knowledge) always inserts a record in “sys_attachment” table.
  1. There is another record in .txt format gets stored in the same table for a corresponding readable attachments removing all the html tags from the contents.
  1. Let’s say a .pdf file with name “Test Attachment.pdf” has been attached to a “kb_knowledge” record. The .txt file with a name “Summary of : Test Attachment.pdf.txt” gets stored in “sys_attachment” table.

So the relation of names between these two files is:

File_name of .txt file = Summary of : <File_name of actual file>.txt

  1. There is one more important relation between these two records in attachment is:

“sys_id” of actual attachment record = “table_sys_id” of summary record

 find_real_file.png

Please refer to the above screenshot:

sys_id of “Test Attachment.pdf” = ‘a133d21a4f71130017ef0f5e9310c741’.

Table_sys_id of “Summary of : Test Attachment.pdf.txt” = ‘a133d21a4f71130017ef0f5e9310c741

       

Now this .txt file can be used to extract only the content in string format. Please refer to the below written code:

var gsa = new GlideSysAttachment();

find_real_file.png

dataAsString variable contains the content in String format that can be used in querying any records within servicenow and perform database operations (insert, update, delete).

It’s preferred not to use the string format data while sending it through any web services(REST, SOAP) to third part systems. We know that servicenow support only UTF-8 encoding in a file. In case if any file with other encoding format such as UNICODE has been attached to servicenow, it would restrict the content having UNICODE character in Web Services methods (GET, POST etc.)

The preferred solution to address the above concern is to encode the content in base64Encoded format and send it to any third party tools using web services. The third party should have the facility of base64Decode.

 

 

Utility:

We always need to download an attachment and look for the content in it. There is no automated way to find the content and use it for any business requirement. Utility of this content processing is to have a complete control of an external entity like attachments in servicenow.

Version history
Last update:
‎05-03-2018 04:25 AM
Updated by: