function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phuc Nguyen 18Phuc Nguyen 18 

Automatically add attachment to email based on field update

I would like to send an email with the attchment that associated with the record when a field is updated on the record.
Thanks,
P
David Zhu 🔥David Zhu 🔥
I would use workflow rules to do that.
Phuc Nguyen 18Phuc Nguyen 18
Hey David,
I did see where I could select the attchment in the email WF rule.
Thanks,
P
David Zhu 🔥David Zhu 🔥
in email template, at the very bottom of the screen, you can attach files.
Phuc Nguyen 18Phuc Nguyen 18
Ah yes.  This attachment would be attached to the current record.  I need to grab the attachment that is associated to the record and email it.
Thanks,
P
David Zhu 🔥David Zhu 🔥
That would be using trigger to handle your requirement.
Phuc Nguyen 18Phuc Nguyen 18
I assumed as much from my research but they all appear to be assoicated to a VF page.  I just need to send the email and the attachment that is already on the opportunity  to a user when the opportunity is won.
Thanks,
P
Phuc Nguyen 18Phuc Nguyen 18
What I am looking for is an apex class.
Phuc Nguyen 18Phuc Nguyen 18
So if I have something like this how do I grab the attachment that is already on the record?
public with sharing class EmailInvoiceAfterApproved {
    public static void email_send(list<Invoice__c> invlist)
	{
		
		List<Messaging.SingleEmailMessage > Email_list=new List<Messaging.SingleEmailMessage >();
		for(Invoice__c inv:invlist)
		{
			if(inv.Approval_Status__c=='Approved')
				{
				Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
       			message.toAddresses =new String[] {'junk@.com','junk20@gmail.com'};  **Can I get the email field from the record instead???***
        		message.setSubject('Invoice Approved');
				String Body_data='';
				Body_data=inv.Name;
				message.sethtmlBody(Body_data);
				Email_list.add(message);
				Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
				}
		}	
		
	}
}