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.

writeBase64 function of GlideSysAttachment class doesn't work

yewu
Mega Contributor

Try to use writeBase64 function of GlideSysAttachment class to save a binary file (base64 string) as an attachment of an sc_req_item record.

I tried the following:

var base64String = "...."

var attachement = new GlideSysAttachment();

var gr = new GlideRecord("sc_req_item");

// find the corresponding record

var attachment_sys_id = attachement.writeBase64(gr, "test.pdf", "application/pdf", base64String );

gs.log("system attchment id:" + attachment_sys_id);

attachment_sys_id is "undefined" and no attachment was created and attached.

However, if I change writeBase64 to write, an attachment was created and attached, attachment sys id was returned, but the attachment was not the correct format.

Any idea?

9 REPLIES 9

josh_nerius
ServiceNow Employee
ServiceNow Employee

Hello,



The example code you're using never specifies which Request Item to attach this file to.



Try something like this:


var base64String = "...."


var attachment = new GlideSysAttachment();


var gr = new GlideRecord("sc_req_item");


gr.get('aeed229047801200e0ef563dbb9a71c2'); // Replace this with desired sys_id


// find the corresponding record


var attachment_sys_id = attachment.writeBase64(gr, "test.pdf", "application/pdf", base64String );


gs.log("system attachment id:" + attachment_sys_id);



Replace the sys_id with the sys_id of the record you want to add this attachment to.


yewu
Mega Contributor

Thank you Josh!



We had a specific query which got us a gliderecord. I just didn't show all the code there.



In my example, if i use write instead of writeBase64, the file was attached to the request.


yewu
Mega Contributor

Our SN instance is Istanbul Patch6, not sure if it is a SN version issue


josh_nerius
ServiceNow Employee
ServiceNow Employee

Ahh, makes sense. I'd check a few things:



1) Try hard coding a sys_id when looking up the sc_req_item to make sure there aren't issues with the query/iteration logic.


2) Check log files - are there any messages after you attempt the insert?


3) Check the sys_attachment table - does anything show up here after the call to writeBase64()?