Send Email using Wrapper Classes in Salesforce.

Send Email using Wrapper Classes in  Salesforce.


Requirement:  Create a table in which there are list of contact name , email . when user select check box against any row of contact list and after that click on button then send the email to  that particular email id 

<!--WrapperDemoTestOnContact-->  Create Visual force page


<apex:page standardController="Contact" extensions="WrapperClsOnContact">
    <apex:form >
    <apex:pageMessages >
    </apex:pageMessages>
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrapperObj}" var="x">
                <apex:column value="{!x.conobj.name}"/>
                <apex:column value="{!x.conobj.email}"/>
                <apex:column >
                  <apex:inputcheckbox value="{!x.checkBox }"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockSection >
            <apex:commandButton value="SendEmail" action="{!sendEmail}"/>
            
            </apex:pageBlockSection>
            </apex:pageBlock>
    </apex:form>
</apex:page>





                           <!--WrapperClsOnContact .cls-->




public with sharing class WrapperClsOnContact {

    public List<WrapperClassEx> WrapperList{get;set;}
    
    public WrapperClsOnContact(ApexPages.StandardController controller) {
        
    }
     
    public List<WrapperClassEx> getwrapperObj(){
        List<Contact> conList =[select id, name, email from contact limit 5];   
        WrapperList = new List<WrapperClassEx>();
        for(Contact con: conList){
            WrapperList.add(New WrapperClassEx(con,false));  
        }
        return WrapperList;  
    }
    
    public class WrapperClassEx{
       public Contact conObj{get;set;}
       public Boolean checkBox{get;set;}
       public WrapperClassEx(Contact conRec, boolean SelectedBox){               
          conObj= conRec;
          checkBox = SelectedBox;
       }
    }
        
       public void sendEmail(){
         List<Messaging.SingleEmailMessage> lstEmailId=new List<Messaging.SingleEmailMessage>();
         for(WrapperClassEx w: WrapperList){
            if(w.checkBox == true){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setToAddresses(new String[] {w.conObj.Email});
                mail.setReplyTo('sumit.shukla@magicsw.com');
                mail.setplainTextBody('Hello');
                mail.setSenderDisplayName('Your Company Name');
                mail.setSubject('Test Email From Force.com Sites');
                lstEmailId.add(mail);                        
            }
          }
            if(lstEmailId.size()>0){
                try{
                    Messaging.sendEmail(lstEmailId);
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'Sent!'));
                }Catch(Exception ee){
                    ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error,ee.getMessage()));
                }
                
            }
       }     
}

OutPut:





If any query then please send the mail @:  sumitshukla.mca@gmail.com
               























































































Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page