+ Start a Discussion
sam_Adminsam_Admin 

Add Cc or To in Visualforce page

Initially when this was created we only had To, but now our users want to send to more than one contact, i tried to add Cc but it doesn't works, is this possible?

VF: <apex:page standardcontroller="Lead" extensions="DistributorEmailController" showheader="false" lightningstylesheets="true">
    <apex:form >
        <apex:pageBlock title="Please select the Distributor below">
    
            <!--<p>Fill out the fields below to test how you might send an email to a contact.</p>-->
            <br/>    
                <apex:pageMessages />
                <b><apex:outputLabel value="To" for="To"/>:</b><br /> 
                <apex:inputField value="{!leadRecord.Contact__c}" id="To" />
                <br />
                <b><apex:outputLabel value="Cc" for="Cc"/>:</b><br /> 
                <apex:inputField value="{!leadRecord.Contact__c}" id="Cc" />
                <br />                                
                <b><apex:outputLabel value="Subject" for="Subject"/>:</b><br />     
                <apex:outputText value="{!ET.subject}" id="Subject" />
                <br />
                <br />
               <!-- <b><apex:outputLabel value="Body" for="Body"/>:</b><br /> (READ-ONLY: if you want to make changes to content, please update Email template and try again.)-->
                <apex:inputTextArea value="{!htmlBody}" id="Body"  rows="30" cols="80" richText="true" disabled="true" /> 
                <br /><br /><br />
                <apex:commandButton value="Send Email" action="{!send}" />
                <apex:commandButton value="Cancel & Return" action="{!Cancel}" /> 
        </apex:pageBlock>     
    </apex:form>
</apex:page>

Apex Class: /*Controller to send This lead to any selected Contact */

