I am writing code to send emails to contacts via Apex using templates. My issue is that I need to be able to copy these emails to various other users that are going to be determined at run time. This is not currently allowed: (see here) http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

Optional. A list of blind carbon copy (BCC) addresses. The maximum allowed is 25. This argument is allowed only when a template is not used.

Optional. A list of carbon copy (CC) addresses. The maximum allowed is 25. This argument is allowed only when a template is not used.

Does anyone have any ideas for a workaround?

Thank you!

1 Answer 1


I use templates and can easily set additional to: addresses using email.setToAddresses(toRecipients); even though the doc says it is not supported.

Have you actually tried to set cc: adddresses and additional to: addresses with a template and seen it fail? I'm not 100% convinced the doc is accurate .

I just tried using anonymous APEX and V.30 using this code and it works fine in both to: and cc: lists with a call such as this:

Util.sendTemplatedEmail(new List<String> {'crop1645@foo.com'},
     new List<String>{'crop1645@bar.com'},
      [select id from Foo__c limit 1][0].id,null,false);

This really makes me think the doc is wrong.

//  -------------------------------------------------------------------------
//  sendTemplatedEmail
//  -------------------------------------------------------------------------
public static void sendTemplatedEmail(String[] toRecipients, String[] ccRecipients, String templateParmName, ID targetObjId, Id whatId, ID orgWideEmailId, Boolean saveAsActivity ) {
    //  templateId  must be ID of an Email template
    //  targetObjId must be a Contact Id -- also used in merge fields of template recipient.xxxx
    //  whatId      must be an SObject that is used in the merge fields of the template relatedTo.xxxx
    //  fromId      if non null, use current user, otherwise, use this ID (most likely an org wide no reply id)

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    String templateApiName = EnvironmentVariable.getString(templateParmName);
    Id templateId;  
    try {templateId = [select id, name from EmailTemplate where developername = : templateApiName].id;}
    catch (Exception e) {
        throw new MyException ('[UTIL-06] Unable to locate EmailTemplate using name: ' + templateApiName + 
                                    ' as indicated in Custom Settings ' + templateParmName);

    email.setSaveAsActivity(saveAsActivity);            // save email as activity on the targetObjId (i.e. Contact)
    try {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    catch (EmailException e) {throw new MyException('[UTIL-07] sendTemplatedEmail error. ' + e.getMessage());}

  • 1
    Thank you! I didn't even think of trying it! It worked perfectly.
    – hgolov
    Apr 9, 2014 at 16:06
  • 1
    good to know - I've noted in doc feedback form that doc is incorrect; if you would be so kind as to report on same feedback form, maybe the 'groundswell' will see the doc get changed. The reason I discovered all this is that I coded the solution (a few years ago) before reading the doc - had I read the doc first, I probably wouldn't have tried.
    – cropredy
    Apr 9, 2014 at 19:21
  • good to know.last time i was not able to do cc but now I can.I am not sure when sfdc update this.
    – unidha
    Oct 19, 2015 at 9:18

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.