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 getEntityAttachments
attribute 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>{'[email protected]','[email protected]'};
List<String> ccAddresses = new List<String>{'[email protected]','[email protected]'};
//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 getEntityAttachments
attribute 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>{'[email protected]','[email protected]'};
List<String> ccAddresses = new List<String>{'[email protected]','[email protected]'};
//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=([email protected], [email protected]);getCcAddresses=([email protected], [email protected]);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);
?