Convert Attachment to base64 encoded content and vice versa
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 05:08 AM
Hi Team,
My first requirement is to get the attachment in base64encoded content, I just achieved this with the below script and output is also given below:-
var tableName = 'incident';
var recordID = 'ca644b5997261110e8fe39000153af00';
gs.print(getAttachmentContentsAsString(tableName, recordID));
function getAttachmentContentsAsString(tableName, recordID) {
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(tableName, recordID);
var base64string = GlideStringUtil.base64Encode(bytesInFile);
gs.info('Attachment content base64 encoded: ' +base64string);
}
My Second requirement is to convert base64 content to an attachment and linked to an incident .
- 6,123 Views
10 REPLIES 10

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 08:17 AM
This is what I did for a PDF attachment
function setPDF(item){
if(item == ''){
return '';
}
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',item);
gr.query();
if(gr.next()){
var sa = new GlideSysAttachment();
var binData = sa.getContentBase64(gr);
return binData;
}
}