1

Here's the issue:

I'm trying to use the method Messaging.SingleEmailMessage.setEntityAttachments(List<Id>) in a class, but it won't compile. However, when I run the same code in anonymous apex, the code compiles, runs, and the getEntityAttachmentsattribute of the SingleEmailMessage is set as expected.

Here's the class I've written to test this issue. It will only compile if you keep the line emailToSend.setEntityAttachments(attachmentIds);commented out:

public with sharing class MessageAttachmentsTest {
public MessageAttachmentsTest() {

    List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();

    for(integer i = 0; i<5;i++){

        Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();

        String subject = 'Subject';
        String body = 'Body';
        List<Id> attachmentIds = new List<Id>{'068630000002jOSAAY', '068630000002jOXAAY'};
        Id whatId = 'a0f63000000aF2EAAU';
        List<String> bccAddresses = new List<String>{'bcc@example.com','bcc2@example.com'};
        List<String> ccAddresses = new List<String>{'cc@example.com','cc2@example.com'};


        //set email values
        emailToSend.setSubject(subject);
        emailToSend.setHTMLBody(body);
        emailToSend.setWhatId(whatId);
        emailToSend.setBCCAddresses(bccAddresses);
        emailToSend.setCcAddresses(ccAddresses);
        //********************WILL NOT COMPILE IF THE NEXT LINE IS UNCOMMENTED OUT****************
        //emailToSend.setEntityAttachments(attachmentIds);
        emailList.add(emailToSend);

    }

    System.debug(emailList[0]);}

}

Here's the anonymous apex, running on the same API version, that compiles, runs, and successfully sets the getEntityAttachmentsattribute as can be seen in the system debug log:

        List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();

    for(integer i = 0; i<5;i++){

        Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();

        String subject = 'Subject';
        String body = 'Body';       
        List<Id> attachmentIds = new List<Id>{'068630000002jOSAAY', '068630000002jOXAAY'};
        Id whatId = 'a0f63000000aF2EAAU';
        List<String> bccAddresses = new List<String>{'bcc@example.com','bcc2@example.com'};
        List<String> ccAddresses = new List<String>{'cc@example.com','cc2@example.com'};


        //set email values
        emailToSend.setSubject(subject);
        emailToSend.setHTMLBody(body);
        emailToSend.setWhatId(whatId);
        emailToSend.setBCCAddresses(bccAddresses);
        emailToSend.setCcAddresses(ccAddresses);
        emailToSend.setEntityAttachments(attachmentIds);
        emailList.add(emailToSend);

    }

    System.debug(emailList[0]);

Excerpt from the debug log:

14:28:33:005 USER_DEBUG [27]|DEBUG|Messaging.SingleEmailMessage[getBccAddresses=(bcc@example.com, bcc2@example.com);getCcAddresses=(cc@example.com, cc2@example.com);getCharset=null;getEntityAttachments=(068630000002jOSAAY, 068630000002jOXAAY);getFileAttachments=null;getHtmlBody=Body;getInReplyTo=null;getOptOutPolicy=SEND;getOrgWideEmailAddressId=null;getPlainTextBody=null;getReferences=null;getTargetObjectId=null;getTemplateId=null;getToAddresses=null;getWhatId=a0f63000000aF2EAAU;isTreatBodiesAsTemplate=false;isTreatTargetObjectAsRecipient=true;isUserMail=false;]

What I've already tried:

  • Using the latest version API for my class (34.0)
  • Checked to make sure none of my classes are named the same as system classes
  • Tried sending the list as a string instead of a list of ids
  • Checked to make sure content version exists

Why won't the class compile with emailToSend.setEntityAttachments(attachmentIds);?

  • 1
    You're not giving us the most critical piece of info. What is the error you're getting? – Sebastian Kessel Nov 7 '16 at 19:44
4

The newest API Version is not 34.0, but rather 38.0, in which this method will compile. It looks like this method was added in API Version 35.0, as it compiles for any version 35.0 and above. Simple class to demonstrate:

public class EmailAttachmentDemo
{
    public static void buildEmail()
    {
        new Messaging.SingleEmailMessage().setEntityAttachments(new List<Id>());
    }
}

In version 35.0 and up, it compiles, but in version 34.0, it gives the compile error:

Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setEntityAttachments(List)

| improve this answer | |
  • Oh damn that worked! You have earned my love! Drink's on me the next time you're in NYC! – Shmuel Kamensky Nov 7 '16 at 19:53

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.