Wrapper Class With Example in Apex Salesforce

Wrapper Class in Apex Salesforce:
Wrapper class is collections of different data type, subject etc.

In following example  we are  bind Account ,Opportunity standard object. We query and perform

business logic on the Collection of elements across unrelated objects with the custom data type.


Visual Force Page:

<apex:page controller="wrapperDemoCtrl">

<apex:pageBlock title="Account From wrapper  Class">

   <apex:pageBlockTable value="{!wraccount}" var="wra">

   <apex:column value="{!wra.acc.Name}"/>

   </apex:pageBlockTable>

</apex:pageBlock>

<apex:pageBlock title="Opportunity From wrapper  Class">

   <apex:pageBlockTable value="{!wraoppn}" var="wropp">

   <apex:column value="{!wropp.op.Name}"/>

   </apex:pageBlockTable>

</apex:pageBlock>


</apex:page>

Controller :

public class wrapperDemoCtrl {

    public list<wrapperClass> wraplist{get;set;}
   
    public list<wrapperClass> getwraccount()
          {
            list<Account>acclist=[select Id,Name from Account limit 3];
            wraplist= new list<wrapperClass>();
            for(Account acn: acclist)
            {
            wraplist.add(new wrapperClass(acn));
            }
           return wraplist;
          }
    public list<wrapperClass> getwraoppn()
        {
         list<Opportunity>opplist=[select Id,Name from Opportunity limit 3];
         wraplist= new list<wrapperClass>();
         for(Opportunity opn:opplist )
         {
         wraplist.add(new wrapperClass(opn));
         }
         return wraplist;
        }
   
    public class wrapperClass{
   
        public Account acc {get;set;}
        public Opportunity op {get;set;}
       
        public wrapperClass(Account accn){
       
                 acc= accn;        
                     }
       public wrapperClass(Opportunity opn)
       {
         op=opn;
     
       }
    }
}

No comments:

Post a Comment