Attachment to base64 in scoped application

Komal_D
Kilo Contributor

How to get attachment of incident record and convert that to base64 in scoped application.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Komal,

Following script should handle that: give the sysId in highlighted in bold

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', '<incidentRecordSysId>');
gr.addQuery('table_name', 'incident');
gr.query();
if (gr.next()){
var sa = new GlideSysAttachment();
sa.getContent(gr);
var encodedData = sa.getContentBase64(gr);
gs.info("Base64 Encoded data is : "+encodedData);
}

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Komal,

Following script should handle that: give the sysId in highlighted in bold

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', '<incidentRecordSysId>');
gr.addQuery('table_name', 'incident');
gr.query();
if (gr.next()){
var sa = new GlideSysAttachment();
sa.getContent(gr);
var encodedData = sa.getContentBase64(gr);
gs.info("Base64 Encoded data is : "+encodedData);
}

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

It seems getContentBase64 works only in scoped application, not in global.

Quite strange.

Is there any alternative to it in global scope?

 

Regards,

Swarnadeep Nandy

@Swarnadeep Nandy 

Refer this link where I have shared the approach for global scope

Attachments reading problem

Regards
Ankur

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

Hi Ankur!  Is there a way to do this the other way around.  So take a field that contains Base64 string and convert it back into an image (and then attach to the record)?  Thank you in advance for your help!!