How to send attachments from one instance another instance through Rest API?

Manasa Allu
Tera Contributor

We have created an incident from one instance to another instance using Rest API. We need to copy attachments from one instance to another instance using rest API.

 

Could you please provide the possible ways?

2 REPLIES 2

Saurabh Gupta
Kilo Patron

Hi,
You can do send the attachment as per below code

var gr = new GlideRecord('sys_attachment');
		gr.addQuery('table_sys_id', current.sys_id);
		gr.addQuery('table_name', 'incident');
		gr.query();
		while (gr.next()) {
			var StringUtil = new GlideStringUtil();
			var sa = new GlideSysAttachment();
			var binData = sa.getBytes(gr);
			encData = StringUtil.base64Encode(binData);
			name = gr.getValue('file_name');
			totalsize = +gr.size_bytes; //this queries for the attachments for the current record

			u_attachment.filename = name;
			u_attachment.data = encData;
			var totalsize_MB = totalsize / 1000000; //converts the total size to MB
			if (totalsize_MB > 10) {
				gs.log('Ignoring sending of attachment size exceeds 10MB.'); // error message
				var str3 = "There is an attachment : "+ " "+ u_attachment.filename +" " + "of size more than 10 MB which is ignored";
				r.setStringParameterNoEscape('comments', str3);
			}
			else
			{
				ar.push(u_attachment);
				u_attachment = {};
			}
		}

 

To get the data from any other source

var attach = new GlideRecord('sys_attachment');
					attach.addQuery('table_sys_id', gr.sys_id);
					attach.addQuery('table_name', 'incident');
					attach.addQuery('file_name', INCfilename1);
					attach.query();
					if (!attach.next()) {
						var StringUtil1 = GlideStringUtil;
						var value1 = StringUtil1.base64DecodeAsBytes(INCattachment1);
						var attachment1 = new Attachment();
						attachment1.write('incident', gr.sys_id, INCfilename1, INCcontenttype1, value1);
					}

Thanks and Regards,

Saurabh Gupta

You can also explore Attachment API

SaurabhGupta_0-1672167008247.png

 

 

 


Thanks and Regards,

Saurabh Gupta