Wednesday, October 16, 2013

Wrapper class a Beginner's walkthrough.








 Have been out for quite some time it’s been a very long time. Since I posted programming content a long time ago, I’m back with some delicious content. Wrapper classes Wrapper generally refers to a type of packaging a content. When I tried to pull the data from unrelated different object many suggested me to use the concept of wrapper classes. I did some case studies about inner classes in Java back in my college really long time ago and in the middle I changed my profession and again back as a programmer. So everything started flying above my head. No simple content I found for creating wrapper classes so at that point I decided to create a proper blog which will help beginners to grasp the usage of this technique. Here we are gonna wrap data in to a single programming unit, Hence this type of programming technique is called wrapper classes.

What is a wrapper class?
In Salesforce we have the concept of collections where different data types like string, SObjects are stored, Iterated and used as per the business logic. So these primitive data types are something standard like these standard data types we can use the properties of custom defined data types using the concept of wrapper classes. Hence we can query do some business logic in the collection elements with custom data type.

When to use Wrapper classes?
As per my experience we can use wrapper class collection elements. If we need to iterate through a data which is stored in a multiple Sobjects and datatypes. For example I wanted to do a pagination where have to display a list of records next to a check box, so that user can choose the check box which means the user has selected a particular record. In this case we are gonna add the Boolean value of the check box and record id in to a list with custom data type.

Structure of wrapper classes:

Forget pagination I’m gonna demonstrate a simple example of displaying data from unrelated multiple objects so the article works like a primer for the concept of wrapper classes. below is the simple interface i gonna create for the demo.if I press the fetch data button the page is gonna display the data from multiple objects which is not related with each other.  Something like this






 Here fetch data and clear data are the buttons which will work as the switches for the mechanism to fire.
here is the controller code to achieve this

Controller Code:

 
//////<<<<<<<<<<<<<<>>>>>>>>>>>>>>////////
/////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();  
 }  
 }  

And this is the VF page. Inside Apex repeat tag I have added the wrapper list “disp_list” Which will have the wrapped up data of all the s objects used in the controller.

 <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>  

Thus the value inside a wrapper list is accessed using the alias name in the list “ttt”. Feel free to write to me if any questions.


2 comments:

Unknown said...

Very simple and informative explanation. Was very much useful. Keep up the good work!!...

Anonymous said...

This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post. ivrs