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 

Attach email sent from Apex to object Opportunity

Hello,

I am sending an email from the Apex class, i want to attache this email to the record of opportunity.
I have the opportunity Id,

I am using Messaging.SingleEmailMessage[] to send the email

Thank you for suggestions
Best Answer chosen by Ab
Raj VakatiRaj Vakati
Refer this links


https://howtodoitinsalesforce.blogspot.com/2016/12/send-emails-with-attachment-from-your.html

https://www.greytrix.com/blogs/salesforce/2017/03/29/send-an-email-with-attachment-from-salesforce/


https://www.forcetalks.com/salesforce-topic/i-am-new-to-salesforce-i-want-to-send-email-with-attachment/
public class EmailStaticResource {

public void StaticresourceDataAsEmail(){

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

email.setUseSignature(false);

email.setSaveAsActivity(true);

email.setSubject(‘Send Email using staticResource as body of mail ‘);

String[] toAddresses = new String[] {‘ganesh.maharana@janbask.com’};

email.setToAddresses(toAddresses);

email.setHtmlBody(‘<html><body>Dear devoplers<b>See the code for Email service</b></body></html>’);

StaticResourcesr = [Select  Name, Id, Body From StaticResource where Name = ‘Document’];

Blob tempBlob = sr.Body;

Messaging.EmailFileAttachmentefa = new Messaging.EmailFileAttachment();

efa.setBody(tempBlob);

efa.setFileName(‘attachment.pdf’);

email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};

Messaging.sendEmail(emailList);

}

}

 

All Answers

Raj VakatiRaj Vakati
Refer this links


https://howtodoitinsalesforce.blogspot.com/2016/12/send-emails-with-attachment-from-your.html

https://www.greytrix.com/blogs/salesforce/2017/03/29/send-an-email-with-attachment-from-salesforce/


https://www.forcetalks.com/salesforce-topic/i-am-new-to-salesforce-i-want-to-send-email-with-attachment/
public class EmailStaticResource {

public void StaticresourceDataAsEmail(){

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

email.setUseSignature(false);

email.setSaveAsActivity(true);

email.setSubject(‘Send Email using staticResource as body of mail ‘);

String[] toAddresses = new String[] {‘ganesh.maharana@janbask.com’};

email.setToAddresses(toAddresses);

email.setHtmlBody(‘<html><body>Dear devoplers<b>See the code for Email service</b></body></html>’);

StaticResourcesr = [Select  Name, Id, Body From StaticResource where Name = ‘Document’];

Blob tempBlob = sr.Body;

Messaging.EmailFileAttachmentefa = new Messaging.EmailFileAttachment();

efa.setBody(tempBlob);

efa.setFileName(‘attachment.pdf’);

email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

Messaging.SingleEmailMessage[] emailList = new Messaging.SingleEmailMessage[] {email};

Messaging.sendEmail(emailList);

}

}

 
This was selected as the best answer
KrishnaAvvaKrishnaAvva
Hi Sandrine,
One way to do it is convert the information sent in the email to a PDF format by developing an intermittent visualforce page and attaching the PDF to the opportunity.
You have to attach it as a related list using the Attachment object.
Something like this in your class:
Attachment attach = new Attachment(parentId = OpportunityId,body=pdfBody Name=Test+'.pdf');
insert attach;

Regards,
Krishna Avva