How to convert attachments to Base64

BoHyun Jung
Mega Sage

I would like to convert the file attached to the RITM to Base 64.

I want a code that can be tested in Background Script.

1 ACCEPTED SOLUTION

Hi@Maddysunil 

 

Did you test the script you shared? I'll share you the right script😁

var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', ritm_sys_id);
attachment.query();
while(attachment.next()){
    // Base64 Encoding
    var gsa = GlideSysAttachment();
    var base64 = GlideBase64.encode(gsa.getByte(attachment));
    var encoding = GlideStringUtil.base64Encode(base64);
}

 

View solution in original post

8 REPLIES 8

Hi@Maddysunil 

 

Did you test the script you shared? I'll share you the right script😁

var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', ritm_sys_id);
attachment.query();
while(attachment.next()){
    // Base64 Encoding
    var gsa = GlideSysAttachment();
    var base64 = GlideBase64.encode(gsa.getByte(attachment));
    var encoding = GlideStringUtil.base64Encode(base64);
}

 

Amit Verma
Kilo Patron
Kilo Patron

Hi @BoHyun Jung 

 

Below link could be helpful :

https://www.servicenow.com/community/now-platform-forum/getting-base64-encoded-data-of-an-attachment...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

shyamkumar VK
Kilo Patron

@BoHyun Jung  , What @Maddysunil  Suggested Script looks good for Background Script and this Works Well 

 

If you want to do this Dynamic later after your Test you can use below Snippet by Designing BR on Update , Insert on RITM (sc_req_item) Table 

 

    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_sys_id', current.sys_id);
    attachment.query();
    if (attachment.next()) {
        // Read the file content
        var fileContent = new GlideSysAttachment().getContent(attachment);
        // Convert to Base64
        var base64File = GlideStringUtil.base64Encode(fileContent);
        gs.info("Base64 encoded file content:\n" + base64File);
    } else {
        gs.info("No attachment found for RITM: " + ritmSysId);
    }
}

 

Regards,

Shyamkumar

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Amit Pandey
Kilo Sage

Hi @BoHyun Jung 

 

Please try the following BS-

var ritmSysId = 'your_ritm_sys_id_here';
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_name', 'sc_req_item');
attachmentGr.addQuery('table_sys_id', ritmSysId);
attachmentGr.query();

if (attachmentGr.next()) {
    var sysAttachment = new GlideSysAttachment();
    var attachmentStream = sysAttachment.getContentStream(attachmentGr.getUniqueValue()); 
    var encodedString = GlideStringUtil.base64Encode(attachmentStream);
    
    gs.info('Base64 of attachment: ' + encodedString);
} else {
    gs.info('No attachments found for RITM with sys_id: ' + ritmSysId);
}

Please mark my answer helpful and correct.

 

Regards,

Amit