Sending attachments to third party system

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-06-2022 12:06 PM
Hello All,
I have a problem sending attachment to a third party system from Servicenow. I can send it through Postman but not able to send it form ServiceNow Rest message.
This is what third party system expects:
curl --form 'content=id: 20220325001\nAction: correspond\nAttachment: test.txt\nText: Ticket Body.' --
form 'attachment_1=@test.txt' 'http://xxxxxxx.net/REST/1.0/ticket/20220325001/comment?
user=laxxxx=laxxxxx'
The content has this value -
Here is my script:
I have tried sending it both the ways, by converting it to base64 and also using inbuild function setRequestBodyFromAttachment
sendAttachment: function(incGr, incSysId, zitoTicketNumber, fileName, attachmentSysId) {
gs.info('Adding attachments');
// var body = 'Text:' + incGr.work_notes.getJournalEntry(1) + ' \n';
var body = 'Text: adding attachments\n';
body += 'id: ' + zitoTicketNumber + '\n';
body += 'Action: correspond \n';
body += 'Attachment: ' + fileName + '\n';
var encodedBody = encodeURIComponent(body);
gs.info('encodedBody' + encodedBody);
var r = new sn_ws.RESTMessageV2('Zito Incident', 'Send Attachment');
r.setStringParameterNoEscape('Body', encodedBody);
r.setStringParameterNoEscape('password', 'labCopaPassword');
r.setStringParameterNoEscape('username', 'labCopa');
r.setStringParameterNoEscape('ticket-id', zitoTicketNumber);
/* var incidentSysId = '';
var attachments = new GlideRecord('sys_attachment');
attachments.addQuery("table_sys_id", incSysId);
attachments.query();
while (attachments.next()) {
var meta = "application_1=";
var StringUtil = new GlideStringUtil();
var sa = new GlideSysAttachment();
var binData = sa.getBytes(attachments);
var data = StringUtil.base64Encode(binData);
gs.info(data);
var allData = meta + data;
// r.setStringParameterNoEscape();
r.setRequestBody(allData);
*/
r.setRequestBodyFromAttachment(attachmentSysId);
r.setRequestBody(encodedBody);
var res = r.execute();
gs.info('response' + res.getBody() + 'statuscode' + res.getStatusCode());
if (res == '' || res == null || res == undefined) {
gs.eventQueue('restresponse', incGr, res.getBody() + 'Could not add attachment to the incident: ' + incGr.number, 'ZITO');
} else {
gs.info(res.getBody());
gs.info(res.getStatusCode());
if (res.getStatusCode() == 200)
return res.getBody();
if (res.getStatusCode() != 200 && res.getStatusCode() != 201 && res.getStatusCode() != 204) {
gs.eventQueue('restresponse', incGr, res.getBody() + '\nReceived error response for ' + incGr.number, 'ZITO');
}
}
},
Here is my HTTP method from rest message:
since it is working fine in the postman, how can the same be leveraged on ServiceNow platform through scripting as well as Test Run in the HTTP Method as shown in the above screenshot?
Regards,
Nithish
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2022 06:38 AM