- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
if you are in global scope then use this
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 you are using in scoped app then use this
var gr = new GlideRecord('sys_attachment');
gr.get('3fd6017407d3dc50540bf2508c1ed027');
var sysAtt = new GlideSysAttachment();
var base64Data = sysAtt.getContentBase64(gr);
gs.info(base64Data);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
if you are in global scope then use this
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 you are using in scoped app then use this
var gr = new GlideRecord('sys_attachment');
gr.get('3fd6017407d3dc50540bf2508c1ed027');
var sysAtt = new GlideSysAttachment();
var base64Data = sysAtt.getContentBase64(gr);
gs.info(base64Data);
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hey @Alon Grod
Try this :
Business Rule (After Insert on sys_attachment)
Script :
(function executeRule(current, previous) {
try {
var stringUtil = new GlideStringUtil();
var inputStream = new GlideSysAttachmentInputStream(current.sys_id.toString());
var outputStream = new Packages.java.io.ByteArrayOutputStream();
inputStream.writeTo(outputStream, 0, 0);
var base64 = stringUtil.base64Encode(outputStream.toByteArray());
outputStream.close();
gs.info("Base64: " + base64);
// Use the Base64 value as required (REST payload, custom field, etc.)
} catch (ex) {
gs.error("Failed to generate Base64: " + ex);
}
})(current, previous);*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.
Regards
Vaishali Singh
Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Alon Grod ,
Use an Async Business Rule on the Attachment [sys_attachment] table so the file upload is not blocked.
Business Rule configuration:
Table: Attachment [sys_attachment]
When: Async
Insert: True
Advanced: True
Add a condition to restrict execution to the required table, for example:
Table name is incident
Script for a scoped application:
(function executeRule(current, previous) {
var attachmentSysId = current.getUniqueValue();
var fileSize = parseInt(current.getValue('size_bytes'), 10) || 0;
var maximumSize = 5 * 1024 * 1024;
if (fileSize > maximumSize) {
gs.warn(
'Base64 conversion skipped. Attachment exceeds 5 MB: ' +
attachmentSysId
);
return;
}
var attachmentGR = new GlideRecord('sys_attachment');
if (!attachmentGR.get(attachmentSysId)) {
gs.error(
'Attachment record not found: ' +
attachmentSysId
);
return;
}
var base64Content =
new GlideSysAttachment().getContentBase64(attachmentGR);
if (!base64Content) {
gs.error(
'Unable to retrieve Base64 content for attachment: ' +
attachmentSysId
);
return;
}
var payload = {
file_name: attachmentGR.getValue('file_name'),
content_type: attachmentGR.getValue('content_type'),
attachment_sys_id: attachmentSysId,
content_base64: base64Content
};
// Pass the payload to a reusable Script Include,
// event, Flow Action, or outbound integration.
// Example:
// new x_your_scope.AttachmentIntegration().send(payload);
})(current, previous);
Important:
getContentBase64() is supported in scoped applications and returns a maximum of 5 MB. It returns undefined when called from Global scope.
If the Business Rule is in Global scope and the attachment is being sent to an external API, avoid converting it to Base64 where possible. Send the attachment directly as the request body:
var request = new sn_ws.RESTMessageV2(
'Your REST Message',
'Send Attachment'
);
request.setRequestBodyFromAttachment(
current.getUniqueValue()
);
var response = request.execute();
This binary approach is preferable for large files because Base64 increases the payload size and memory usage.
Do not:
- Run the conversion in a Before Business Rule.
- Run the rule for every attachment without a table condition.
- Log the complete Base64 value.
- Store large Base64 strings permanently in a table field.
- Query or concatenate records from sys_attachment_doc manually.
- Use getBytes() for large attachments.
If the external API specifically requires Base64 inside JSON, keep the file-size limit and process it asynchronously through a reusable scoped Script Include or Flow Action.
Official references:
https://www.servicenow.com/docs/r/api-reference/server-api-reference/c_RESTMessageV2API.html
Hope this helps!
If this response helped, please mark it as Helpful.
If it resolves your issue, please Accept it as Solution.
Kind Regards,
Abhishek Pal