Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Base64 to Attachment Conversion

supriya pratapa
Tera Guru

I have tried below code and it works for converting base64 to an attachment(pdf). But the base64DecodeAsBytes Method is not documented under the GlideStringUtil API

ServiceNow Link: GlideStringUtil - Scoped, Global | ServiceNow Developers)

var base64Bytes = GlideStringUtil.base64DecodeAsBytes(base64Payload);

var attachment = new GlideSysAttachment();

var rec = new GlideRecord('incident');

rec.get('57af7aec73d423002728660c4cf6a71c');

var fileName = 'myfile.pdf';

var contentType = 'application/pdf';

var agr = attachment.write(rec, fileName, contentType, base64Bytes);

 

Also we have writeBase64 method in GlideSysAttachment() but this doesnt work when I tested it. 

var attachment = new GlideSysAttachment();

 

var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';

 

var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);

 

gs.info('The attachment sys_id is: ' + agr);

Link for the same:

 GlideSysAttachment | ServiceNow Developers

 

I would like to know the best practise to be followed here.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@supriya pratapa 

writeBase64() works only in scoped app and is not available when script runs in global scope.

script is written in global or scoped app?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@supriya pratapa 

writeBase64() works only in scoped app and is not available when script runs in global scope.

script is written in global or scoped app?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar 

Yes I have tested it in scoped app. But it didn't work.

@supriya pratapa 

this script worked for me in scoped app

var base64Payload = 'JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAyIDAgUgovTGFuZyAoZW4tVVMpCj4+CmVuZG9iagoyIDAgb2JqCjw8L1R5cGUgL1BhZ2VzCi9LaWRzIFsgMyAwIFIgXQo+PgplbmRvYmoKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMiAwIFIKL1Jlc291cmNlcyA8PC9Gb250IDw8L0YxIDQgMCBSPj4+PgovQ29udGVudHMgNSAwIFIKL01lZGlhQm94IFswIDAgMjAwIDIwMF0KL0Nyb3BCb3ggWzAgMCAyMDAgMjAwXQo+PgplbmRvYmoKNCAwIG9iago8PC9UeXBlIC9Gb250Ci9TdWJ0eXBlIC9UcnVlVHlwZQovTmFtZSAvRjEKL0Jhc2VGb250IC9IZWx2ZXRpY2EKL0VuY29kaW5nIC9XaW5BbnNpRW5jb2RpbmcKPj4KZW5kb2JqCjUgMCBvYmoKPDwvTGVuZ3RoIDYwPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgowIDAgVEQKKEhlbGxvLCBXb3JsZCEpIFRqCkVUCmVuZHN0cmVhbQplbmRvYmoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE1MCAwMDAwMCBuIAowMDAwMDAwMjUwIDAwMDAwIG4gCjAwMDAwMDAzNDYgMDAwMDAgbiAKdHJhaWxlcgo8PC9Sb290IDEgMCBSCj4+CnN0YXJ0eHJlZgozNTYKJSVFT0Y=';

var gr = new GlideRecord('incident');
var recordSysId = '57af7aec73d423002728660c4cf6a71c';
if (!gr.get(recordSysId)) {
    gs.error('Record not found: incident sys_id=' + recordSysId);
    return;
} else {
    var fileName = 'test10.pdf';
    var contentType = 'application/pdf';
    var attachment = new GlideSysAttachment();
    attachment.writeBase64(gr, fileName, contentType, base64Payload);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

 

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

@supriya pratapa 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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