• Franciszek Grobelny
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I'm not understanding how merge fields work when using templates sent via apex.  Here is my current code:
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            Id templateId;
            String[] toAddresses = new String[]{'sales.admin@companydomain.com'};
            email.setToAddresses(toAddresses);
            email.setTargetObjectId(p.Id);
            email.setTemplateId('00X11000000OO2B');
            email.setorgWideEmailAddressId('0D2150000008TCX');
            email.setSaveAsActivity(false);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

What I'm still really confused about, even after reading the documentation, is what the setTargetObjectId and setWhatId actually do.  

I need to merge several fields from our custom project object, so I was assuming that's what I would use the specific project record id as the setTargetObjectId, but I'm not sure if that's correct as everything I've read says the setTargetObjectId is who you're sending the email to, which in this case is our sales admin.  
So, where / how do I set what the record is that I want to merge fields from, e.g. a specific project record.  Thanks in advance.