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

what happens when SetTargetObjectId used?

1.what is the importance setTargetobjectId  and when to use it? ,Does it sends the emails in a way that they will not be counted against daily email limit or will it bypassses the daily email limit?. to use WhoId and WhatId,will those work on all objects or only on specific objects?
Himanshu ParasharHimanshu Parashar

Here is the answer for both questions.

1. setTargetobject is 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.

it means it will not avoid daily email limit.

2. WhoId and WhatId are not available for all object for example while sending an email you can specify whatId. If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.

Task and Event object refer whatId and WhoId.

Salesforce Certified Developer, Administrator, Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks  @Himanshu Parashar,
lastly, from my understanding
-we can only use if we want to use any templates,and
-The emails which sent by using SetTargetrObjectId are also counted against daily email limit
am i right.
Are there any type of emails that can be sent in way that those will not be counted againest daily email limit. 
Himanshu ParasharHimanshu Parashar
yes you understood correctly. 

no all email counted against your daily limit