+ Start a Discussion

setTargetObjectId and setWhatId


Is there any reason that the setWhatId function can only be used when setTargetObjectId is a contact? I am building an email notice around a template and I want to send it to a user. Without the setWhatId, none of the merge fields pull through. Is there a work around that I am not aware of?


Rasmus MenckeRasmus Mencke
There are 2 types of id's you can pass in.

  1. The WhoId - targetObjectId which is the contact/lead or use
  2. The WhatId - which is a related object, will be used to merge in the data.
If you only pass in the contactId you should be able to get the merged data for the contact.
Hi Rasmus,

What I want to do is set the setTargetId as a user and still get the merged data - ie: I need to setWhatId to work when the setTargetObjectId is a user, not a contact. Right now, use of setWhatId is limited to instances when the target object is a contact. I need to know what to do in instances where the target object is a user.


Was this question ever answered?





Nope. I ended up using a fake contact whose record I locked so it couldn't be deleted by accident.





Has this been addressed by Salesforce?  I find this interesting because within the UI, I can specify a custom object record for my "Related To" (whatId), leave the "Name" field blank (Lead or Contact - whoId).  All the merge fields from the custom object record pull in to the email template just fine and I specify an internal user within the 'additional to' field.  Can't do this in my Apex class.  If I specify a setWhatId I have to have to setTargetObjectId to a lead or contact.  Seems backwards.  I also created the dummy contact record that is not visible to anyone else which is annoying.


Hi MsKnight, 


    I met this problem same as yours recently, did you have any luck to solve this problem later on?


- Chandler

I know this is old, but thought I might share how I solved this. 

I have a template that is VisualForce Email. The recipient type is user and the related to is contact. In this case I need both a user Id and a contact id to get the fields to merge. If I wanted to just use a contact, I can change reciepent type. If I want to have just user, then the relatedtotype should be User and not Contact. 

I have a label for the admin user, who is the user to send as the cc. I used the admin label for targetobjectid and the con.Id for whatId, which is the relatedTo record Id. 

To be clear, if you have a contact id, then you can make sure your recipenttype on the email template is contact and not a user. Or vice a versa if you are tagging to a user record. This is of course for VisualForce templates, which I prefer because I need to send a related list for all the record. 

It took a while to work through the errors, but once I understood what the system was trying to do, it made a bit more sense, all though sometimes salesforce methods have me scratching my head. 
Safiya pvSafiya pv
You can use what id along with target object as user and it will resolve the merge fields in email template.
settargetobject is required field when we use template and you can use contact, user or lead. Setwhatid is optional, if there only hardcoded text in your template. if the email template uses any merge fields then you need to set what id to the record id from which you want the field value (must be same as relatedToType).
below there is example email template and the controller
<messaging:emailTemplate subject="Merge field template" recipientType="User" relatedToType="Account">
	<messaging:htmlEmailBody >
				Name: {!relatedto.Name} <br/>
				Account Number:{!relatedto.AccountNumber}<br/>

public with sharing class sendEmailController {
      public String recordId ;
      public sendEmailController(ApexPages.StandardController controller) {
          recordId = ApexPages.currentPage().getParameters().get('id');
      public void sendEmail() {       
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          mail.setTargetObjectId('123456789qwertyu'); //it can be user id, contactid, or lead. to whom you are sending the email
          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Prasanna NemalipuriPrasanna Nemalipuri
Hi everyone,

I ran into similar use case where I had to use User asn targetobjectId and custom object Id as whatId. After a lot of time I was finally able to make it work by using recipientType="User" and relatedToType="Custom_Object__c" in the Visualforce email template. Also, my email message method is being invoked from Apex trigger.

Here is the SingleEmailMessage methods that I used:
List<Messaging.SingleEmailMessage> lstMessages = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new List<String> { User_Field__c };
message.setCcAddresses(new List<String> { ccAddress });
List<Messaging.SendEmailResult> lstResults = Messaging.sendEmail(lstMessages);

Hope this helps!!