2

I am trying to modify a trigger to add attachments to an email that is sent when a checkbox is checked. The attachments will come from the notes and attachments section of the Opportunity. I continue to get the "Method does not exist or incorrect signature:[Messaging.EmailFileAttachment}.add(Messaging.EmailFileAttachment)" Error. Below is my trigger. Any help? Thanks!

trigger Send_To_PropSub on Opportunity (before insert, before update) {

for(Opportunity opp : trigger.new){
    if (opp.Sent_to_Prop_Sub__c == 'False') {
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       opp = [SELECT id, Name, Brand_Name__c, Business_Unit__c, Business_Units__c FROM Opportunity WHERE Id = :opp.Id];                    

        // create email content
        String Name = opp.Name; 
        String Brand = opp.Brand_Name__c;
        String BusinessUnit = opp.Business_Units__c;
        String subject = 'New Proposal ';
        email.setSubject(subject);


        String line1 = 'Name:'+Name+'+';
        String line2 = 'Brand:'+Brand+'+';
        String line3 = 'Business Unit: '+BusinessUnit+'+';
        String body = line1 + line2 + line3; 
        email.setPlainTextBody(body);


        email.setToAddresses(new String[]{'[email protected]'});
        if(email != null){
            Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
            opp.Sent_to_Prop_Sub__c = True; 

            // fetch attachments for Opportunity
            Attachment att = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId = : opp.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);
     efa.add(efa);
   }

    // Attach files to email instance
    email.setFileAttachments(efa);
          }

 }  
}
}
3
  • What is this line supposed to be doing? efa.add(efa); From the docs, there doesn't seem to be any method add on Messaging.EmailFileAttachment Jun 2, 2015 at 17:22
  • The trigger I was trying to modify referred to a list of attachments, and I believe that was the line that added the list. What is the best way to create a method add on Messaging.EmailFileAttachment? Jun 2, 2015 at 17:39
  • It's a Salesforce-defined class, you can't create a method on it. The error being thrown means that you're trying to call a method that doesn't exist, either by name (in this case) or by incorrect types/parameters. Jun 2, 2015 at 18:00

2 Answers 2

2

Replace the line efa.add(efa); with the following:

List<Messaging.EmailFileAttachment> attachmentList = new List<Messaging.EmailFileAttachment>();
attachmentList.add(efa);

And then change email.setFileAttachments(efa); to:

email.setFileAttachments(attachmentList);

You also have the code where the email is actually sent before you attach any attachments...

Also as an aside, your trigger needs to be bulkified. You're running 2 queries per record that the trigger is executing on. This is really undesirable. Remember that the governor limit (synchronous) for SOQL queries in an execution context is currently 100 - if this trigger is activated by an event with more than ~50 records, or if some other process in the execution context uses any queries, the whole thing will fail. I recommend doing a search on trigger bulkification, there are tons of resources with walkthroughs.

1
  • Well, my syntax errors are gone, thanks Brian. I don't think there will more than 10 of these a day, and it's probably going to happen one at a time for now- but that is good to keep in mind! Thanks again! Jun 2, 2015 at 18:25
0

Send Email After if condition because of that it attach attachment to the email. Insert following lines after if condition to send attachment.

 email.setToAddresses(new String[]{'[email protected]'});
 Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .