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
Daniel MuirDaniel Muir 

Sending Attachment from Object Notes and Attachments section from Salesforce email

Hi All, 

Is there a way that we can send an email from Salesforce and easily attach items form a custom objects notes and attachmnets section? The current method of saving locally and then uploading is very time consuming. 
From what I have seen so far, the best method is to create a custom "send email" button which fetches the required attachments, but as I am not a developer I cannot say this with any clarity.  
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Daniel Muir,
Please follow this example, create a visual force page and extension controller and create a custom button by adding this visual force page to the corresponding object.

Controller Extension: 
public class sendAttachments {
    public string con;
    public sendAttachments(ApexPages.StandardController controller)
    {
    this.con=(String)controller.getRecord().Id;
    System.debug('contact id is'+con);
    }
    public  void sendEmailWithAttachements(){
       List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
       //Create attachment list
       List<Messaging.EmailFileAttachment> attachmentList = new List<Messaging.EmailFileAttachment>();
          Attachment att = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId = : con];
        // List of attachments handler 
        {
       Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
       efa.setFileName(att.Name);
       efa.setBody(att.body);
       efa.setContentType(att.ContentType);
       efa.setInline(false);
       attachmentList.add(efa);
        }
        System.debug('attachment list is'+attachmentList);
        
       Messaging.SingleEmailMessage singlemail = new Messaging.SingleEmailMessage();
        
       String []ToAddress= new string[] {'gd@gmail.com'};
       singlemail.setToAddresses(toAddress);
       singlemail.setFileAttachments(attachmentList);
        singlemail.setPlainTextBody('PFA');
       emails.add(singlemail);
       
       

     
      // Attach files to email instance
    
      //send the message
      Messaging.sendEmail(emails);
    
	}
}
Visual Force Page:
<apex:page standardcontroller="Contact" extensions="sendAttachments">
<apex:form >
<apex:commandButton value="Send Email!" action="{!sendEmailWithAttachements}"/>
</apex:form>
</apex:page>

If this helps to solve your problem, please choose this as the best answer.

Thank you!
Daniel MuirDaniel Muir
Thanks Akhilesh. I've created these two, but when I go to create a custom button, no content appears. As i am not a developer, I amnot sure how much of the above code needs changed for our object. 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Hi Daniel, to use this for your own custom object make sure you give the custom object name in the standard controller for example, make sure you give the name of your customobject as below and change the email id to the email ids that you need to send this attachments in line 26 of apex code.



<apex:page standardcontroller="exampleObject__c" extensions="sendAttachments">


String []ToAddress= new string[] {'example@gmail.com'};


 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
<apex:page standardcontroller="customObjectName__c" extensions="sendAttachments">
<apex:form >
<apex:commandButton value="Send Email!" action="{!sendEmailWithAttachements}"/>
</apex:form>
</apex:page>
This is the code that helps you choose this visual force page on the custom buttons for your custom object.

Hope this helps, if this helps please choose this as best answer.

Thank you!
 
Daniel MuirDaniel Muir
Thanks Akhilesh. 
I will try this now. 
I noticed that you wrote a piece regarding email ID's. The attachments we would be sending would be related to the contact in the specific record, with an email template, and may add others in CC if necessary. Is that also possible with this code? 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
HI Daniel, as you asked how to send attachments related to a custom object, the above code helps to achieve this how ever if you need to send email templaes then that is different code (https://developer.salesforce.com/forums/?id=906F00000005IlPIAU).
Hope this helps 

Thank you