Insert attachment in Servicenow from 3rd party with base64 and REST API

sunilsafare
Mega Guru

Hello experts,

I have a requirement to insert attachments in incident record from 3rd party tool with REST API. They dont have physical location and they are receiving as base64 data from other tool. So they need to use Snow rest api and attach it to target incident record. I have gone through Attachment API provided by Servicenow. But it did not help as it expects the physical location of the file in both of the below methods.

POST /now/attachment/file

POST /now/attachment/upload

Also, I have tried below URL but not working.

http://www.john-james-andersen.com/blog/service-now/generate-attachments-in-servicenow-via-rest.html

Please let me know if you have implemented this.

1 ACCEPTED SOLUTION

sunilsafare
Mega Guru

I have achieved using below.

 

Created a scripted rest API with the below script and successfully created an attachment.

var StringUtil = new GlideStringUtil();
var value = StringUtil.base64DecodeAsBytes(<base64data>);
var attachment = new GlideSysAttachment();
attachment.write(<tablename>,<record_sys_id>,<filename>,'', value);

 

Thanks,

Sunil Safare

View solution in original post

3 REPLIES 3

AirSquire
Tera Guru

I havn't used Attachment API yet. But, can't you just hold that Base64 data in a staging table(using normal POST) and from there use below script to attach

var StringUtil = new GlideStringUtil();
var value = StringUtil.base64DecodeAsBytes(<base64data>);
var attachment = new Attachment();
attachment.write(<tablename>,<record_sys_id>,<filename>,'', value);

Regards
Air

sunilsafare
Mega Guru

I have achieved using below.

 

Created a scripted rest API with the below script and successfully created an attachment.

var StringUtil = new GlideStringUtil();
var value = StringUtil.base64DecodeAsBytes(<base64data>);
var attachment = new GlideSysAttachment();
attachment.write(<tablename>,<record_sys_id>,<filename>,'', value);

 

Thanks,

Sunil Safare

Yes, that didn't clicked my mind. Glad to know it helped.

Regards
Air