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
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12 

Sending two attachments by using standard email UI!!!

Hi EveryOne,

The user request is that when a Custom button(Send Email) is clicked on the oportunity screen it should open a standard email UI and select the email template associated to the product for the template in that body and two attachments - attachment1  and attachment2 .

I am able to achieve all the functionality but we can add only one attachment. when we call the url to call that standard email page, we can only add one &docid = .

I am Passing this URL:

emailURL = '/_ui/core/email/author/EmailAuthor?p2_lkid=' + opp.Account.id + '&p3_lkid=' + opp.id + 
                    '&template_id='+getTemplateId(opp.Product__r.Name) + '&doc_id='+getCreatedDocId();

With this I am able to do the above functionality of sending email with one attachment.

How do I send 2 attachments?

Thanks In advance.........

I am sending my code below.
//Send the CCL Document to the potential when we cliclk the Send CCL Button.
public class CCLSendEmail {

    public Account acc;
    public Opportunity opp;
    public CCL_Documents__c cclDocs;//Custom settings
    Product_EmailTemplates__c template;//Custom settings
    public CCLSendEmail(ApexPages.StandardController controller){
        opp = [Select Id, Account.id, Product__r.Name, Account.Name,StageName from Opportunity where Id = :ApexPages.currentPage().getParameters().get('id')];
    }
    
    public PageReference pdfEmail(){
        try{
            if(opp.StageName.equals('CCL Sent')){        
                String emailURL;
                emailURL = '/_ui/core/email/author/EmailAuthor?p2_lkid=' + opp.Account.id + '&p3_lkid=' + opp.id + 
                    '&template_id='+getTemplateId(opp.Product__r.Name) + '&doc_id='+getCreatedDocId();
                PageReference cclPage = new PageReference(emailURL); 
                cclPage.setRedirect(true); 
                return cclPage;
             }
             else{
                     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING, 'Email Template is not there for this perticular Product');         
                     return null;
                  }
                  
             
              }catch(Exception e){
                    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING, e.getMessage()); 
                    ApexPages.addMessage(myMsg); 
                    return null;
       }  
   }
   
   
    public String getCreatedDocId(){
        Blob content;
        cclDocs = CCL_Documents__c.getInstance('VNP CCL Documents');
        PageReference pr = Page.CCLPdf; 
        pr.setRedirect(true); 
        pr = Page.CCLPdf; 
        pr.setRedirect(true); 
        pr.getParameters().put('id', opp.id); 
        
        if(!Test.isRunningTest()){ 
            content = pr.getContentAsPDF(); 
        } 
        
        Document doc = new Document(Name = opp.Account.Name+'.PDF', Body = content, FolderId = cclDocs.Document_Id__c);
        Database.SaveResult insertResult = Database.Insert(doc, true); 
        return doc.Id;
   }  
   
   
   public String getTemplateId(String prodName){
       template = [Select Name, Email_Template_Id__c from Product_EmailTemplates__c 
                        where Name = : prodName];
       return template.Email_Template_Id__c;
   }
}


 
Best Answer chosen by rmranjith8881.3927046400771116E12
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi,

We can't send the Multiple Attachments by overriding the Send an Email Button(Which i tried above).

So, I tried differently and I posted below for referenced to others.

I was able to send more than one email attachment by writing below Extensions and Visulforce page.

Extensions:
 
public class SendEmail {

    public Account acc;
    public String subject { get; set; }
    public String body { get; set; }
    Product_EmailTemplates__c template;//Custom settings to read the templatedID for which template to send
    public Opportunity opp { get; set;}
    public String htmlBody {get; set;}
    

    // Create a constructor that populates the Opportunity object
    public SendEmail(ApexPages.StandardController controller) {
        opp = [Select Id, Name, Email__c, Amount, Contact_Name__c, Account.PersonContactId, Account.id, Product__r.Name, Account.Name, StageName, Tax_Amount__c, Total_after_Tax__c, payment_1_Amount__c, Payment_2_Amount__c, Payment_3_Amount__c
               from Opportunity where Id = :ApexPages.currentPage().getParameters().get('id')];
              
       EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = :getTemplateId(opp.Product__r.Name)];
    