public class DistributorEmailController {
    public Lead leadRecord {get;set;}   
    public String TemplateID {get;set;}
    public String subject { get; set; }
    public String body { get; set; }
    public String htmlBody { get; set; }          
    public EmailTemplate  ET{ get; set; }
    public DistributorEmailController(ApexPages.StandardController controller) {
        String lid = '';
        TemplateID = ''; 
        htmlBody ='';
        if(ApexPages.currentPage().getParameters().get('id') != null )
            lid = ApexPages.currentPage().getParameters().get('id');
         system.debug('lidid ' + lid );    
            
        try{             
            leadRecord = [select Status,ID,Contact__c,Name,Company,NumberOfEmployees,Street,City,State,PostalCode,Country,Website,Salutation,FirstName,
                         LastName,Title,Department__c,Email,Phone,MobilePhone,Fax,Product__c,Industry,Specialty__c,Rating,BuyingTimeframe__c,LeadSource,WebFormDetail__c,
                         Description,Marketing_Comments__c from Lead where id = : lid];
            //Change the Template name below.
            ET = [Select ID,HTMLValue,Subject from EmailTemplate  where   name = 'Distributor Lead Email' ];            
            if (ET != null){
                TemplateID  = ET.id;        
                htmlBody  = ET.HTMLValue ;
                Subject = ET.Subject;
                ReplaceTags();   
            }
        }
        catch(Exception e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,e.getMessage()));
        }
    }                  
    
    public void ReplaceTags(){
        
        if(htmlBody.indexOf('{!Lead.Id}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Id}',nullToString(leadRecord.Id));
                
        if(htmlBody.indexOf('{!Lead.Name}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Name}',nullToString(leadRecord.Name));
                    
        if(htmlBody.indexOf('{!Lead.Company}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Company}',nullToString(leadRecord.Company));
        
        if(htmlBody.indexOf('{!Lead.NumberOfEmployees}') >-1)
            htmlBody = htmlBody.replace('{!Lead.NumberOfEmployees}',nullToString(String.valueOf(leadRecord.NumberOfEmployees)));
            
        if(htmlBody.indexOf('{!Lead.Address}') >-1){
            String Address = '' ;
            Address =  Address  + nullToString(String.valueOf(leadRecord.Street));
            Address =  Address  + ' ' + nullToString(leadRecord.City);
            Address =  Address  + ','+ nullToString(leadRecord.State);
            Address =  Address  + ' ' + nullToString(leadRecord.PostalCode);
            Address =  Address  + ' ' + nullToString(leadRecord.Country);
            htmlBody = htmlBody.replace('{!Lead.Address}',Address);
            
        }
        if(htmlBody.indexOf('{!Lead.Street}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Street}',nullToString(String.valueOf(leadRecord.Street)));
            
        if(htmlBody.indexOf('{!Lead.City}') >-1)
            htmlBody = htmlBody.replace('{!Lead.City}',nullToString(leadRecord.City));
        
        if(htmlBody.indexOf('{!Lead.State}') >-1)
            htmlBody = htmlBody.replace('{!Lead.State}',nullToString(leadRecord.State));
            
        if(htmlBody.indexOf('{!Lead.PostalCode}') >-1)
           htmlBody = htmlBody.replace('{!Lead.PostalCode}',nullToString(leadRecord.PostalCode));
           
        if(htmlBody.indexOf('{!Lead.Country}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Country}',nullToString(leadRecord.Country));
            
        if(htmlBody.indexOf('{!Lead.Website}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Website}',nullToString(leadRecord.Website));
            
        if(htmlBody.indexOf('{!Lead.Salutation} ') >-1)
            htmlBody = htmlBody.replace('{!Lead.Salutation} ',nullToString(leadRecord.Salutation) );
            
        if(htmlBody.indexOf('{!Lead.FirstName} ') >-1)
            htmlBody = htmlBody.replace('{!Lead.FirstName} ',nullToString(leadRecord.FirstName) );
            
        if(htmlBody.indexOf('{!Lead.LastName}') >-1)
            htmlBody = htmlBody.replace('{!Lead.LastName}',nullToString(leadRecord.LastName));
            
        if(htmlBody.indexOf('{!Lead.Title}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Title}',nullToString(leadRecord.Title));
            
        if(htmlBody.indexOf('{!Lead.Department__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Department__c}',nullToString(leadRecord.Department__c));
        
        if(htmlBody.indexOf('{!Lead.Email}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Email}',nullToString(leadRecord.Email));
            
        if(htmlBody.indexOf('{!Lead.Phone}') >-1)
           htmlBody = htmlBody.replace('{!Lead.Phone}',nullToString(leadRecord.Phone));
           
        if(htmlBody.indexOf('{!Lead.MobilePhone}') >-1)
            htmlBody = htmlBody.replace('{!Lead.MobilePhone}',nullToString(leadRecord.MobilePhone));
            
        if(htmlBody.indexOf('{!Lead.Fax}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Fax}',nullToString(leadRecord.Fax));
            
        if(htmlBody.indexOf('{!Lead.Product__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Product__c}',nullToString(leadRecord.Product__c));
            
        if(htmlBody.indexOf('{!Lead.Industry}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Industry}',nullToString(leadRecord.Industry));
            
        if(htmlBody.indexOf('{!Lead.Specialty__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Specialty__c}',nullToString(leadRecord.Specialty__c));
            
        if(htmlBody.indexOf('{!Lead.Rating}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Rating}',nullToString(leadRecord.Rating));
            
        if(htmlBody.indexOf('{!Lead.BuyingTimeframe__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.BuyingTimeframe__c}',nullToString(leadRecord.BuyingTimeframe__c));
            
        if(htmlBody.indexOf('{!Lead.LeadSource}') >-1)
            htmlBody = htmlBody.replace('{!Lead.LeadSource}',nullToString(leadRecord.LeadSource));
            
        if(htmlBody.indexOf('{!Lead.WebFormDetail__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.WebFormDetail__c}',nullToString(leadRecord.WebFormDetail__c));
            
        if(htmlBody.indexOf('{!Lead.Description}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Description}',nullToString(leadRecord.Description));
            
        if(htmlBody.indexOf('{!Lead.Marketing_Comments__c}') >-1)
            htmlBody = htmlBody.replace('{!Lead.Marketing_Comments__c}',nullToString(leadRecord.Marketing_Comments__c));
                    
    }
    public PageReference send() {
     List<Messaging.SingleEmailMessage> email = new List<Messaging.SingleEmailMessage>();
     
        if(leadRecord.Contact__c == null){
             ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Please select contact and Try again.'));
             return null;   
        }
     
        if (TemplateID != null){
            // Query Contact Email ID
             try{
                Contact c =  [Select Id,Email,Name from Contact where ID =: leadRecord.Contact__c]; 
                if (c.Email != null && c.Email != ''){ 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        
                      if(htmlBody.indexOf('{!Contact.Name}') >-1)
                          htmlBody = htmlBody.replace('{!Contact.Name}',nullToString(c.name));
                        String[] toAddresses = new String[] {c.email};
                        String[] ccAddresses = new String[] {c.email};
                        mail.setTargetObjectId(c.id);
                        mail.setSubject(Subject);
                        mail.setToAddresses(toAddresses);
                        mail.setCcAddresses(ccAddresses);
                        mail.setHtmlBody(HtmlBody); 
                        mail.setsaveAsActivity(true);
                        mail.setTreatTargetObjectAsRecipient(false); 
                        mail.setTargetObjectId(LeadRecord.Id);                                     
                    email.add(mail);
                    Messaging.sendEmail(email);                      
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email sent successfully!'));
                    
                    leadRecord.Status = 'Dealer (ISR Only)';
                    update leadRecord;
                }
                else{
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Selected Contact has no Valid Email Address, kindly update the contact and Try again.'));
                    return null;   
                }
             }catch(Exception e){}
        }
                
        return new PageReference('/' + leadRecord.id);  
    }
    public String nullToString(String s){
        if (s== null)
            return '';
        else 
            return s;           
    }
    public PageReference cancel(){
        return new PageReference('/' + leadRecord.id);  
    }
}