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
sunfishettesunfishette 

Emailing an attachment

Hi -

 

I want to be able to attach a file from opportunity objects Notes and Attachments section to an email.

 

Example:

Fred logs in through partner portal.  He opens Opportunity XYZ and sees there are 6 attached pdf files.  He wants to select 1 of the files, attach it to an email and send it.


So far, the only way he can do it is to download the file to his desktop and work from there. 

 

Can we accomplish this without the download?

 

How?

Thanks!

cloudElephantcloudElephant

Hi sunfishette,

 

As far as I understand, you want a mechanism that allows a partner user to send email via Activity History with attachments that are present under Notes & Attachments for an opportunity record.

 

Since the only option to attach file in an email via Activity History is to browse through your hard-drive and upload, we cannot do much here in standard declarative way. However, there is something that can be done leveraging Apex.

 

First of all, you would require to bring down this functionality to VF page that can be launched via Custom button placed on Opportunity detail page. You'll have to provide a selectList or check-box menu(for grabbing attachment Id) for attachments that belong to host opportunity.

 

On selection of one or more attachments, you can call below apex code that sends an email with selected attachment:

 

List<Attachment> lstAttachment =	[	Select Id, ContentType, BodyLength, Body 
						From Attachment 
						Where Id = 'ATTACHMENT_ID'
					];		

// Declare an array for email attachments
Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[]{};

Messaging.EmailFileAttachment pdfFileAttachment = new Messaging.EmailFileAttachment();
pdfFileAttachment.setBody(lstAttachment[0].Body);
pdfFileAttachment.setFileName('File.pdf');

fileAttachments.add(pdfFileAttachment);

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('YOUR SUBJECT');     
email.setPlainTextBody('YOUR EMAIL BODY');
email.setToAddresses(new String[]{'YOUR@EMAIL.com'});
email.setFileAttachments(fileAttachments);

Messaging.SendEmailResult[] sendResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});

 

After validating sendResult success, you can push a task record with necessary details so that record remains in activity history too.

 

Please let me know if this helps.