function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shree KShree K 

what is the use of SetTargetObjectId and when will be appropriate to use it?

Best Answer chosen by Shree K
Shashikant SharmaShashikant Sharma
Yes Email will be send to the Email field on the record ( Lead, Contact or User ) based on the Id that you provide and template will show merge fields accordingly. So if you put the merged field like

Hi {!Contact.FirstName} and use a contact record id having name Mark then it will say
Hi Mark

Answers to you questions:


- Does This works only for one record at a time or for multiple records?
[Shashikant] -  It will take one id for one SingleEmailMessage instance but you could send for multiple records at a time by using a array like below.
 
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.setTargetObjectId(con1.Id);
mail1.setTemplateId('00X90000000QHUD');

Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setTargetObjectId(con2.Id);
mail2.setTemplateId('00X90000000QHUD');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1, mail2 });



-- Does the merge fields in the template should particularly belong to the object which we are referring to or it could be from any other object? 
[Shashikant] -  Merge fields should be seleced accordingly you are puting the id. Like given the example above for contact.

--I have seen few references that says,this also works for MassEmailMessaging,if so ,does setTargetObjectId works in the same manner for both
single and massemailmessaging or any difference.
[Shashikant] - setTargetObjectId 


Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
setTargetObjectId(targetObjectId) is used while sending email from Apex code using SingleEmailMessage.

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

Read this for more - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Thanks
Shashikant
Shree KShree K
 Hi shashikant thank you....

-- so that mean.... The email will be sent to the email address that is in that record(refering by the recordID),and the template will be having the merge fields that belongs to that object....am i right till here...

lastly :
-- Does This works only for one record at a time or for multiple records?
-- Does the merge fields in the template should particularly belong to the object which we are referring to or it could be from any other object? 
--I have seen few references that says,this also works for MassEmailMessaging,if so ,does setTargetObjectId works in the same manner for both single and massemailmessaging or any difference.

Thanks once again.


 
Shashikant SharmaShashikant Sharma
Yes Email will be send to the Email field on the record ( Lead, Contact or User ) based on the Id that you provide and template will show merge fields accordingly. So if you put the merged field like

Hi {!Contact.FirstName} and use a contact record id having name Mark then it will say
Hi Mark

Answers to you questions:


- Does This works only for one record at a time or for multiple records?
[Shashikant] -  It will take one id for one SingleEmailMessage instance but you could send for multiple records at a time by using a array like below.
 
Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
mail1.setTargetObjectId(con1.Id);
mail1.setTemplateId('00X90000000QHUD');

Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setTargetObjectId(con2.Id);
mail2.setTemplateId('00X90000000QHUD');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1, mail2 });



-- Does the merge fields in the template should particularly belong to the object which we are referring to or it could be from any other object? 
[Shashikant] -  Merge fields should be seleced accordingly you are puting the id. Like given the example above for contact.

--I have seen few references that says,this also works for MassEmailMessaging,if so ,does setTargetObjectId works in the same manner for both
single and massemailmessaging or any difference.
[Shashikant] - setTargetObjectId 


Thanks
Shashikant
This was selected as the best answer
SForceBeWithYouSForceBeWithYou

By default, when you use setTargetObjectId(ID), the Lead/Contact/User WILL be used as a recipient.

  • The merge fields will use target object AND record specified by setWhatId.
  • This only works for one record at a time, but you can loop through a list and build up a List<Messaging.SingleEmailMessage> to send all at once.
  • The merge fields MAY only work directly on the object(s) you specify for setTargetObjectId and setWhatId, but if you want to get relationship fields, try the approach mentioned in this StackExchange post: Email Template merge fields (Account.Name) (https://salesforce.stackexchange.com/questions/185301/email-template-merge-fields-account-name). Otherwise, you may have to create formula fields directly on the object giving you the values you want on the related/parent object.
  • You CAN do this with Messaging.MassEmailMessage, but the methods are setTargetObjectIds(List<Id>) and setWhatIds(List<Id>).  All target objects must be of the same type (Lead, Contact, or User).  I'm not sure how it works if you pass in a list of different size for setWhatIds than setTargetObjectIds, but don't do that! :-)

 

Important: If you DON'T want the target object to be used as a recipient and instead want ONLY setToAddresses to dictate that, make sure to use:

setTreatTargetObjectAsRecipient(false);