SEND ATTACHMENT THROUGH REST API

nandika 1
Tera Contributor

Hi 

I want to send attachment through Rest Api.can anyone please help me on it .Kindly guide on how to write the rest api and business rule for it

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @ nandika 1 ,

Use below script in business rule  to sent attachment to third party api

 

 

var attachmentid = '';
		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_sys_id", current.getValue('sys_id'));
		gr.query();
		if (gr.next()) {
			attachmentid = gr.getValue('sys_id');//getting attachent sys_id
		}
		var tosend = JSON.stringify(body);
		var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
		request.setRequestHeader("Accept", "application/json");
		request.setRequestHeader('Content-Type', 'application/json');
		request.setRequestBody(tosend);
		request.setRequestBodyFromAttachment(attachmentid);//setting attachment ot thirdparty
		request.setWorkflow(false); //attempt to stop loops
		var response = request.execute();
		var responseBody = response.getBody();
		var json = responseBody;
		var obj = JSON.parse(json); //define JSON parsing for the response JSON file to decode
	

 

 

Mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand


 

View solution in original post

5 REPLIES 5

Anand Kumar P
Giga Patron
Giga Patron

Hi @ nandika 1 ,

Use below script in business rule  to sent attachment to third party api

 

 

var attachmentid = '';
		var gr = new GlideRecord("sys_attachment");
		gr.addQuery("table_sys_id", current.getValue('sys_id'));
		gr.query();
		if (gr.next()) {
			attachmentid = gr.getValue('sys_id');//getting attachent sys_id
		}
		var tosend = JSON.stringify(body);
		var request = new sn_ws.RESTMessageV2('Ottos test instance', 'Otto_Post');
		request.setRequestHeader("Accept", "application/json");
		request.setRequestHeader('Content-Type', 'application/json');
		request.setRequestBody(tosend);
		request.setRequestBodyFromAttachment(attachmentid);//setting attachment ot thirdparty
		request.setWorkflow(false); //attempt to stop loops
		var response = request.execute();
		var responseBody = response.getBody();
		var json = responseBody;
		var obj = JSON.parse(json); //define JSON parsing for the response JSON file to decode
	

 

 

Mark it as helpful and solution proposed if it serves your purpose.

Thanks,

Anand


 

Hi @Anand Kumar P 

could you please check this .It is not working . I tried for both incident and attachment table 

nandika1_0-1702025163346.png

nandika1_1-1702025221588.png

 

Hi @ nandika 1 ,

Put logs and check what exactly you are getting

	var responseBody = response.getBody();

var getstatus=response.getStatusCode();

gs.log('Statuscode'+getstatus);

gs.log('ResponseBody'+responseBody); 

Thanks,

Anand

Hi @Anand Kumar P I think its issue with my rest .can you please tell me how to solve this issue community7.pngcommunity8.pngcommunity 9.png