- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 01:55 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 07:49 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 05:34 PM
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.
Name | Type | Description |
---|---|---|
gr | GlideRecord | The record to which the attachment is to be attached. |
fileName | String | The attachment's file name. |
contentType | String | The attachment's content type. |
content | String | The attachment content in base64 format. |
Type | Description |
---|---|
String | The 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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 07:42 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 07:49 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2019 11:19 PM
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!