How to send attachment as a binary data as one of the parameter inside the body to the third party??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 08:06 AM
When I tried to use the below code to convert the attachment to binary data I got the error. When explored more on the error I found the article saying this will only work scope other than global. is it right? if not how can I convert the attachment into binary data to send it to third-party ???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 03:01 AM
getBytes() function require GlideRecord object of sys_attachment but you are sending sysId
So assuming "attachment_list" is the correct name of input variable and it holds sysAttachment sysId, the below script should work fine
var attGr = new GlideRecord('sys_attachment');
attGr.addQuery('sys_id', inputs.attachment_list);
attGr.query();
if (attGr.next()) {
var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
var attachmentData = gsa.getBytes(attGr);
var attachment = String(Packages.java.lang.String(attachmentData));
var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
gs.info("attachment bae64 encoded data is" + encData);
}
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
02-12-2025 04:17 AM - edited 02-12-2025 04:30 AM
I am neither getting log nor error when I used above. can you check once.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2025 04:51 AM
I am able to get it.
Are you sure you gave the correct sys_attachment sysId as input to your action and using correct input variable name?
As per your earlier screenshot the variable name is "Attachment List" but in script you are using wrong spelling -> inputs.attachemntList
Hope you are testing it correctly
Output for script I shared
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
03-11-2025 01:59 AM
Hope you are doing good.
Did my reply answer your question?
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
04-10-2025 03:27 AM
Hope you are doing good.
Did my reply answer your question?
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