wrapper class examples in salesforce

Wrapper Class

  • The wrapper class is a class that contains the collection of objects or different objects to store the manipulated values.
  • The wrapper class will have fields and different data types as Objects.


Apex Controller

public class wrapperClassController {

//Our collection of the class/wrapper objects cContact
public List<cContact> contactList {get; set;}

//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts() {
if(contactList == null) {
contactList = new List<cContact>();
for(Contact c: [select Id, Name, Email, Phone from Contact ]) {
// As each contact is processed we create a new cContact object and add
                it to the contactList
contactList.add(new cContact(c));
}
}
return contactList;
}


public PageReference processSelected() {

//We create a new list of Contacts that we be populated only with Contacts
        if they are selected
List<Contact> selectedContacts = new List<Contact>();

//We will cycle through our list of cContacts and will check to see if the selected
        property is set to true, if it is we add the Contact to the selectedContacts list
for(cContact cCon: getContacts()) {
if(cCon.selected == true) {
selectedContacts.add(cCon.con);
}
}

// Now we have our list of selected contacts and can perform any type of logic we want,
         sending emails, updating a field on the Contact, etc
System.debug('These are the selected Contacts...');
for(Contact con: selectedContacts) {
system.debug(con);
}
contactList=null; // we need this line if we performed a write operation
         because getContacts gets a fresh list now
return null;
}


// This is our wrapper/container class. A container class is a class, a data structure,
     or an abstract data type whose instances are collections of other objects.
//In this example a wrapper class contains both the standard salesforce object Contact
    and a Boolean value
public class cContact {
public Contact con {get; set;}
public Boolean selected {get; set;}

//This is the contructor method. When we create a new cContact object we pass a
        Contact that is set to the con property. We also set the selected value to false
public cContact(Contact c) {
con = c;
selected = false;
}
}
}

Visualforce Page

<apex:page sidebar="false" controller="wrapperClassController">
<!--VF PAGE BLOCK-->
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!ProcessSelected}" value="Show Selected accounts" reRender="block2"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="2">
<apex:pageBlockTable value="{!wrapaccountList}" var="waccl">
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox />
</apex:facet>
<apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
</apex:column>
<apex:column value="{!waccl.accn.name}"/>
<apex:column value="{!waccl.accn.phone}"/>
<apex:column value="{!waccl.accn.billingcity}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!selectedAccounts}" var="sa" id="block2">
<apex:column value="{!sa.name}"/>
<apex:column value="{!sa.phone}"/>
<apex:column value="{!sa.billingcity}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Popular posts from this blog

How to extract information from xml using node.js

salesforce questions and answers

How to clone a record using custom button in salesforce