       htmlBody = emailTemplate.HtmlValue;
       subject = emailTemplate.subject;
    }

    public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.CCLPdf;
        pdf.getParameters().put('id',opp.id); 
        pdf.setRedirect(true);

        // Take the PDF content
        Blob b = pdf.getContentAsPDF();
        
        PageReference pdf2 =  Page.ProformaInvoicePdf;
        pdf2.getParameters().put('id',opp.id); 
        pdf2.setRedirect(true);

        // Take the PDF content
        Blob b2 = pdf2.getContentAsPDF();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(opp.account.Name+'-CCL.pdf');
        efa.setBody(b);
        
        Messaging.EmailFileAttachment efa2 = new Messaging.EmailFileAttachment();
        efa2.setFileName(opp.account.Name+'-Invoice.pdf');
        efa2.setBody(b2);


        
        // Sets the paramaters of the email
        email.setSubject(subject);
        email.setHtmlBody(htmlBody);
        //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name)
        email.setTargetObjectId(opp.Account.PersonContactId);
        email.setSaveAsActivity(true);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa,efa2});
        

        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        StoringAttachments();
        
        return new PageReference('/'+opp.Id);
    }
    
    public void StoringAttachments() {
        Attachment myAttach = new Attachment();
        myAttach.ParentId = opp.Account.PersonContactId;
        myAttach.name = opp.account.Name+'-CCL.pdf';
        PageReference myPdf = Page.CCLPdf;
        myAttach.body = myPdf.getContentAsPdf();
        insert myAttach;
        
        Attachment myAttach1 = new Attachment();
        myAttach1.ParentId = opp.Account.PersonContactId;
        myAttach1.name = opp.account.Name+'-Invoice.pdf';
        PageReference myPdf1 = Page.ProformaInvoicePdf;
        myAttach1.body = myPdf1.getContentAsPdf();
        insert myAttach1;
        } 
            
    public String getTemplateId(String prodName){
       template = [Select Name, Email_Template_Id__c from Product_EmailTemplates__c 
                        where Name = : prodName];
       return template.Email_Template_Id__c;
   }
}

Visualforce Page:
 
<apex:page standardController="Opportunity" extensions="SendEmail">
 <apex:messages />
    <apex:pageBlock >
        
        <apex:pageBlockTable value="{!Opportunity.Account.Name}" var="contact" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                {!Opportunity.Name}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Email</apex:facet>
                {!Opportunity.Email__c}
            </apex:column>
        </apex:pageBlockTable>
    
        <apex:form >
        <br /><br />
            <apex:outputLabel value="Subject" for="Subject"/>:<br />     
            <apex:inputText value="{!subject}" id="Subject" maxlength="80" size="100"/>
            <br /><br />
            <apex:outputLabel value="Body" for="Body"/>:<br />     
            <apex:inputtextarea richText="True" value="{!htmlBody}" id="Body" rows="10" cols="80" />        
            <br /><br /><br />
            <apex:outputText value="{!Opportunity.Product__r.Name}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Tax_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Total_after_Tax__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.payment_1_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Payment_2_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Payment_3_Amount__c}" rendered="false"/>
            <apex:commandButton value="Send Email" action="{!send}" />&nbsp;
            <apex:commandButton value="Cancel" action="{!Cancel}" /> 
        </apex:form><br/>
        <apex:pageBlockSection title="Attachments" columns="1">
            <apex:outputLink value="{! $Page.CCLPdf}" target="_blank">{!Opportunity.Name} CCL.pdf</apex:outputLink>
            <apex:outputLink value="{! $Page.ProformaInvoicePdf}" target="_blank">{!Opportunity.Name} ProformaInvoice.pdf</apex:outputLink>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Don't forget to Create a Button on Opportunity.

I hope, it helps a lot.

Thanks
Ranjith

All Answers

Shaijan ThomasShaijan Thomas
Line 49 - createing document.
Create onemore document, return should be Id List, change the string return to Id list or string list
Shaijan
rmranjith8881.3927046400771116E12rmranjith8881.3927046400771116E12
Hi,

We can't send the Multiple Attachments by overriding the Send an Email Button(Which i tried above).

So, I tried differently and I posted below for referenced to others.

I was able to send more than one email attachment by writing below Extensions and Visulforce page.

Extensions:
 
public class SendEmail {

    public Account acc;
    public String subject { get; set; }
    public String body { get; set; }
    Product_EmailTemplates__c template;//Custom settings to read the templatedID for which template to send
    public Opportunity opp { get; set;}
    public String htmlBody {get; set;}
    

