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
cambart14cambart14 

Emailing Multiple Attachments with Apex Trigger

Hi Experts,

I am trying to get the below trigger to work for multiple attachments (users may attached 2 or 3 files to the Special_Product_Request__c object).  If there is more than one attachment, this trigger will fail (even with small files).  Your assistance is much appreciated!

trigger EmailAttachments on Special_Product_Request__c (after insert, after update) {

for(Special_Product_Request__c spr : trigger.new){
    if (spr.Engineering_Group__c == 'ASI' && spr.SPR_Stage__c == '2 - Marketing Review' && (spr.SPR_Stage__c != Trigger.oldMap.get(spr.Id).SPR_Stage__c)) {
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       spr = [SELECT id, Opportunity.Name, Product_Managerlookup__c.Email, Opportunity.Owner.Name, Opportunity.Account.Name FROM Special_Product_Request__c WHERE Id = :spr.Id];                    

        // create email content
        String Name = spr.Opportunity.Name; 
        String OwnerName = spr.Opportunity.Owner.Name;
        String Account = spr.Opportunity.Account.Name;
        String subject = 'Special Product Request Attachments - '+Name;
        email.setSubject(subject);
        
        List<Messaging.EmailFileAttachment> attachmentList = new List<Messaging.EmailFileAttachment>();

        String line1 = 'Please see attached for special product request files related to opportunity: '+Name+'.  \r\n';
        String line2 = 'Opportunity Owner: '+OwnerName+'.  \r\n';
        String line3 = 'Account Name: '+Account+'.  \r\n';
        String body = line1 + line2 + line3; 
        email.setPlainTextBody(body);

		String ProductManager = spr.Product_Managerlookup__c.Email;
        email.setToAddresses(new String[]{ProductManager});

            // fetch attachments for Special_Product_Request__c
            Attachment att = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId = : spr.id];

   // List of attachments handler
   Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

   {
     // Create the email attachment
     efa.setFileName(att.Name);
     efa.setBody(att.body);
     efa.setContentType(att.ContentType);
     efa.setInline(false);
	 attachmentList.add(efa);
    
   }

    // Attach files to email instance
    email.setFileAttachments(attachmentList);
    Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
    }

 }  
}

Thanks,
cambart14
 
Best Answer chosen by cambart14
Kevin Chiles 930Kevin Chiles 930
I found this answer that helped me with the same issue:

https://developer.salesforce.com/forums/?id=906F0000000904xIAA

Let me know if that helps!

All Answers

Kevin Chiles 930Kevin Chiles 930
I found this answer that helped me with the same issue:

https://developer.salesforce.com/forums/?id=906F0000000904xIAA

Let me know if that helps!
This was selected as the best answer
Kevin Chiles 930Kevin Chiles 930
Hello!

Are you still having issues?  I solved this one with this blog: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000MMtBIAW

It's the complete code you just have to switch out your objects.  Thanks!


Zammyblammymataz