How to use email template without targetobjectid?

Sample Trigger:

trigger EmployeeTrigger on Employee__c (after insert, after update) {
    Contact con = [ SELECT Id FROM Contact WHERE Email != null LIMIT 1];
    List < Messaging.SingleEmailMessage > listMsgs = new List < Messaging.SingleEmailMessage > ();
    for ( Employee__c e : trigger.new ) {
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setWhatId(e.Id);
        msg.setTemplateId('00XU00000011sTd');
        msg.setToAddresses(new List < String > {'test30@gmail.com'});
        msg.setTreatTargetObjectAsRecipient(false);
        msg.setTargetObjectId(con.Id);
        listMsgs.add(msg);
    }    
    Messaging.sendEmail(listMsgs);
}

Cheers!!!

No comments:

Post a Comment