- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2025 10:45 PM
I am trying to get the attachment from one of the end point, the attachment is passed via base 64 encoded. I would like to know how to decode this and view this attachment.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 08:07 PM
Hi @Community Alums
Try following script according to your table
var attachment_name = ;//attachment name with extension
var contentType = ;//content type of the file(optional)
var incident_id = "sys_id of the incident";
var stringUtil = GlideStringUtil;
var attachment = new Attachment();
var value = attachment.write('incident', incident_id, attachment_name, contentType, stringUtil.base64DecodeAsBytes(base64 encoded string));
Thanks,
Swathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 04:19 PM
Hi @Community Alums ,
You can refer this page to decode a base64 encoded string.
I think you can use "GlideStringUtil.base64Decode" for your purpose.
Please confirm it, thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 07:31 PM
@Community Alums
are you consuming the endpoint from global scope or scoped app?
what did you try so far?
What should happen after you decode? are you planning to add it as attachment to some record?
If yes then you will require filename, filetype as well
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 09:16 PM
are you consuming the endpoint from global scope or scoped app?
Yes
what did you try so far?
I tried to decode base 64 to binary data and will use the GlideSysAttachment.write() method to create an attachment and associate it with the incident record
// Base64 encoded attachment data (Replace with actual Base64 string)
var base64EncodedString = 'JVBERi0xLjQKJ...'; // This is your Base64 encoded data for the file
// The Sys ID of the incident record to attach the file to
var incidentSysId = 'your_incident_sys_id'; // Replace with the Sys ID of the incident record
// The file name you want to attach
var fileName = 'example_attachment.pdf'; // Replace with the actual file name you want
// The table name (in this case, 'incident')
var tableName = 'incident';
// Step 1: Decode the Base64 string to binary data (byte array)
var binaryData = GlideStringUtil.base64Decode(base64EncodedString);
// Step 2: Create the attachment on the incident record
var attachment = new GlideSysAttachment();
var attachmentSysId = attachment.write(incidentSysId, tableName, fileName, binaryData);
// Log the Sys ID of the created attachment
gs.info('Created attachment with Sys ID: ' + attachmentSysId);
(This didn't work for me )
What should happen after you decode? are you planning to add it as attachment to some record?
yes after decoding am planning to attach it to Incident record
If yes then you will require filename, filetype as well
what is file name and file type, I am trying to attach it in PDF to incident record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 09:32 PM
1) Are you consuming the endpoint from global scope or scoped app? yes
2) what did you try so far? Tried using a background script to decode base64 and attach to incident table as pdf using the below code in background scripts.
// Base64 encoded attachment data (Replace with actual Base64 string)
var base64EncodedString = 'JVBERi0xLjQKJ...'; // This is your Base64 encoded data for the file
// The Sys ID of the incident record to attach the file to
var incidentSysId = 'your_incident_sys_id'; // Replace with the Sys ID of the incident record
// The file name you want to attach
var fileName = 'example_attachment.pdf'; // Replace with the actual file name you want
// The table name (in this case, 'incident')
var tableName = 'incident';
// Step 1: Decode the Base64 string to binary data (byte array)
var binaryData = GlideStringUtil.base64Decode(base64EncodedString);
// Step 2: Create the attachment on the incident record
var attachment = new GlideSysAttachment();
var attachmentSysId = attachment.write(incidentSysId, tableName, fileName, binaryData);
// Log the Sys ID of the created attachment
gs.info('Created attachment with Sys ID: ' + attachmentSysId);
Getting error after running the script.