The CreatorCon Call for Content is officially open! Get started here.

Unable to send attachment to 3rd party

surbhi_123
Tera Expert

We are integrating with Zendesk. As per their process first I need to get the token and I need to pass the token in the request body. I am trying with below code.

 

(function executeRule(current, previous /*null when async*/ ) {

var file = '';
var token = '';
var sysid = '';
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true^state=4^u_state_substatus=42^u_vendor=2b24fd67db8538d076b9ee71ca96199a");
gr.addQuery("sys_id", current.table_sys_id);
gr.query();
if (gr.next()) {
file = current.file_name;
var r = new sn_ws.RESTMessageV2('rest','Post Attachment');
r.setEndpoint("https://xx.com/api/v2/uploads?filename="+file);
r.setHttpMethod("POST");
r.setRequestHeaders("Content-Type", "image/png");
r.setRequestBody(file);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
token = jsonObject.upload.token;
var ticket = gr.u_vendor_ticket_number;
var ticketUpdate = new sn_ws.RESTMessageV2('int rest','Get token');
ticketUpdate.setEndpoint("https://xx.com/api/v2/requests/" + ticket);
ticketUpdate.setRequestBody('{"request":{"comment":{"body": "Attempting to Add and attachment to the ticket.", "uploads": ["' + token + '"]}}}');
var s = ticketUpdate.execute();
var responseBody1 = response.getBody();
var httpStatus1 = response.getStatusCode();
var jsonObject1 = JSON.parse(responseBody1);
gs.log("Attachment upload status " + httpStatus1);
gs.log("Attachment response body after token" + responseBody1);
}
})(current, previous);

 

I am getting error as 

surbhi_123_0-1692868292450.png

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@surbhi_123 

Did you try to test it via postman first?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

yes, in postaman it is working properly