Friday, May 11, 2012

Send email to multiple contacts in Salesforce

Task:-- SendEmail() to Multiple Contacts onclick on CheckBox Corresponding to them through use of Wrapper Class

Description:-- In this task we have to perform mainly three different work and they are :--
1. Firstly we have to make a Wrapper Class for getting Table of Contacts with Check Box  corresponding to them.
2. After getting all records we have to use JavaScript for getting Id corresponding to each selected Check Box, Same  for Select All functionality and in the Last for placing Email of contacts for which check box is selected and placing it in To Section of Email.
3. Finally we have to write code for sending Email.

So, Apex Class for this is looking like this:---

public class CheckBoxEmailAssignmentPage{
    public List<Contact> allContacts{get;set;}
    public List<ContactModel> modelList{get;set;}
    public string to{get;set;}
    public List<string> addresses{get;set;}
    public String fromAdd{set;get;}
    public String body{set;get;}
    public String subject{get;set;}
    public CheckBoxEmailAssignmentPage(){
        addresses = new List<String>();
        integer  i =0;
        allContacts = [SELECT id, FirstName,LastName, email from Contact];
        modelList = new List<ContactModel>();
        for(Contact con :allContacts){
        if(con.Email!=null){
            ContactModel cm = new ContactModel();
            cm.srno = ++i;
            cm.con = con;
            modelList.add(cm);
            }
        }
    }
    public void sendEmail(){
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        addresses = to.split(',');
        email.setInReplyTo(fromAdd);
        email.setSubject(subject);
        email.setToAddresses(addresses);
        email.setPlainTextBody(body);
        emails.add(email);
        Messaging.sendEmail(emails);
    } 
    public class ContactModel{
        public Integer srno{get;set;}
        public Contact con{set;get;}
        public String text{get;set;}   
        public ContactModel(){
            srno = 0;
            con = new Contact();
        }
    }
}


Page:--

<apex:page controller="CheckBoxEmailAssignmentPage" id="pg">
    <apex:form id="frm">
     <apex:actionStatus id="st" startText="Email Sending....." startStyle="color:red"></apex:actionStatus>
        <script>
            function checkAll(cb,cbid){
                var inputElem = document.getElementsByTagName("input");
                if(inputElem.id != 'pg:frm:pb:pbt:allcheck'){
                for(var i=0; i<inputElem.length; i++){
                    if(inputElem[i].id.indexOf(cbid)!=-1){
                        inputElem[i].checked = cb.checked;
                    }
                }
                }
            }
            function selectedContacts(){
                var divObj = document.getElementById("contacts");
                var inputs = divObj.getElementsByTagName("input");
                var emails = '';
                var comma = "";
                if(inputs.length > 0 ){
                    for(i=0; i<inputs.length; i++){
                        if(inputs[i].type == "checkbox" && inputs[i].checked && inputs[i].id != 'pg:frm:pb:pbt:allcheck'){
       
                            emails += comma + inputs[i].value;
                            comma = ",";
                        }
                    }
                }
                 document.getElementById('pg:frm:pbs:pbs1:to').value=emails;
                 document.getElementById('pg:frm:pbs:pbs1:to1').value=emails;
            }
        </script>  
        <apex:pageBlock title="All Contacts" id="pb">
        <apex:actionFunction name="sendEmail" action="{!sendEmail}" reRender="frm" status="st"/>
        <apex:pageBlockButtons >
            <apex:commandButton value="Send" onclick="sendEmail();return false;"/>
        </apex:pageBlockButtons>
        <div id="contacts">
            <apex:pageBlockTable value="{!modelList}" var="mCon" id="pbt">
                <apex:column headerValue="Srno" value="{!mCon.srno}"/>
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="checkAll(this,'chk')" id="allcheck" onchange="selectedContacts()"/>
                        </apex:facet>                
                    <input type="checkbox" id="chk" value="{!mCon.con.email}" onclick="selectedContacts()"/>
                </apex:column>
                <apex:column headerValue="First Name" value="{!mCon.con.FirstName}"/>
                <apex:column headerValue="Last Name" value="{!mCon.con.LastName}"/>
                <apex:column value="{!mCon.con.Email}"/>
            </apex:pageBlockTable> 
        </div>         
        </apex:pageBlock>
        <apex:pageBlock id="pbs" rendered="true">
            <apex:pageBlockSection id="pbs1">
                <apex:outputLabel >From </apex:outputLabel>
                <apex:inputText value="{!fromAdd}"/>
                <apex:outputLabel >To </apex:outputLabel>
                <apex:inputText value="{!to}" id="to"/>
                <apex:outputText value="{!to}" id="to1"/>
                <apex:outputLabel >Subject </apex:outputLabel>
                <apex:inputText value="{!subject}"/>
                <apex:outputLabel >Message </apex:outputLabel>
                <apex:inputTextarea value="{!body}" rows="5"/>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>




Run this Code and send Email to selected Contacts and enjoy coding.

                                                                ======Abhinav Sharma===========


4 comments:

  1. Very useful post for improving basics of sendEmail().

    ReplyDelete
  2. great, can this be used in oracle apex?
    I would like to do the exact thing in my application.

    ReplyDelete
  3. @Nalini Please refer this Link for doing the same through Apex-Oracle

    http://www.dba-oracle.com/htmldb/t_htmldb_sending_multiple_email_select_lov.htm

    ReplyDelete
  4. Thanks Dear it is really a nice example.

    ReplyDelete