Another attachment issue/question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2024 02:28 PM
Evening all. I am working on a requirement to send attachments through a rest integration, this triggers as expected, sends through to the other side (response 200) but they are getting an empty attachment and my logs show respsonsebody is empty. My BR is below. I am certain this is a simple fix but we seem to be going around the houses on it. Any help to get this finally working would be most appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2024 02:46 PM
Hi @Jack62,
Check out this code
(function executeRule(current, previous /*null when async*/) {
gs.log('Ensatt running');
var corrID = '';
var ensChk = new GlideRecord(current.table_name);
ensChk.addQuery('sys_id', current.table_sys_id);
ensChk.query();
if (ensChk.next()) {
corrID = ensChk.correlation_id;
gs.log('Ensatt corr ' + corrID);
}
var corrIDchk = corrID.search('e-CS');
if (corrIDchk != '-1') {
gs.log('Ensatt found e-CS');
var r = new sn_ws.RESTMessageV2('an integration', 'Get Attachment URL');
r.setStringParameterNoEscape('ticketno', corrID);
var responser = r.execute();
var responseBodyr = responser.getBody();
var httpStatusr = responser.getStatusCode();
gs.log('Ensatt GET responsebody: ' + responseBodyr);
gs.log('Ensatt status' + httpStatusr);
var parsed = JSON.parse(responseBodyr);
for (var key in parsed) {
if (parsed.hasOwnProperty(key)) {
var parsed_object = parsed[key];
var body = parsed_object.body;
}
}
gs.log('ensx ' + body);
gs.log('ensx att sys ' + current.sys_id);
gs.log('ensx att type ' + current.content_type);
// Create REST message for sending attachment
var attachmentMessage = new sn_ws.RESTMessageV2('an integration', 'Send attachment');
attachmentMessage.setRequestHeader('Content-Type', current.content_type);
attachmentMessage.setEndpoint(body);
// Read attachment content and set it as the request body
var attachmentGR = new GlideRecord('sys_attachment');
if (attachmentGR.get(current.sys_id)) {
attachmentMessage.setRequestBody(attachmentGR.getValue('content')); // Assuming 'content' is the field storing attachment data
}
// Execute REST message
var response = attachmentMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Ensatt PUT responsebody: ' + responseBody);
gs.log('Ensatt PUT status' + httpStatus);
}
})(current, previous);
or checkout this
https://www.servicenow.com/community/developer-forum/send-attachments-as-part-of-rest-message/m-p/13...
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!