Getting attachments via api and send them to Salesforce

yehiel
Kilo Explorer

Hi,

I work on an integration between ServiceNow   and Salesforce using Dell BOOMI.

I want to get Attachements from ServiceNow and send them to Salesforce.

As described in this link: How to retrieve an attachment from service now ... | ServiceNow Community ,     ServiceNow store attachments zipped, splitted in multiple chucks and then encoded in Base64.

On ServiceNow I can query sys_attachment table and then I can query sys_attachment_doc table where sys_attachment = sys_id from the previous call.

The result is a list of sys_attachment_doc records which have to be

  1. Base64 decoded
  2. combined in one document and send them to Salesforce.  
  3. Unzipped
  4. Send the result to SF

Currently I am able to get one document combined with all data but not decoded neither unzipped.

When I add the Base 64 Decode or the unzip step I get errors

When I remove the Base64 Decode step, the file is created in SF but it is corrupted.

When I leave the Base64 Decode step I get an error:

Test execution of [SNOW->SF] Update Case From Incident completed with errors. Embedded message: Unable to store data, error copying stream.; Caused by: Failed generating xml document; Caused by: Invalid white space character (0x1b) in text to output

the error occured at the decoding step.

Is there any other way to receive the whole attachment content together?

Any help will be appreciated.

5 REPLIES 5

adamjgreenberg
ServiceNow Employee
ServiceNow Employee

Sorry, let me be more clear.



in order to get the attachment out of the instance, not using the instance directly, you need to do the following:



decode base64 (encode base64(group concat (decode base64(data)) order by position)



eg:


from_base64(to_base64(group concat(from_base64(data)) order by position)




Then you should have a valid doc/image/whatever



Now you can place these in a container of your choice and send to SF.




I hope this makes sense.


Thank you. I still don't understand:

I did the following (as you suggested)

 

Step 1 - decode each chunk

Step 2 - group concat all decoded chunks ordered by position

Step 3 - encode the grouped chunks as one contiguous attachment.

 

Then I don't understand the last step:  If step 3 is to encode, so why do I need to decode again?

Step 4 - decode the attachment into the viewable document and place it in a container of your choice to send to where ever.

 

Actually I was able to get the files from ServiceNow and send them to Salesforce but they are corrupted and when we cannot open them with any app. (excel, picture viewer...)

 

I believe I am very close to the solution but I am still missing something elsewhere.

 

Thank you very much for your help.

Oleg
Mega Sage

I think that one don't need to access sys_attachment table sys_attachment_doc directly. I recommend you to consider to use GlideSysAttachment class (getContentBase64() or getContentStream()) or to use REST API Attachment API alternatively.

yehiel
Kilo Explorer

I am outside of ServiceNow so I can't use GlideRecords. I use REST api. But there is no documentation about the api response that I receive.