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

attachment api in scripted rest api

Sneha39
Mega Guru

Hi,

I have to create attachment api by using scripted rest api.

Please help me with example.

Thanks

6 REPLIES 6

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Sneha,



The below script should work.



(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
   
var hdrs = {},
  attachment_sys_id
= '1852fd52471321009db4b5b08b9a71a9';

  hdrs
['Content-Type'] = 'image/jpeg';
  response
.setStatus(200);
  response
.setHeaders(hdrs);

   
var writer = response.getStreamWriter();
   
var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
  writer
.writeStream(attachmentStream);
})(request, response);



Refer Scripted REST API example - streaming file attachments



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Hi Antin,



This is for only one sys_id ? What if I have upload multiple documents which inculdes excel aswell moreover how will it get the incident for which I have to upload the document ?



Thanks


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Sneha,



If you want upload documents into ServiceNow, please refer the below. You may create a Scripted API and use the Attachment API to handle single or multiple files. The script I gave above is to download/GET attachments from ServiceNow.



Attachment API - POST /now/attachment/file



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


Hi @antin_s / @Ankur Bawiskar,

 

The above link is not opening as I also have the requirement to upload the attachment in an incident ticket with its other field values also.

 

Request you to please guide me in this

 

Regards,

Shriyansh