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
Joe HayesJoe Hayes 

Add more than one attachment from different objects

Hi Everyone,

I have got a class that sends an email and it works really well adding the files from the related notes and attachments. However, I would also like to include a file from the documents object.
This file will have a fixed id of 015D0000003rgYq

I tried something like this, but cannot get it to work:
Document doc = [SELECT Id FROM Document WHERE Id = '015D0000003rgYq'];

How can I reference this in my code and then get it to attach both files from the different objects?

A sample of the code I currently have is:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     List<String> toaddress = new List<String>();
	 toaddress.add(string.valueof(cf.Email_to_send_confirmation_to__c));
        mail.settoaddresses(toaddress);
        mail.setReplyTo('trainingbookings@certsure.com');
        mail.setSenderDisplayName('Certsure Training');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.saveAsActivity = true;
     	mail.setSubject(subject);
     	mail.setHtmlBody(htmlBody);
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        } 
        mail.setFileAttachments(fileAttachments);
      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
Best Answer chosen by Joe Hayes
Abhishek_DEOAbhishek_DEO
What errors are you getting? COuld you please try below code?
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     List<String> toaddress = new List<String>();
	 toaddress.add(string.valueof(cf.Email_to_send_confirmation_to__c));
        mail.settoaddresses(toaddress);
        mail.setReplyTo('trainingbookings@certsure.com');
        mail.setSenderDisplayName('Certsure Training');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.saveAsActivity = true;
     	mail.setSubject(subject);
     	mail.setHtmlBody(htmlBody);
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     
	 // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        } 
	 
	 // for document
        Document doc = [select id, name, body, contenttype, developername, type from Document where id = '015D0000003rgYq'][0];
       	 Messaging.Emailfileattachment MyDocefa = new Messaging.Emailfileattachment();
		 MyDocefa.setContentType(doc.contentType);
        MyDocefa.setFileName(doc.developerName+'.'+doc.type);
        MyDocefa.setBody(doc.Body);
        fileAttachments.add(MyDocefa);  
        mail.setFileAttachments(fileAttachments);
      
	  //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;

 

All Answers

Abhishek_DEOAbhishek_DEO
What errors are you getting? COuld you please try below code?
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     List<String> toaddress = new List<String>();
	 toaddress.add(string.valueof(cf.Email_to_send_confirmation_to__c));
        mail.settoaddresses(toaddress);
        mail.setReplyTo('trainingbookings@certsure.com');
        mail.setSenderDisplayName('Certsure Training');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.saveAsActivity = true;
     	mail.setSubject(subject);
     	mail.setHtmlBody(htmlBody);
        
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
     
	 // Add to attachment file list
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        } 
	 
	 // for document
        Document doc = [select id, name, body, contenttype, developername, type from Document where id = '015D0000003rgYq'][0];
       	 Messaging.Emailfileattachment MyDocefa = new Messaging.Emailfileattachment();
		 MyDocefa.setContentType(doc.contentType);
        MyDocefa.setFileName(doc.developerName+'.'+doc.type);
        MyDocefa.setBody(doc.Body);
        fileAttachments.add(MyDocefa);  
        mail.setFileAttachments(fileAttachments);
      
	  //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;

 
This was selected as the best answer
Joe HayesJoe Hayes
Abishek,

You are a legend! Thank you!

Ta
Joe