WHAT IS A WRAPPER CLASS IN APEX? | Techila
1935
single,single-post,postid-1935,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

WHAT IS A WRAPPER CLASS IN APEX?

WRAPPER CLASS IN APEX

WHAT IS A WRAPPER CLASS IN APEX?

A wrapper class is simply put a class which wraps variables of different data types into one single type.

We use List(account), List(string), List(integer) etc. But, what do you do when you want to create a list of all these three data types into one single list? That is when a wrapper class comes into help. Below is a simple code snippet to help understand this concept.

VF Page:

<apex:page controller=”wrapperClass” action=”{!onloadfunction}”>

<apex:pageBlock >

  <apex:pageblocktable value=”{!wrapperClassList}” var=”wc”>

      <apex:column headerValue=”String”>

          <apex:outputText value=”{!wc.sampleString}”></apex:outputText>

      </apex:column>

      <apex:column headerValue=”Integer”>

          <apex:outputText value=”{!wc.sampleInteger}”></apex:outputText>

      </apex:column>

      <apex:column headerValue=”Boolean”>

          <apex:outputText value=”{!wc.sampleBoolean}”></apex:outputText>

      </apex:column>

      <apex:column headerValue=”Account”>

          <apex:outputText value=”{!wc.sampleAccount.Name}”></apex:outputText>

      </apex:column>

      <apex:column headerValue=”Contact”>

          <apex:outputText value=”{!wc.sampleContact.LastName}”></apex:outputText>

      </apex:column>

  </apex:pageblocktable>

</apex:pageBlock>

</apex:page>

Apex Class:

public class wrapperClass

{

public class sampleWrapperClass

{

    public String sampleString {get; set;}

    public Integer sampleInteger {get; set;}

    public Boolean sampleBoolean {get; set;}

    public Account sampleAccount {get; set;}

    public Contact sampleContact {get; set;}

    public sampleWrapperClass(String sampString,Integer sampInteger,Boolean sampBoolean, Account a, Contact c)

    {

        this.sampleString = sampString;

        this.sampleInteger = sampInteger;

        this.sampleBoolean = sampBoolean;

        this.sampleAccount  = a;

        this.sampleContact  = c;

    }

}

public List<sampleWrapperClass> wrapperClassList {get; set;}

public void onloadfunction()

{

    wrapperClassList = new List<sampleWrapperClass>();

    Contact con = new Contact(LastName=’TestContact’);

    Account acc = new Account(Name=’TestAccount’);

    wrapperClassList.add(new sampleWrapperClass(‘Testing One’,23,false,acc,con));

}       

}

Analyzing the code:

The Wrapper Class in our example is named ‘sampleWrapperClass’. Every wrapper class has two parts. One part to declare the variables, and the other part to define the constructor for the class. In our example, we have only one constructor. However, you can have multiple constructors by altering the parameters based upon your needs. Note that we have created a list which holds Integer,String,Boolean,Account,Contact datatypes together into one single custom data type called ‘sampleWrapperClass’. You can then process this List as any other normal list. In our Visualforce page we display the list using a pageBlockTable.

sample Wrapper Class

Practical Examples:

A common application is to use wrapperClass to display a table with a List of Sobject records and a check box or radio button next to each record and enable the user to select records from the table and process.

Here is one such example: http://www.forcetree.com/2009/07/database-search-and-selection.html

Courtesy:  http://http://www.forcetree.com/2014/03/what-is-wrapper-class-in-apex.html


https://www.minddigital.com/wp-content/uploads/2013/11/wrapper-class-300×218.jpg

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment