0

Is it possible to use attachments from notes and attachments in send email. If person A attached some files to account in Notes and attachments. IF he click on send email button, to attach these files first he need to download files on local machine and then he can select to attach. But is it possible to select attachments from Notes and attachments directly without downloading it? Please help.

Thanks in advance.

  • I think you will need to download it to create the attachment - or you could provide a link in the email to the attachments - requiring the user to login - as well have view access to the file. – Ronnie Sep 24 '18 at 19:18
0

Create a SendingEmailVF visualforce Page

<apex:page standardController="Account" extensions="SendemailController">
<apex:form >
<script type="text/javascript">
function init() {
sendEmail();
}
if(window.addEventListener)
window.addEventListener('load',init,true)
else
window.attachEvent('onload',init)
</script>

<apex:actionFunction name="sendEmail" action="{!sendEmailFunction}">
</apex:actionFunction>
</apex:form>
</apex:page>

Controller

public class SendemailController {    

public String AccountId {get;set;}

Public SendemailController(ApexPages.StandardController controller){
AccountId = ApexPages.currentPage().getParameters().get('Id');
}

Public Pagereference sendEmailFunction(){

String toaddress = 'test@test.com'; //your email address
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {toaddress};
mail.setToAddresses(toAddresses);
mail.setReplyTo(toaddress);
mail.setSenderDisplayName('Name');
mail.setSubject('Testing email through apex');
mail.setBccSender(false);
mail.setUseSignature(true);
mail.setPlainTextBody('This is test email body. This mail is being sent from apex code');
//mail.setHtmlBody('<b> This is HTML body </b>' );

List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :AccountId]){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);
//mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
}
mail.setFileAttachments(fileAttachments);

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

} catch(Exception e) {}


PageReference reference = new PageReference('https://na35.salesforce.com/'+AccountId);
reference.setRedirect(true);
return reference;
}

}

Create a Account Custom Button OnClick JavaScript

window.location = 'apex/SendingEmailVF?id={!Account.Id}';  
  • Actually Nagarjuna, I need the functionality same as Send Email standard button on Account. But while attaching attachments, it should select attachments from Notes and attachment of account. The above code is sending mail directly on click of button. – Sofiya Jan 3 '17 at 12:25
0

The uploaded attachment to Notes and Attachments is available from APEX/SOQL. This is uploaded as Content Version and is a little bit complicated. See this example:

    ContentDocumentLink contentLink = [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId ORDER BY SystemModstamp DESC][0];
    ContentDocument contentDocument = [SELECT Id, LatestPublishedVersionId FROM ContentDocument WHERE Id = :contentLink.ContentDocumentId ORDER BY LastModifiedDate DESC][0];
    ContentVersion link = [SELECT Id, Title, VersionData FROM ContentVersion WHERE Id = :contentDocument.LatestPublishedVersionId ORDER BY LastModifiedDate DESC][0]; 

RecordId is the record related to those Notes and Attachments and from there you need to get the ContentDocument and then finally the ContentVersion. The ContentVersion holds the document in the field VersionData. For more info regarding this please see the documentation for this ContentVersion.

When it comes to sending the email you can look at the Developer Guide about Outbound Email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.