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
Apurv SaxenaApurv Saxena 

Why do we set email.saveAsActivity = false everytime? What is it used for

Here is my email manager class
public class EmailManager {
    
    public static void sendMail(String Address,string subject, string body)
    {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        string [] toaddress = new string[]{address};
            
        email.setToAddresses(toaddress);
            
            //Avoid to hit the max number of emails
        //email.saveAsActivity = false;
        //email.setTargetObjectId(userId);
        
        email.setSubject(subject);
        email.setPlainTextBody(body);
        
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { email } );
    }
}

If I uncomment 'email.saveAsActivity = false' then also its working as expected then why do we use it? Thanks in advance.
Best Answer chosen by Apurv Saxena
buggs sfdcbuggs sfdc
HI,

`saveAsActivity` property works only when you set the `TargetObjectId` property of the `SingleEmailMessge` to a ContactId, not a UserId.

In detail:--

setSaveAsActivity(Boolean)
Optional. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates.

setTargetObjectId(ID)
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.
Signature
public Void setTargetObjectId(ID targetObjectId)
Parameters
targetObjectId
Type: ID
Return Value Type: Void
Usage
Do not specify the IDs of records that have the Email Opt Out option selected. All email must have a recipient value of at least one of the following:
• targetObjectIds
• toAddresses
• ccAddresses
• bccAddresses
• targetObjectId

And also I suggest you refer below link for more information after some research I found this link.Thought it could help you.

http://salesforce.stackexchange.com/questions/17553/messaging-sendemail-fails-with-targetobjectid-whatid-and-saveasactivity
 

Hope this helps you.

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards

All Answers

sfdcMonkey.comsfdcMonkey.com
hi Apurve
saveAsActivity   is a Optional attribute. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates.
i hop it helps you
Thanks
Mark it best answer if it helps you :)
buggs sfdcbuggs sfdc
HI,

`saveAsActivity` property works only when you set the `TargetObjectId` property of the `SingleEmailMessge` to a ContactId, not a UserId.

In detail:--

setSaveAsActivity(Boolean)
Optional. The default value is true, meaning the email is saved as an activity. This argument only applies if the recipient list is based on targetObjectId or targetObjectIds. If HTML email tracking is enabled for the organization, you will be able to track open rates.

setTargetObjectId(ID)
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.
Signature
public Void setTargetObjectId(ID targetObjectId)
Parameters
targetObjectId
Type: ID
Return Value Type: Void
Usage
Do not specify the IDs of records that have the Email Opt Out option selected. All email must have a recipient value of at least one of the following:
• targetObjectIds
• toAddresses
• ccAddresses
• bccAddresses
• targetObjectId

And also I suggest you refer below link for more information after some research I found this link.Thought it could help you.

http://salesforce.stackexchange.com/questions/17553/messaging-sendemail-fails-with-targetobjectid-whatid-and-saveasactivity
 

Hope this helps you.

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
This was selected as the best answer
Apurv SaxenaApurv Saxena
thanks :)