How to Send Attachments To Third party Tool using Rest Message
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 12:12 AM
Hi
I have created Bussiness rule for Rest message integration in incident table ,in script i have also mapped the attachments also but attachment script is not working
script:
function executeRule(current, previous /*null when async*/) {
function executeRule(current, previous /*null when async*/) {
try {
var rest = new sn_ws.RESTMessageV2('Test_integration_outbound', 'Create');
var Pcomments=current.comments.getJournalEntry(-1);//Get all comments
var attch=new GlideRecord('sys_attachment');
attch.addQuery('table_name=incident');
attch.addQuery('u_incident.number',current.sys_id);
attch.query();
if(attch.next){
var sa= new GlideSysAttachment();
var binData=sa.getBytes(attch);
var base64Data=GlideStringUtil.base64Encode(binData);
var filename=attch.file_name;
var filetype=attch.content_type;
}
// Prepare the request body
var requestBody = {
"Ticket":{
'Attachments' : {
'Attachment': {
'AttachmentContent': base64Data,
'AttachmentName': filename,
'AttachmentDescription': filetype
}
},
'CustomerTicketID': current.getValue('number'),
'Comments':Pcomments,
'CustomerContactInformation': current.caller_id.email.getDisplayValue(),
'Description': current.getValue('description'),
'Title': current.getValue('short_description'),
'TicketType':'Incident',
'AffectedCIs': current.cmdb_ci.getDisplayValue(),
'BusinessUnit': current.u_companys_affected.getDisplayValue(),
}
};
// Convert the request body to a JSON string
var requestBodyString = JSON.stringify(requestBody);
// Set the request body in the REST message
rest.setRequestBody(requestBodyString);
rest.setRequestHeader('Content-Type', 'application/json');
rest.setStringParameter('token', authCode);
// Execute the REST call
var response1 = rest.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
// Optionally, log the response for debugging
gs.info('REST Response: ' + responseBody1);
gs.info('HTTP Status: ' + httpStatus);
} catch (ex) {
var message1 = ex.getMessage();
gs.error('Error executing REST message: ' + message1);
}
})(current, previous);
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 01:06 AM - edited 10-23-2024 01:30 AM
Use
Thankfully there is a much easier way of doing this. RESTMessageV2 has a method called setRequestBodyFromAttachment(attachmentSysId).
Use this instead of setRequestBody. You can remove all of the code that calls GlideSysAttachment and just pass the sys id of the attachment you want to send to this method, and that should work for you.
***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***
Regards
Paul
Regards
Paul
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2024 02:24 AM
Hi @Paul Curwen
can u send modify code