Prevent duplicate image attachments from email replies

Marcel H_
Tera Guru

I'm trying to solve an issue with inbound email replies and the images that are attached to the target record each time.

Currently I have inbound actions that are properly creating or updating a record when someone replies to an email thread, and email properties set so that each reply email is stripping off the previous entries in the thread and updating Additional Comments with only the latest reply. The problem that I'm having is that even though the previous replies in the email thread are stripped out effectively, any inline pictures (signature logos, pictures pasted into the email body, etc.) get duplicated as attachments each time there is a reply.

As an example, in the pictures below, there was one record where a few people replied to an email thread that originally had 4 photos and 1 signature logo. There are now 52 attachment on the record, which are all duplicates of the pics and the logo:

find_real_file.png

find_real_file.png

I had previously come across a Business Rule script that was meant to prevent exact duplicate attachments to a record, but it doesn't seem to work the way that I expected. This is a late running (order 1,000) on before BR on the sys_attachment table:

function onBefore(current, previous) {

     var attach = new GlideRecord('sys_attachment');

		attach.addQuery('table_sys_id', current.table_sys_id);
		attach.addQuery('table_name', current.table_name);
		attach.addQuery('file_name', current.file_name);
		attach.addQuery('content_type', current.content_type);
		attach.addQuery('size_bytes', current.size_bytes);
		attach.query();

	if(attach.next()){
	current.setAbortAction(true);
	}

}

 

Has anyone else come across this issue and found a way to prevent duplicate attachments?

 

 

 

 

1 ACCEPTED SOLUTION

Just did a quick try and verified that the bin compare logic works well.

1. Upload 2 same image to a record.

2. Upload a resized image to the same record.

3. Run background script to compare the binary data.

 

var gr1 = new GlideRecord('sys_attachment');
gr1.get('e55cdb554fa02300df6d4ebf9310c753');

var gsa = new GlideSysAttachment();
var binData = gsa.getBytes(gr1);
var strData = Packages.java.lang.String(binData);

var gr2 = new GlideRecord('sys_attachment');
gr2.get('da8f9bd54fa02300df6d4ebf9310c70a');
var gsa1 = new GlideSysAttachment();
var binData1 = gsa1.getBytes(gr2);
var strData1 = Packages.java.lang.String(binData1);

gs.print('Equal = '+(strData == strData1));

 

Looks promising. 

View solution in original post

2 REPLIES 2

Bryan Tay3
Mega Guru

hi Marcel, 

this seems interesting.

https://community.servicenow.com/community?id=community_question&sys_id=afbc8fe5db9cdbc01dcaf3231f961970

has a solution to compare the binary data that you can try?

hope this helps.

Just did a quick try and verified that the bin compare logic works well.

1. Upload 2 same image to a record.

2. Upload a resized image to the same record.

3. Run background script to compare the binary data.

 

var gr1 = new GlideRecord('sys_attachment');
gr1.get('e55cdb554fa02300df6d4ebf9310c753');

var gsa = new GlideSysAttachment();
var binData = gsa.getBytes(gr1);
var strData = Packages.java.lang.String(binData);

var gr2 = new GlideRecord('sys_attachment');
gr2.get('da8f9bd54fa02300df6d4ebf9310c70a');
var gsa1 = new GlideSysAttachment();
var binData1 = gsa1.getBytes(gr2);
var strData1 = Packages.java.lang.String(binData1);

gs.print('Equal = '+(strData == strData1));

 

Looks promising.