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
AbAb 

best way to represent an email sent from apexclass on the opportunity record

Hello,

I am sending an email from an apex class like
 
Messaging.SendEmailResult [] r = 
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

This email is sent afer a modification made on opportunity.

When i send email:
I have Body, attachment.

I want this email to be showed on Opportunity.

Can i show this as Task and add it on related list on Opportunity ?
or I add an attachment and say that opportunity is the parent.

thank you in advance for suggestions
Best Answer chosen by Ab
Deepali KulshresthaDeepali Kulshrestha
Hi Sandrine,

If you want to send email and want to show in task and want to add it on related list on Opportunity.
You have to add this line in your code:
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSaveAsActivity(true);      
email.setWhatId(opportunityId);           

You can refer to this link to know how to send attachment as an email. But you have to include the lines i have mentioned in the code to save it as task in the related list of opportunity.
https://developer.salesforce.com/forums/?id=906F00000005J1cIAE

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha