- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2025 10:47 AM
Hi,
3rd party tool is calling servicenow to get attachments of RITM in binary stream.
I want to achieve this using script include and Scripted REST API. How to achieve this
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 03:16 AM
I am able to get the base64 data using background script in global scope
You can check the script and enhance
I believe if the base64 string is huge then it might not work while setting the response of scripted rest api
var attGr = new GlideRecord('sys_attachment');
attGr.addQuery('sys_id', 'b45f80356f102100758ecb512e3ee485');
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));
gs.info(attachment); //the data in the file will be printed as String
var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
gs.info(encData);
}
Output:
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-11-2025 03:00 AM
@Ankur Bawiskar ,yes tried, not getting any value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 03:16 AM
I am able to get the base64 data using background script in global scope
You can check the script and enhance
I believe if the base64 string is huge then it might not work while setting the response of scripted rest api
var attGr = new GlideRecord('sys_attachment');
attGr.addQuery('sys_id', 'b45f80356f102100758ecb512e3ee485');
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));
gs.info(attachment); //the data in the file will be printed as String
var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
gs.info(encData);
}
Output:
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-18-2025 11:47 PM
Hi @Ankur Bawiskar ,
When I try with files of size 96-100mb, we are not getting the base64. How can we achieve that.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 11:50 PM
it has a limitation for file size of 5MB
check this if it works, I haven't tested for huge file size such as 90MB or so
var gr = new GlideRecord('sys_attachment');
gr.get('d7ff2c044f96cc90fc11fa218110c746');
var StringUtil = new GlideStringUtil();
var gsis = GlideSysAttachmentInputStream(gr.sys_id.toString());
var ba = new Packages.java.io.ByteArrayOutputStream();
gsis.writeTo(ba,0,0);
ba.close();
var base64EncodedData = StringUtil.base64Encode(ba.toByteArray());
gs.info(base64EncodedData);
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-19-2025 12:04 AM
@Ankur Bawiskar , getting Out Of Memory error. Don't we have other option for large files?