[SalesForce] Email Alerts and Merge Fields not working.

i just need an email alert to be sent to contacts, and activity to be tracked, i need to know if they were opened or not. Process builder cannot provide that for me.

The Problem is that none of the merge fields work. They dont populate, either sending it to myself, or sending it to the contact.

ive tried Pass the template as a string but then if i change the template, or a certain field does not come over in the lead then the program will throw an error. so id like to just get the merge fields to work.

if i change the trigger to after update to populate WhatId correctly (i believe this is correct) i get errors thrown stating:

INVALID_ID_FIELD, Invalid entity type for whatId

if i comment out setWhatId the emails send out, but the merge fields are not populated:
enter image description here

if any one could give some insight of why this isnt working, please let me know. i have been through most of the suggested links on stack exchange and none seem to apply to this situation, so im having a hard time relating them to my situation. please explain why and how i should be making a connection if you provide a link that shows a possible solution.

Class:

public with sharing class vohliLeadAlert {
public void alerts(List<VOHLI_Leads__c> lstLead){
    leadSourceFacebook(lstLead);
}

private void leadSourceFacebook(List<VOHLI_Leads__c> lstLead){
    OrgWideEmailAddress vohliAddress = [SELECT ID, Address FROM OrgWideEmailAddress WHERE Address = 'jfrangoulis@vohli.com' LIMIT 1];
    EmailTemplate vohliFBExternalTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'VOHLI External Lead: Facebook' limit 1];
    EmailTemplate vohliFBInternalTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'VOHLI Internal Lead: Facebook' limit 1];


    list<Messaging.SingleEmailMessage> lstEmailFBLead = new list<Messaging.SingleEmailMessage>();

    for(VOHLI_Leads__c fbLead : lstLead){
        if(fbLead.Lead_Source__c == 'Facebook'){



            //External Email
            Messaging.SingleEmailMessage externalEmail = new Messaging.SingleEmailMessage();
            externalEmail.setTargetObjectId(fbLead.Realtor__c);                     
            externalEmail.setOrgWideEmailAddressId(vohliAddress.ID); 
            externalEmail.setTemplateId(vohliFBExternalTemplate.Id); 
            externalEmail.setSaveAsActivity(true);
            externalEmail.setUseSignature(false);
            externalEmail.setWhatId(fbLead.Id);
            lstEmailFBLead.add(externalEmail);

            //internal Email
            Messaging.SingleEmailMessage internalEmail = new Messaging.SingleEmailMessage();
            internalEmail.setTargetObjectId(fbLead.OwnerId); 
            internalEmail.setOrgWideEmailAddressId(vohliAddress.ID); 
            internalEmail.setTemplateId(vohliFBInternalTemplate.Id); 
            internalEmail.setSaveAsActivity(true);
            internalEmail.setUseSignature(false);
            //internalEmail.setWhatId(fbLead.Id);
            lstEmailFBLead.add(internalEmail);
        }
    }
    if(!lstEmailFBLead.isEmpty()){
        Messaging.SendEmail(lstEmailFBLead);
    }
}

}

Best Answer

There is a checkbox on every custom object definition, "Track Activities". You'll need to ensure that the box is set on VOHLI_Leads__c so that it can be the WhatId of a Task.

Your merge fields, presumably, are using a relationship path that goes through the related object. That's what WhatId corresponds to in the template, so if WhatId is not populated, none of those merge fields will receive values.