Get Attachments through REST API

Aruna Sree Yela
Tera Guru

Hi,

 

3rd party tool is calling servicenow to get attachments of RITM in binary stream.

 

I want to achieve this using script include and Scripted REST API. How to achieve this

 

Thanks

1 ACCEPTED SOLUTION

@Aruna Sree Yela 

I am able to get the base64 data using background script in global scope

You can check the script and enhance

I believe if the base64 string is huge then it might not work while setting the response of scripted rest api

var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('sys_id', 'b45f80356f102100758ecb512e3ee485');
  attGr.query();
  if(attGr.next())
        {
   var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
   var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
   var attachmentData = gsa.getBytes(attGr);
   var attachment = String(Packages.java.lang.String(attachmentData));
   gs.info(attachment); //the data in the file will be printed as String
   var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
   gs.info(encData);
}

Output:

AnkurBawiskar_0-1739272573205.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

20 REPLIES 20

@Aruna Sree Yela 

you are returning base64 data from script include, so use this

var siUtil = new SI_Utils();
var reqResponse = siUtil.getAttachment(request);
gs.info(reqResponse); // this will print the base64 data

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Info is fine, that's working. but, its not setting the response body. Getting this error

 

message: "Script Evaluation Exception",

details: "Cannot convert xebywynuemxyzdbednuic4....................."

 

var siUtil = new SI_Utils();
var reqResponse = siUtil.getAttachment(request);
gs.info(reqResponse);
response.setContentType('application/json');
if (reqResponse) {
        response.setStatus(200);
        response.setBody(reqResponse);
}

 

@Aruna Sree Yela 

the base64 data should be included ideally.

is the file big?

Try to use small file and see if that works

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,if I try with small files of around 350 bytes, its not converting into base64,its showing null in logs

@Aruna Sree Yela 

Did you try in background script if base64 is printed in logs?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader