How to get Base64 content of all attachments on Incident?

peterraeves
Mega Guru

I have been reading about 'GlideSysAttachment.getBytes()', but it started to confuse me. As far as I understand it should be called like this:

var sa = new GlideSysAttachment();

var bytes = sa.getBytes(attachment.getValue("table_name"), attachment.getValue("table_sys_id"));

var content64 = GlideStringUtil.base64Encode(bytes);

What confuses me is that you have to use 'table_sys_id'. That you have to use the sys id of the incident instead of the attachment. So how do you differentiate between different attachments? What if you need to get all attachments on an Incident, how would you get the second attachment? I tried to replace 'table_sys_id' with 'sys_id', but that returned empty bytes...

14 REPLIES 14

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Be very careful with the getBytes() API call!   It is basically putting the attachment in memory and depending on the attachment size it could cause issues.   I am not sure your use case of needing this code, but there is now a REST Attachment API that is much safer to use:


Attachment API


We are interfacing with a different tool. So whenever a document is attached to one of our tickets, it should be sent over to the linked ticket in the other tool, which expects base64 strings for the attachments. So are you saying it would be better to call our own instance first through the API to retrieve the file, before sending it over to their tool? Is it possible with the Attachment API?



This also makes me wonder, why ServiceNow does not just have a function available... Ow wait they have, but it just does not work... (GlideSysAttachment.getContentBase64( GlideRecord sysAttachment))...


If this other tool accepts a RESTful call, you can easily pass the attachment via REST using setRequestBodyFromAttachment where you just need to pass it the SysID of the attachment record and it will pull it from the database and base64 encode it as well.


RESTMessageV2 - setRequestBodyFromAttachment(String attachmentSysId)


It does not, we have to do SOAP calls and call their attachment function ...


Oké I just realized, that GlideSysAttachment.getContentBase64 is not just not working. It just does not exist on pre-helsinki releases... I wonder what the alternative was 😕 This is getting pretty frustrating. Have been searching for such a long time 😕