    // Create a constructor that populates the Opportunity object
    public SendEmail(ApexPages.StandardController controller) {
        opp = [Select Id, Name, Email__c, Amount, Contact_Name__c, Account.PersonContactId, Account.id, Product__r.Name, Account.Name, StageName, Tax_Amount__c, Total_after_Tax__c, payment_1_Amount__c, Payment_2_Amount__c, Payment_3_Amount__c
               from Opportunity where Id = :ApexPages.currentPage().getParameters().get('id')];
              
       EmailTemplate emailTemplate = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Id = :getTemplateId(opp.Product__r.Name)];
    
       htmlBody = emailTemplate.HtmlValue;
       subject = emailTemplate.subject;
    }

    public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.CCLPdf;
        pdf.getParameters().put('id',opp.id); 
        pdf.setRedirect(true);

        // Take the PDF content
        Blob b = pdf.getContentAsPDF();
        
        PageReference pdf2 =  Page.ProformaInvoicePdf;
        pdf2.getParameters().put('id',opp.id); 
        pdf2.setRedirect(true);

        // Take the PDF content
        Blob b2 = pdf2.getContentAsPDF();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(opp.account.Name+'-CCL.pdf');
        efa.setBody(b);
        
        Messaging.EmailFileAttachment efa2 = new Messaging.EmailFileAttachment();
        efa2.setFileName(opp.account.Name+'-Invoice.pdf');
        efa2.setBody(b2);


        
        // Sets the paramaters of the email
        email.setSubject(subject);
        email.setHtmlBody(htmlBody);
        //email.setTemplateId('00Xj0000000Yg9k');//getTemplateId(opp.Product__r.Name)
        email.setTargetObjectId(opp.Account.PersonContactId);
        email.setSaveAsActivity(true);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa,efa2});
        

        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        StoringAttachments();
        
        return new PageReference('/'+opp.Id);
    }
    
    public void StoringAttachments() {
        Attachment myAttach = new Attachment();
        myAttach.ParentId = opp.Account.PersonContactId;
        myAttach.name = opp.account.Name+'-CCL.pdf';
        PageReference myPdf = Page.CCLPdf;
        myAttach.body = myPdf.getContentAsPdf();
        insert myAttach;
        
        Attachment myAttach1 = new Attachment();
        myAttach1.ParentId = opp.Account.PersonContactId;
        myAttach1.name = opp.account.Name+'-Invoice.pdf';
        PageReference myPdf1 = Page.ProformaInvoicePdf;
        myAttach1.body = myPdf1.getContentAsPdf();
        insert myAttach1;
        } 
            
    public String getTemplateId(String prodName){
       template = [Select Name, Email_Template_Id__c from Product_EmailTemplates__c 
                        where Name = : prodName];
       return template.Email_Template_Id__c;
   }
}

Visualforce Page:
 
<apex:page standardController="Opportunity" extensions="SendEmail">
 <apex:messages />
    <apex:pageBlock >
        
        <apex:pageBlockTable value="{!Opportunity.Account.Name}" var="contact" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                {!Opportunity.Name}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Email</apex:facet>
                {!Opportunity.Email__c}
            </apex:column>
        </apex:pageBlockTable>
    
        <apex:form >
        <br /><br />
            <apex:outputLabel value="Subject" for="Subject"/>:<br />     
            <apex:inputText value="{!subject}" id="Subject" maxlength="80" size="100"/>
            <br /><br />
            <apex:outputLabel value="Body" for="Body"/>:<br />     
            <apex:inputtextarea richText="True" value="{!htmlBody}" id="Body" rows="10" cols="80" />        
            <br /><br /><br />
            <apex:outputText value="{!Opportunity.Product__r.Name}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Tax_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Total_after_Tax__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.payment_1_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Payment_2_Amount__c}" rendered="false"/>
            <apex:outputText value="{!Opportunity.Payment_3_Amount__c}" rendered="false"/>
            <apex:commandButton value="Send Email" action="{!send}" />&nbsp;
            <apex:commandButton value="Cancel" action="{!Cancel}" /> 
        </apex:form><br/>
        <apex:pageBlockSection title="Attachments" columns="1">
            <apex:outputLink value="{! $Page.CCLPdf}" target="_blank">{!Opportunity.Name} CCL.pdf</apex:outputLink>
            <apex:outputLink value="{! $Page.ProformaInvoicePdf}" target="_blank">{!Opportunity.Name} ProformaInvoice.pdf</apex:outputLink>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Don't forget to Create a Button on Opportunity.

I hope, it helps a lot.

Thanks
Ranjith
This was selected as the best answer