Sending attachments to third party system

Nithish1
Tera Guru

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 - 

Text: Ticket Body
id: 20220505002
Action: correspond
Attachment: test attachment

find_real_file.png

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:

find_real_file.png

find_real_file.png

 

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

1 REPLY 1

Nishu Saini
Kilo Guru
Kilo Guru
Hi, Please check below thread it may help you Link - https://community.servicenow.com/community?id=community_question&sys_id=6b30cf61db98dbc01dcaf3231f961963 Mark helpful/ correct if applicable. Regards Nishu