0

I have this Invocable method which I will be calling from Process builder. When I sending email from here, I am not able to see the merge fields in the email body. This is a custom object, upon checking the few fields I am calling this apex method to send email to Authorised contact of this custom object.

Kindly let me know what value/Id shall i set in WHATID to make merge fields in this custom object work in email body. i.e. {!customObj.ApplicationNumber}

@InvocableMethod
public static void Sendnotification(List<application__c> appidlist)
{
  Id appid = appidlist[0].Id;

  String EMAIL_TEMPLATE = 'Email_temp_Notification';
  //To get the contact email 
  Contact appInitiator = ApplicationService.getAppInitiator(appid);

    EmailTemplate et = [SELECT Id,Subject,HtmlValue FROM EmailTemplate WHERE DeveloperName =:EMAIL_TEMPLATE];
    List<string> toAddress = new List<string>();
    toAddress.add(appInitiator.Email);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(et.Id);
        mail.setToAddresses(toAddress);
        mail.setSubject(et.Subject);
        mail.setHtmlBody(et.HtmlValue);
        mail.setReplyTo('No-Reply@MyCompany.com');
        mail.setSenderDisplayName('My Company');
        mail.setTargetObjectId(appInitiator.id);
       // mail.setWhatId();
        mail.setSaveAsActivity(false);
        mail.setUseSignature(false);
    List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
    allmsg.add(mail);

    try {
        Messaging.sendEmail(allmsg,false);
        return;
    } catch (Exception e) {
        System.debug(e.getMessage());
       }

}

0

You don't need to set every option, and in fact, this may be causing problems. The following changes should suffice:

    mail.setTemplateId(et.Id);
    // mail.setToAddresses(toAddress);
    // mail.setSubject(et.Subject);
    // mail.setHtmlBody(et.HtmlValue);
    mail.setReplyTo('No-Reply@MyCompany.com');
    mail.setSenderDisplayName('My Company');
    mail.setTargetObjectId(appInitiator.id);
    mail.setWhatId(appId);
    mail.setSaveAsActivity(false);
    mail.setUseSignature(false);

Apex will automatically merge data correctly without specifying a subject or body, since the email template Id has been specified.

Also, when using allOrNone=false, there is no need to use try-catch blocks, as no exceptions can be thrown:

// try {
    Messaging.sendEmail(allmsg,false);
//    return;
// } catch (Exception e) {
//    System.debug(e.getMessage());
// }
  • Subject and Html skipping is fine, Not sure if I can skip "setToAddresses" as to Email Addresss is field in the custom object and I am setting that to settoAddress. And my issue right now is inside the template I am using {!Customobj.Name} merge feild which is not populating when email is sent. – SFKumar Aug 13 '19 at 8:34
  • @Kumar It's {!Application__c.Name}, by the way. Make sure you use __c for custom objects. If you're using setTargetObjectId, that is who will get emailed as well. You might consider that. – sfdcfox Aug 13 '19 at 12:13
  • i double checked and i see that custom object name is spelled correctly with __c sufix. – SFKumar Aug 14 '19 at 10:09
  • @SFKumar You still need to setWhatId and setTargetObjectId in order to make sure everything merges correctly. Also, make sure the Id you're setting is for the correct type of record, and the user running the code has access to that record. – sfdcfox Aug 14 '19 at 12:29
0

I'm not completely sure I'm following your question, i'm assuming you mean you have a separate customObj from application__c. If that's the case, I also assume it is related to application__c somehow. If so then it would be mail.setWhatId(appInitiator.customObjId);

  • Its not related to Application but infact its a field from that object only, Inside the email body template I am using {!Application__c.Name} merge feild which is not populating when email is sent. – SFKumar Aug 13 '19 at 8:37

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.