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

Manish Vinayak1
Tera Guru

Hi Shane,

Did you try using writeBase64 function from GlideSysAttachment API? You don't need to convert the data to bytes if you are using this I suppose; and it is supposed to work in Scoped Application. I haven't tried it, but you could give it a try.

Here is the syntax from the documentation:

writeBase64( GlideRecord gr, String fileName, String contentType, String content_base64Encoded)

Inserts an attachment for the specified record using base64 encoded content.

Parameter(s):
NameTypeDescription
grGlideRecordThe record to which the attachment is to be attached.
fileNameStringThe attachment's file name.
contentTypeStringThe attachment's content type.
contentStringThe attachment content in base64 format.
Return:
TypeDescription
StringThe sysID of the attachment created.

 

Here's the link to the documentation:

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_SGSA-writeBase64_GR_S_S_S

 

Hope this helps!

Cheers,

Manish

Sukraj Raikhraj
Kilo Sage

Hi Shane, is the content for the attachment already in base64? I have this working in a load and transform using ECC queue. Please let me know if you are still having issue.

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

Thanks Ankur... this did the trick. 

I had tried this method earlier on and it did not work, but I could not remember why... I thought it worked with base64 encoded documents (like a text file) but not on base64 encoded images... but when I tried again so I could provide the error that was occurring... it actually worked!