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

Maddysunil
Kilo Sage

@BoHyun Jung 

Please try below code in background script:

 

// Define the RITM sys_id here
var ritmSysId = '1a7ec42b4730021096979530116d43e5';

// Fetch the RITM record
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmSysId)) {
    // Get the attached file
    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_sys_id', ritmSysId);
    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);
    }
} else {
    gs.info("RITM not found with sys_id: " + ritmSysId);
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Hi, @Maddysunil 

 

Decoding the encoded value results in undefined.

Please tell me how to decode it.

 

[background script]

var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', '5c8ebfb997094210499879b71153afdf');
attachment.query();
if(attachment.next()){
    // Read the file content
    var fileContent = new GlideSysAttachment().getContent(attachment);
    // Convert to Base64
    var base64FileEncode = GlideStringUtil.base64Encode(fileContent);
    gs.info("Base64 encoded file content:\n" + base64FileEncode);
    var base64FileDecode = GlideStringUtil.base64Decode(base64FileEncode);
    gs.info("Base64 encoded file content:\n" + base64FileDecode);
}

 

[log]

*** Script: Base64 encoded file content:
dW5kZWZpbmVk
*** Script: Base64 encoded file content: undefined

@BoHyun Jung 

 

// Define the RITM sys_id here
var ritmSysId = 'YOUR_RITM_SYS_ID';

// Fetch the RITM record
var ritm = new GlideRecord('sc_req_item');
if (ritm.get(ritmSysId)) {
    // Get the attached file
    var attachment = new GlideRecord('sys_attachment');
    attachment.addQuery('table_sys_id', ritmSysId);
    attachment.query();
    if (attachment.next()) {
        // Read the file content
        var fileContent = new GlideSysAttachment().getContent(attachment);
        
        // Decode the Base64 encoded content
        var decodedContent = GlideStringUtil.base64Decode(fileContent);
        
        gs.info("Decoded file content:\n" + decodedContent);
    } else {
        gs.info("No attachment found for RITM: " + ritmSysId);
    }
} else {
    gs.info("RITM not found with sys_id: " + ritmSysId);
}

 

Also you can go through below link:

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

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks