Trigger a business rule when an attachment is added to case table and eventually make outbound call
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2023 07:07 AM - edited 03-24-2023 07:10 AM
Hi All,
First of all, I am new to ServiceNow application and recently got an opportunity to work with ServiceNow. We have a requirement - to send attachments to another system via Outbound REST Message. To transfer attachments, I have configured the BR as... When to Run --> After, Update (event). and written a script as below, but my business rule is not getting triggered when I am attaching attachments to a case record. Please do help me on this.
Version 1:
(function executeRule(current, previous /*null when async*/)
{
try
{
var attachmentList = []; // Create an empty array to store attachment information
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name', current.getTableName());
attachment.addQuery('table_sys_id', current.sys_id);
attachment.query();
while (attachment.next())
{
var fileObj = {}; // Create a new object for each attachment
fileObj.name = attachment.getValue('file_name');
fileObj.type = attachment.getValue('content_type');
fileObj.content = attachment.getValue('content');
attachmentList.push(fileObj); // Add the attachment object to the array
}
var r = new sn_ws.RESTMessageV2('Attachments', 'Default POST');
r.setStringParameterNoEscape('baseURL', 'prod-96.westeurope.logic.azure.com:443/workflows/7uio1c9215154e5daa8ff6d57dc121b5/triggers/manual');
r.setRequestBody("{\"attachmentData\":\"" + JSON.stringify(attachmentList) + "\",\"case_sys_id\":\"" + current.sys_id);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch (ex)
{
var message = ex.message;
}
}) (current, previous);
Version 2:
(function executeRule(current, previous /*, g */) {
// Check if the current record has any attachments
if (current.hasAttachments()) {
// Get the attachment details
var attachments = current.getAttachments();
// Loop through each attachment
attachments.forEach(function (attachment) {
// Check if the attachment is new
if (attachment.isNewRecord())
{
// Get the attachment content, name, and type
var content = attachment.getContentStream().readAll();
var name = attachment.getDisplayValue('file_name');
var type = attachment.getDisplayValue('content_type');
// Prepare the payload for the API
var payload =
{
content: content,
name: name,
type: type
};
// Send the payload to the API
var r = new sn_ws.RESTMessageV2('Attachments', 'Default POST');
r.setStringParameterNoEscape('baseURL', 'prod-96.westeurope.logic.azure.com:443/workflows/7uio1c9215154e5daa8ff6d57dc121b5/triggers/manual');
r.setRequestBody("{\"attachmentData\":\"" + JSON.stringify(payload) + "\",\"case_sys_id\":\"" + current.sys_id);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
});
}
})(current, previous);
0 REPLIES 0