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[]{'lisa.thornsberry@xcenda.com'});
        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);
          }

 }  
}
}
  • 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 – Brian Mansfield Jun 2 '15 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? – Lisa Thornsberry Jun 2 '15 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. – Brian Mansfield Jun 2 '15 at 18:00
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.

  • 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! – Lisa Thornsberry Jun 2 '15 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[]{'lisa.thornsberry@xcenda.com'});
 Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.