how to insert attachment(inbound) for incident using attachment API

String
Kilo Sage

In service now ,I can navigate to Rest API explorer and check the attachment API but  we have to give table_name ,table_sys_id and file_name manually .Request body is binary 

How to use the attachment API when we receive Inbound transaction (attachment )   Json as below

{

  "number": "INC12345",

  "tbl_name": "incident",

  "att_payload" : "VGVzdCBhdHRhY2htZW50IGRhdGE=",

  "att_name" : "Test.txt",

  "att_cont_type" : "text/plain"

}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kiran,

you can get the base64 encoded data by parsing the json response

then you can use GlideSysAttachment.write method to create attachment

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideSysAttach...

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_ws-namespace/c_RESTMessage...

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Kiran,

you can get the base64 encoded data by parsing the json response

then you can use GlideSysAttachment.write method to create attachment

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideSysAttach...

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/sn_ws-namespace/c_RESTMessage...

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@kiranchand 

Sample script below

I have hardcoded the json; but you will get the json response once you consume REST Endpoint

var json = '{"number":"INC12345","tbl_name":"incident","att_payload":"VGVzdCBhdHRhY2htZW50IGRhdGE=","att_name":"Test.txt","att_cont_type":"text/plain"}';

var parser = JSON.parse(json);

var base64String = parser.att_payload;

var tableName = parser.tbl_name;

var fileName = parser.att_name;

var contentType = parser.att_cont_type;

var number = parser.number;

var attachment = new GlideSysAttachment();

var gr = new GlideRecord(tableName);

gr.addQuery('number', number);
gr.query();
if(gr.next()){

var attachment_sys_id = attachment.writeBase64(gr, fileName, contentType, base64String );

gs.info("system attachment id:" + attachment_sys_id);

}

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@kiranchand 

Glad to know that it helped.

If my response helped you please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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