The CreatorCon Call for Content is officially open! Get started here.

Convert Attachment to base64 encoded content and vice versa

Reena Kumari3
Tera Contributor

 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);
}



ReenaKumari3_0-1666092760188.png

 

My Second requirement is to convert base64 content to an attachment and  linked to an incident .

@Ankur Bawiskar  @Mark Roethof 

10 REPLIES 10

Mark Endsley
Tera Guru

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;
			
		}
		
	}