Ankur Bawiskar
Tera Patron

Often, we need to send only the latest attachment via email. However, using the "Include attachment" checkbox in notifications sends all attachments, making it impossible to send just the latest or a specific attachment.

To address this, follow these steps:

1) Create business rule which is After Insert on sys_email with correct condition for table name

AnkurBawiskar_0-1736429521127.png

 

 

2) Use the below script to pick latest attachment

(function executeRule(current, previous /*null when async*/) {

	var s_Attach = new GlideRecord('sys_attachment');
	s_Attach.addEncodedQuery('table_sys_id=' + current.instance);
	s_Attach.orderByDesc('sys_created_on');
	s_Attach.setLimit(1);
	s_Attach.query();
	if(s_Attach.next()){
		var rec = new GlideRecord('sys_email_attachment');
		rec.initialize();
		rec.attachment = s_Attach.sys_id;
		rec.file_name = s_Attach.file_name;
		rec.source = 'notification';
		rec.content_disposition = 'attachment';
		rec.email = current.sys_id;
		rec.insert();
	}

})(current, previous);
 
Comments
RixelB
Tera Contributor

It is not a good practice to create business rules in the [sys_email] table from an architecture perspective.

SSantacroce
Tera Contributor

Hi @Ankur Bawiskar ,

What did you include in the filter condition for the BR?

The image is all blurry so I can't read it.

Thanks!

Version history
Last update:
‎01-09-2025 05:34 AM
Updated by:
Contributors