+ Start a Discussion
Abby BeckerAbby Becker 

Uncheck custom field after Apex trigger checks it and workflow email alert sent?

Hi all,

New "admineloper" here. I have an Apex trigger that checks a custom field "Has_Attachment__c" when an attachment is added to a record, then a workflow rule that sends an email alert to the record owner, notifying them that an attachment has been added.  Right now, it works great the FIRST time an attachment is added. Any further attachments, don't generate emails.

How can I go about sending an email for every added attachment? Is there a way to uncheck the box after the trigger has executed and the email has been sent? Below is my Apex trigger.
 

trigger AttachmentAdded on Attachment (before insert) {	
 
	list<Customer_Tool__c> ctList = new List<Customer_Tool__c>();
	list<Customer_Tool__c> ctListAdded = new List<Customer_Tool__c>();
	Set<id> ctID = new Set<id>();

	for(Attachment at : trigger.new) {
		ctID.add(at.parentid);
	}

	ctList = [Select id, Has_Attachment__c from Customer_Tool__c where id in: ctID];
		if(ctList!=null && ctList.size()>0){
			for(Customer_Tool__c ct : ctList) {
				ct.Has_Attachment__c = true;
				ctListAdded.add(ct);
			}
			update ctListAdded;
		}
}
 

Very new to development - so any advice/help/guidance would be greatly appreciated!

Thanks,

Best Answer chosen by Abby Becker
@anilbathula@@anilbathula@
Hi Abby Becker,

In the same work flow add a field update to make the Has_Attachment__c to false.
So when every an attachment is inserted it makes true and sends email, after that it will make the same field to false.
Which will be again updated when an attachment is linked.

Thanks
Anil.B

All Answers

@anilbathula@@anilbathula@
Hi Abby Becker,

In the same work flow add a field update to make the Has_Attachment__c to false.
So when every an attachment is inserted it makes true and sends email, after that it will make the same field to false.
Which will be again updated when an attachment is linked.

Thanks
Anil.B
This was selected as the best answer
Abby BeckerAbby Becker
Why didn't I think of that?! Thank you! Problem solved.