Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Base 64 to attachment conversion

supriya pratapa
Tera Guru

We have a base64 response and want to convert this base64 into attachment and attach it to a record in ServiceNow.

(function() {
    var base64Payload = 'JVBERi0xLjQKJeLjz9MKMSAwIG9iago8PC9UeXBlIC9DYXRhbG9nCi9QYWdlcyAyIDAgUgovTGFuZyAoZW4tVVMpCj4+CmVuZG9iagoyIDAgb2JqCjw8L1R5cGUgL1BhZ2VzCi9LaWRzIFsgMyAwIFIgXQo+PgplbmRvYmoKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMiAwIFIKL1Jlc291cmNlcyA8PC9Gb250IDw8L0YxIDQgMCBSPj4+PgovQ29udGVudHMgNSAwIFIKL01lZGlhQm94IFswIDAgMjAwIDIwMF0KL0Nyb3BCb3ggWzAgMCAyMDAgMjAwXQo+PgplbmRvYmoKNCAwIG9iago8PC9UeXBlIC9Gb250Ci9TdWJ0eXBlIC9UcnVlVHlwZQovTmFtZSAvRjEKL0Jhc2VGb250IC9IZWx2ZXRpY2EKL0VuY29kaW5nIC9XaW5BbnNpRW5jb2RpbmcKPj4KZW5kb2JqCjUgMCBvYmoKPDwvTGVuZ3RoIDYwPj4Kc3RyZWFtCkJUCjcwIDUwIFRECi9GMSAxMiBUZgowIDAgVEQKKEhlbGxvLCBXb3JsZCEpIFRqCkVUCmVuZHN0cmVhbQplbmRvYmoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4gCjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE1MCAwMDAwMCBuIAowMDAwMDAwMjUwIDAwMDAwIG4gCjAwMDAwMDAzNDYgMDAwMDAgbiAKdHJhaWxlcgo8PC9Sb290IDEgMCBSCj4+CnN0YXJ0eHJlZgozNTYKJSVFT0Y=';
    var base64Bytes = GlideStringUtil.base64DecodeAsBytes(base64Payload);
    var ByteArrayInputStream = Packages.java.io.ByteArrayInputStream;
    var inputStream = new ByteArrayInputStream(base64Bytes);
    var gr = new GlideRecord('incident');
    var recordSysId = '57af7aec73d423002728660c4cf6a71c';
    if (!gr.get(recordSysId)) {
        gs.error('Record not found: incident sys_id=' + recordSysId);
        return;
    }
    var gsa = new GlideSysAttachment();
    gsa.write(gr.getUniqueValue(),gr.getTableName(),'test10.pdf', 'application/pdf', inputStream);

    gs.info('Attachment created successfully.');
})();
 
The attachment is attached to the record but unable to open the file.

 

1 ACCEPTED SOLUTION

@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

View solution in original post

6 REPLIES 6

@supriya pratapa 

I already informed the script I shared should work fine provided you give correct base64 for PDF

The same script worked for me in global scope

-> I took base64 of some pdf file using this website "https://base64.guru/converter/encode/pdf"

-> then background script ran and file attached and when I opened the file, data is seen

see the gif attached

💡 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