0

Apex Wrapper Class: a Beginner’s walkthrough

Apex Wrapper class–

a Beginner’s Walk.

In Apex, wrapper classes refer to a type of content packaging.

When I first tried to pull data from unrelated objects, my mentors suggested that I use the concept of ‘wrapper classes’. Long-ago-back-in-college, I did some case studies about inner classes in Java. In the middle of this,  I changed my profession and everything about wrapper classes flew out my head. When, I came back, again, as a programmer, I couldn’t find simple instructions for creating wrapper classes. It was at that point, that I decided to create a proper blog to help beginners grasp the usage of this technique.

We are going to wrap data in to a single programming unit and I’m going to make it simple to understand!

What is a wrapper class?

In Salesforce, we have the concept of Collections where different data types like string and SObjects are stored, iterated and used per the business logic. These primitive data types can be collectively addressed using Collections. The limitation here is that we can apply the collection framework only to these primitive data types like S-Object, or an Integer, String etc. However, we can use Wrapper Classes to address the properties of Custom data types i.e. a data type can be defined as a Custom type just like e-num operator in the programming languages. We query and perform business logic on the Collection of elements across unrelated objects with the custom data type.

wrapp1

 

When to use Wrapper classes?

If we need to iterate through data which is stored in multiple Sobjects and datatypes, we can use Wrapper Class Collection elements.

For example, if I wanted to create a pagination to display a list of records each next to a check box, I could collect the Boolean value of the check box and the related record id in to a list with a custom data type.

Forget pagination!~  I’m going to demonstrate a simpler example of displaying data from unrelated multiple objects. Below is a simple interface, that demonstrates the Wrapper Class. In the example below, if I press the ‘Fetch Data’ button, the page will display the data from multiple objects, none of them related to each other. The ‘Fetch Data’ and the ‘Clear List’ buttons work as the switches for the mechanism to fire.

wrapp2

listwrap3

The Controller Code that made all of that happen:

 
//////<<<<<<<<<<<<<<>>>>>>>>>>>>>>////////
/////Created by Nirmal Christopher////////
//////<<<<<<<<<<<<<<>>>>>>>>>>>>>>////////
public class sun_example {  
 public list<wrapperclass> disp_list{get;set;}  
 //Declare a wrapper class  
      public class wrapperclass{  
      public VF_practice_pages__c vf1{get;set;}  
     //custom wrapper datatype  
      public string name{get;set;}  
      public string ssn_number{get;set;}  
      public string name1{get;set;}  
      public string productName{get;set;}  
    }   
   public sun_example() {  
 //define constructor to instantiate the wrapper data type  
   disp_list=new List<wrapperclass>();  
   }  
   //Method for processing logic  
   public void generate_data(){  
 //querying different objects accounts, VF_practice_pages__c, product2 which is not related at all  
     List<account>acc=[select id,Name from account where createddate!=null order by name limit 5];  
     list<VF_practice_pages__c>vfpp=[select id,name,name1__c,SSN_Number__c from VF_practice_pages__c where createddate!=null order by name limit 5 ];  
     list<product2>proList= [SELECT IsActive,Name FROM Product2 WHERE IsActive = true order by name limit 5 ];  
 //Iterate through each list to extract the values and add it to the custom wrapper data type  
       for(account accs:acc){  
         for(integer i=0;i<vfpp.size();i++){  
           for(integer k=0;k<proList.size();k++){  
 //Instantiating the wrapper SObject   
           wrapperclass w=new wrapperclass();  
 //Assigning the wrapper variables from the SObject Fields in the database.  
           w.ssn_number=vfpp[i].SSN_Number__c ;  
           w.productName=proList[k].name;  
           w.name1=vfpp[i].name1__c;  
 //Adding everthing to the List  
           disp_list.add(w);  
           w.name=accs.name;  
      disp_list.add(w);  
         }  
       }  
     }  
   }  
   public void cancel_data(){  
     disp_list.clear();  
 }  
 }  

 

Use of Apex Wrapper Class in a Visualforce page: Inside an Apex repeat tag, I have added the Wrapper Class list “disp_list”. It wraps up data of all the Sobjects used in the controller. The value inside This Wrapper list is accessed using the alias name in the list “ttt”.

 <apex:page controller="sun_example" showHeader="false" sidebar="false" >  
  <apex:form >  
  <apex:pageBlock >  
 <apex:pageBlockButtons location="both">   
 <apex:commandButton action="{!generate_data}" value="Fetch Data"/>   
 <apex:commandButton action="{!cancel_data}" value="Clear List"/>   
 </apex:pageBlockButtons>  
  <apex:pageBlockSection >  
   <table>  
  <tr>  
    </tr>  
   <tr>  
  <td><b>Account Name</b></td><td><b>Custom object field</b></td><td><b>custom object field</b></td><td><b>product name</b></td></tr>  
  <apex:repeat value="{!disp_list}" var="ttt">  
  <tr><td>{!ttt.name}</td>  
  <td>{!ttt.name1}</td>  
  <td>{!ttt.ssn_number}</td>  
  <td>{!ttt.productName}</td>  
 </tr>  
 </apex:repeat>  
  </table>  
  </apex:pageBlockSection>  
  </apex:pageBlock>  
  </apex:form>  
 </apex:page>  

 

Feel free to write to me if any questions.
Nirmal Christopher
SFDC Techical Consultant
twittergoogle_pluslinkedinrssyoutubetwittergoogle_pluslinkedinrssyoutube

No Comments Yet.

Leave a reply

You must be logged in to post a comment.

Subscribe to Blog
Extend Talent