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
nduchnduch 

How to insert Activity History record after sending an email

Hi,

I cannot create a new Activity History record when using message.setSaveAsActivity(true);

 

This is my code:

 

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {recipient.Email};
        mail.setToAddresses(toAddresses);
        mail.setBccSender(true);
        String[] bccAddresses = new String[] {person.Email};
        mail.setBccAddresses(bccAddresses);
        mail.setSaveAsActivity(true);
        mail.setSubject(subject);
        mail.setPlainTextBody(templateBody);

       Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

Am I doing something wrong?My controller is extending the custom controller.

 

Many thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

I think you have forgot to mention the value of setTargetObjectId  inside the SingleEmailMessage method try to use this: Put setTargetobjectid value as per your requirement.

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

       Contact con=[select id from contact limit 1];               

        String[] toAddresses = new String[] {recipient.Email};

        mail.setToAddresses(toAddresses);

        mail.setBccSender(true);

        String[] bccAddresses = new String[] {person.Email};

        mail.setBccAddresses(bccAddresses);

       mail.setTargetObjectId(con.id);

        mail.setSaveAsActivity(true);

        mail.setSubject(subject);

        mail.setPlainTextBody(templateBody);

       Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

For more detail go through this:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

I think you have forgot to mention the value of setTargetObjectId  inside the SingleEmailMessage method try to use this: Put setTargetobjectid value as per your requirement.

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

       Contact con=[select id from contact limit 1];               

        String[] toAddresses = new String[] {recipient.Email};

        mail.setToAddresses(toAddresses);

        mail.setBccSender(true);

        String[] bccAddresses = new String[] {person.Email};

        mail.setBccAddresses(bccAddresses);

       mail.setTargetObjectId(con.id);

        mail.setSaveAsActivity(true);

        mail.setSubject(subject);

        mail.setPlainTextBody(templateBody);

       Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

For more detail go through this:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
nduchnduch

I have one small problem now - I have added this code (as you recommended), but my recipient is now getting 2 emails!

 

mail.setTargetObjectId(recipient.Id);

 

Any idea why?

 

Many thanks!

 

Kayla Borg 14Kayla Borg 14
If you pass a value to setTargetObjectId AND setToAddresses, and the email address for the TargetObject and the email address you provided in setToAddresses are the same, they will receive 2 e-mails.