Home > Blog > Wrapper Class in Salesforce

Wrapper Class in Salesforce

A wrapper class is a data structure which contains objects and collection of objects and its data member. Wrapper class has different type of data or property as per requirement. Important use case of wrapper class is to show table of data with checkbox and then process only the records that are selected. Examples:
  1. Wrapper class, controller and visual force page.
  2. Test class of Wrapper class.
Select contacts from contacts list and then send mail to selected contacts emails.

Figure-1

1.Wrapper class, controller and visual force page

Apex class with wrapper class to send email to selected or checked contact’s emails:

public class SendMailpageClass {   public list<Wrapper> ListWrap{get;set;} public list<contact> listcon{get;set;} //=====================class Constructor public SendMailpageClass(){ list<contact> listcon = [select id,name,email from contact where email !=null limit 10]; //– contact list where email not null if(listcon.size()!=0){ ListWrap = new List<wrapper>(); //———- wrapper class object } for(contact c : listcon){ ListWrap.add(new wrapper(c,c.Email)); } } //===========================End of constructor List<String> selectedlst = new List<String>(); //——-selected Email list   public void sendMail(){ for(wrapper c : ListWrap){ if(c.checkbox == true){ selectedlst.add(c.email); //——– select contact’s email ids } } //======================== send email to contact’s selected emails list try { System.debug(‘I am here—–>>>’); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setSubject(‘Morning Wishes’); mail.setPlainTextBody(‘Good Morning’); mail.setToAddresses(selectedlst); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } catch(Exception e) { } } //========== wrapper class ==================== public class wrapper{ public contact con{get;set;} public string email{get;set;} public boolean checkbox{get;set;}   public Wrapper(contact con, string email) { this.con=con; this.email=email; checkbox=false; } } } Visualforce page : <apex:page controller=”SendMailpageClass”> <script type=”text/javascript”> function selectAllCheckboxes(obj,receivedInputID){ var inputCheckBox = document.getElementsByTagName(“input”); for(var i=0; i<inputCheckBox.length; i++){ if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){ inputCheckBox[i].checked = obj.checked; } } } </script> <apex:form > <apex:pageBlock > <apex:pageBlockTable value=”{!ListWrap}” var=”c”> <apex:column > <apex:facet name=”header”> <apex:inputCheckbox onclick=”selectAllCheckboxes(this,’inputId’)”/> </apex:facet> <apex:inputCheckbox value=”{!c.checkbox}” id=”inputId”> </apex:inputCheckbox> </apex:column> <apex:column value=”{!c.con.Name}”/> <apex:column value=”{!c.email}” headerValue=”Email”/> </apex:pageBlockTable> <apex:pageBlockButtons location=”bottom”> <apex:commandButton action=”{!sendMail}” value=”send”> </apex:commandButton> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>
  1. Test class of Wrapper class:
@istest public class TestClass1 { static testMethod void testMethod1() { Test.startTest(); list<contact> listcon=new list<contact>(); contact a=new contact(); a.LastName=’shahwaz’; a.Email  =’abc@gmail.com’; listcon.add(a); insert listcon; system.assertNotEquals(0, listcon.size()); SendMailpageClass w=new SendMailpageClass(); w.sendMail(); SendMailpageClass.Wrapper wr2 = new SendMailpageClass.Wrapper(listcon[0], ‘abc@gmail.com’); wr2.checkBox=true; Test.stopTest(); } }
sales
Enquire Now
X

lets get over a cup of coffee and discuss!