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

I am trying the Attachment API approach, but not sure what I'm doing wrong. I could successfully get the data and it seemed like correct byte data when I print the data. Though after encode in base64 and decoding by only decoders, it does not give the correct file. Do you know what is wrong in the below code?



var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://<instance>.service-now.com/api/now/attachment/<sys_id>/file')');


request.setHttpMethod('GET');




//Eg. UserName="admin", Password="admin" for this code sample.


var user = 'admin';


var password = 'admin';




request.setBasicAuth(user,password);


request.setRequestHeader("Accept","*/*");



var response = request.execute();


var body = response.getBody();


var content64 = GlideStringUtil.base64Encode(body.toString());


gs.print(content64);


I am not following the script.   What end point are you using?


I'm using the instance this is running on. So it points back to itself. How else would I be able to retrieve the attachments?


Can you please paste in the endpoint?   Don't need your instance name just the URL after that.   Otherwise I don't know which API you are using and cannot help.


Sorry about that Michael. It's https://<instance>.service-now.com/api/now/attachment/<sys_id>/file



Today, I have tried out a different approach, which encountered max size string errors. So I am assuming this approach will encounter the same problem. The problem was that String variables have a max size of 16MB 😕