Getting base64 encoded data of an attachments of request in a scripted rest api

Ansuha Chipuri
Kilo Guru

Hi,

We have a custom table that extends request table . Lets take table name as ABC

We have a record producer that creates tickets in this ABC table

this record producer has a variable of type Macro - which is an OOB attachment widget.

I have a scripted rest api that pulls all the variables data on the record producer

Now our customer is using second api call which is OOB to get the attachment content. the requirement here is the a scripted api call is returning empty value for attachment variable, here the customer wants to get base64 encodedd data of all the attachments so that he can avoid the second api call.

how i can code my scripted rest api to get all attachments base64 encoded data with the scripted rest api

 

Thanks in advance

 

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Sample script to get it for Scoped App

var gr = new GlideRecord('sys_attachment');

gr.get('3fd6017407d3dc50540bf2508c1ed027');

var sysAtt = new GlideSysAttachment();

var base64Data = sysAtt.getContentBase64(gr);

gs.info(base64Data);

For global scope

var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('sys_id', '3fd6017407d3dc50540bf2508c1ed027');
  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);
}

Regards
Ankur

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

Hi ankur,

Thanks for replying, does this code work for encrypted attachments as well with user having access to encryption context

Hi,

Your Scripted REST API will be doing the GlideRecord and then get the base64 data.

Try if that works.

haven't tried it earlier for encryption context

Regards
Ankur

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

@Ansuha Chipuri 

Hope you are doing good.

Did my reply answer your question?

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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