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.

Need to create Attachments via Scripted REST API on Scoped application (HR Core) from base64 encoded data.

Shane-DXC
Tera Expert

I am trying to add base64 encoded files (images, etc.) as attachments onto an HR Case (scoped) via integration with a Scripted REST API. (I am unable to use the REST Attachment API as the system sending the data is unable to provide the target record sys_id, and it also needs to include additional attributes to update the target record when it sends the attachment.)

I am able to achieve the task successfully in the Global scope without issue (other than the fact that there is a 25 Mb payload limitation), but the methods I am using in the Global scope (particularly GlideStringUtil.base64DecodeAsBytes()) are not available from the scoped application.  Is there a scoped alternative to GlideStringUtil.base64DecodeAsBytes()?

The plain method I utilized in Global scope was:

var data = '<base64 encoded image/file data>';
var attach = new GlideSysAttachment(),

//Decode base64 data
var decodedBytes = GlideStringUtil.base64DecodeAsBytes(data);
			
//Write attachment associated to target record
var attchID = attach.write(<target_GlideRecord>, <Attachment_Name>, <Attachment_Type>, decodedBytes);

If I try using the AttachmentCreator method, the ECC Queue fails to write the attachment (due to scope permissions) with an error like: "Could not find a record in table '<target_table>' with sys_id '<target_record>'."

I have been unable to pass the work over to a Global script include, as I am unable to pass back the decoded bytes for the actual creation of the attachment.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shane,

use following code and it should work; I have tested the same

writeBase64() method accepts base64EncodedData as last parameter and this should work in scoped app;

no need to convert to bytes etc

 

var data = '<base64 encoded image/file data>';
var attach = new GlideSysAttachment(),

//Write attachment associated to target record
var attchID = attach.writeBase64(<target_GlideRecord>, <Attachment_Name>, <Attachment_Type>, data);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

7 REPLIES 7

shiven
Tera Contributor

I have tried the "writeBase64" method in scope, unfortunately the attachment is generated using this but the file is corrupted, doesn't work for docx, image etc only works for text. 

 

 

@shiven 

can you post a new thread and tag me there as this is an old thread?

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