Wrapper Classes in Apex S...

What is Wrapper Class (Salesforce)?

A quick search for Salesforce wrapper results in the following definition.Wrapper class in Apex Salesforce. A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members. A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties.

That’s fine but if you’re new to the concept then it is likely that the definition of a Salesforce wrapper class is not as useful as a practical example of the logic.

For the page you will require a controller similar to the following:

For example in Salesforce we have custom objects, In custom objects, we store different types of field. Likewise Wrapper class is the custom object defined by a salesforce developer.

When to use Wrapper Class?:

Suppose You have to store account corresponding to the primary contact. In that case, you have to use the wrapper class because in general what we have collections like List which can hold only one Object type-
 List acc = new List();  Using List we won’t be able to store Accounts and Contacts together, their wrapper class comes to rescue, we can declare Account and contact as wrapper class members and then use that class as a collection. Class wrapperClass{
Public Account a;
Public Contact c;
} And then use this wrapper class as a collection type. List wrap = new List();

How to use the Wrapper Class?:

Here is an example, to show the accounts that are selected by the checkbox shown on the right side of the page.
Create a visualforce page which displays the accounts and checkbox.

Create an apex class which has a wrapper class with Account and checkbox selection as its properties.

 public class WrapClass {
//list of wrapper class type
public List wrapaccountList{get;set;}
//Accounts which are selected will be stored in this list
public List selectedAccounts{get;set;}
//constructor
public WrapClass(){
//Initalize the list
wrapaccountList = new List();
//reterive Account records
for(Account a:[Select id, Name, Phone, BillingCity From Account limit 10]){
//Add the accounts to the wrapper class list
wrapaccountList.add(new wrapAccount(a));
}}
//When button is clicked this method will get executed
public void processSelected(){
selectedAccounts =new List();
//for every record in the wrapper class list add the records in selectedAccounts list //whose checkbox are selected
for(wrapAccount wrapObj: wrapaccountList){
if(wrapObj.isSelected == true){
selectedAccounts.add(wrapObj.accn);
}}}
//Define Wrapper Class
public class wrapAccount{
public Boolean isSelected {get;set;}
public Account accn {get;set;}
public wrapAccount(Account a ){
accn =a ;
isSelected =false;
}
wrap account}

Here wrap account is wrapper class which has checkbox isSelected and Account object as its properties. The table will iterate through the list of the wrapper class.

Next Post

Share this...
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Post a Comment

Your email address will not be published. Required fields are marked *