Converting attachment to base64 in Flow Designer.

Dotychczas
Mega Guru

Hello. I have to take attachments from incident convert them to base64 to send them in xml message using rest. I already fetch all attachments from incident but now i have to convert them preferably one by one to base64 to use it in xml file that I send to other system. Is it possible to do that? Thanks!

1 ACCEPTED SOLUTION

Geoff_T
Mega Sage

Hi,

Here is the logic I use to get both the attachment name and attachment data from a RITM record.

function genAttachementName()
{
	var fileName = "";
	
	var sa = new GlideRecord('sys_attachment');
	sa.addQuery('table_name', 'sc_req_item');
	sa.addQuery('table_sys_id', current.sys_id);
	sa.addQuery('size_bytes', '<', 5242880);
	sa.setLimit(1);
	sa.query();

	while(sa.next())
	{
		fileName = sa.file_name.toString();
	}
	
	return fileName;
}

function genAttachementData()
{
	var b64attachment = "";
	var allAttachments = "";
	var attachmentData = "";

	var sa = new GlideRecord('sys_attachment');
	sa.addQuery('table_name', 'sc_req_item');
	sa.addQuery('table_sys_id', current.sys_id);
	sa.addQuery('size_bytes', '<', 5242880);
	sa.setLimit(1);
	sa.query();
	
	while(sa.next())
	{
		var attachmentIS = new GlideSysAttachmentInputStream(sa.sys_id);
		var byteArrayOS = new Packages.java.io.ByteArrayOutputStream();

		attachmentIS.writeTo(byteArrayOS);
		b64attachment = GlideBase64.encode(byteArrayOS.toByteArray());
		attachmentData = b64attachment;
	}
	
	return attachmentData;
}

 

Hopefully you can use this for your scenario. Let me know if it helps.

Geoff

View solution in original post

2 REPLIES 2

Geoff_T
Mega Sage

Hi,

Here is the logic I use to get both the attachment name and attachment data from a RITM record.

function genAttachementName()
{
	var fileName = "";
	
	var sa = new GlideRecord('sys_attachment');
	sa.addQuery('table_name', 'sc_req_item');
	sa.addQuery('table_sys_id', current.sys_id);
	sa.addQuery('size_bytes', '<', 5242880);
	sa.setLimit(1);
	sa.query();

	while(sa.next())
	{
		fileName = sa.file_name.toString();
	}
	
	return fileName;
}

function genAttachementData()
{
	var b64attachment = "";
	var allAttachments = "";
	var attachmentData = "";

	var sa = new GlideRecord('sys_attachment');
	sa.addQuery('table_name', 'sc_req_item');
	sa.addQuery('table_sys_id', current.sys_id);
	sa.addQuery('size_bytes', '<', 5242880);
	sa.setLimit(1);
	sa.query();
	
	while(sa.next())
	{
		var attachmentIS = new GlideSysAttachmentInputStream(sa.sys_id);
		var byteArrayOS = new Packages.java.io.ByteArrayOutputStream();

		attachmentIS.writeTo(byteArrayOS);
		b64attachment = GlideBase64.encode(byteArrayOS.toByteArray());
		attachmentData = b64attachment;
	}
	
	return attachmentData;
}

 

Hopefully you can use this for your scenario. Let me know if it helps.

Geoff

Hi @Dotychczas - did this help or resolve your issue? Please let me know if I can try and help further.

Geoff