Skip to main content
more experiments
Source Link
cropredy
  • 73.3k
  • 8
  • 128
  • 283

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 . Looking at my code, my class that does

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> {'[email protected]'},
     new List<String>{'[email protected]'},
     'fooTemplate',UserInfo.getUserId(),
      [select id from Foo__c limit 1][0].id,null,false);

This really makes me think the send emaildoc is at V18wrong.0; maybe the restrictions are an introduced reality

//  -------------------------------------------------------------------------
//  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.setToAddresses(toRecipients);
    email.setCcAddresses(ccRecipients);
    email.setTargetObjectId(targetObjId);
    email.setWhatId(whatId);
    email.setorgWideEmailAddressId(orgWideEmailId);
    email.setTemplateId(templateId);
    email.setSaveAsActivity(saveAsActivity);            // save email as activity on the targetObjId (i.e. Contact)
    try {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        return;
    }
    catch (EmailException e) {throw new MyException('[UTIL-07] sendTemplatedEmail error. ' + e.getMessage());}
    
}

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 . Looking at my code, my class that does the send email is at V18.0; maybe the restrictions are an introduced reality

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> {'[email protected]'},
     new List<String>{'[email protected]'},
     'fooTemplate',UserInfo.getUserId(),
      [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.setToAddresses(toRecipients);
    email.setCcAddresses(ccRecipients);
    email.setTargetObjectId(targetObjId);
    email.setWhatId(whatId);
    email.setorgWideEmailAddressId(orgWideEmailId);
    email.setTemplateId(templateId);
    email.setSaveAsActivity(saveAsActivity);            // save email as activity on the targetObjId (i.e. Contact)
    try {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        return;
    }
    catch (EmailException e) {throw new MyException('[UTIL-07] sendTemplatedEmail error. ' + e.getMessage());}
    
}
Source Link
cropredy
  • 73.3k
  • 8
  • 128
  • 283

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 . Looking at my code, my class that does the send email is at V18.0; maybe the restrictions are an introduced reality