wrapper class in salesforce

What is Wrapper Class in Salesforce - Everything you Need to Know

Wrapper Class is a class within a class that stores a group of different or similar data type values into a single object.

In other words, it is a collection of data members only without methods or user-defined data types.

Example:

Suppose that we have a List of Mobile phones like the iPhone or Nokia. Now we want to store the characteristics of each Mobile like display size, color, etc. In that case, we will use the wrapper class to store the different data types of each mobile in a single object.

dont miss out iconDon't forget to check out: Creating Wrapper Class in Salesforce Visualforce Pages

Benefits Of Wrapper Class in Salesforce:

  • With the use of Wrapper class, we can store the data from multiple sObject.
  • With the use of Wrapper class, we don’t require multiple lists of different sObject.
  • Code re-usability.
  • We can use wrapper class in Visualforce Page, Lightning Component and we can also convert the JSON string into wrapper class.
  • We can use multiple wrapper classes in a single Apex Class.
  • We can use a nested wrapper class in a single Apex Class.

Example 1: We can use Multiple Wrapper Classes in a Single Apex Class

Apex Class:

public class Wrapper1 {
    public string name {set;get;}
    public Integer age {set;get;}
}

public class Wrapper11 {
    public decimal sal {set;get;}
    public integer exp {set;get;}
    public Wrapper1 W1 {set;get;}
    public Wrapper11(){
        W1 = new Wrapper1();
        sal = 30000;
        exp = 5;
        W1.name = 'John';
        W1.age = 50;
    }
}

Visualforce Page:

<apex:page controller="Wrapper11">
    <apex:form> 
        Name: {!W1.name}<br/>
        Age: {!W1.age}<br/>
        Sal: {!sal}<br/>
        Exp: {!exp}<br/>
    </apex:form>
</apex:page>

Output:

Name: John
Age: 50
Sal: 30000
Exp: 5

Example 2: Nested Wrapper Classes in a Single Apex Class with Logic

Apex Class:

public class Wacc {
    public string name {set;get;}
    public string industry {set;get;}
    public String rating {set;get;}
}
public class Wopty {
    public string name {set;get;}
    public string stagename {set;get;}
    public date closedate {set;get;}
    public Wacc acc {set;get;}
}
public class WAccOpty {
    public Wopty opty {set;get;}
    public WAccOpty(){
        opty=new Wopty();
        opty.acc=new Wacc();
    }
    public void accs(){
        if(opty.acc.industry=='Banking'){
            opty.acc.rating='Hot';
        }
        else {opty.acc.rating='cold';}
    }
    public void optys(){
        if(opty.stagename=='close won'){
            opty.closedate=system.today();
        }  
        else{opty.closedate=system.today()+15;}
    }
}

Visualforce Page:

<apex:page controller="WAccOpty">
    <apex:form>
        <apex:pageblock Id="pb">
            <apex:commandButton value="accs" action="{!accs}" rerender="pb"/>
            <apex:pageblocksection Title="Account" collapsible="false">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Name"></apex:outputLabel>
                    <apex:inputText value="{!opty.acc.name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Industry"></apex:outputLabel>
                    <apex:inputText value="{!opty.acc.industry}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!opty.acc.rating}"></apex:outputLabel>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
            <apex:commandButton value="optys" action="{!optys}" rerender="pb"/>
            <apex:pageblocksection Title="Opportunity" collapsible="false">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Name"></apex:outputLabel>
                    <apex:inputText value="{!opty.name}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="stagename"></apex:outputLabel>
                    <apex:inputText value="{!opty.stagename}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!opty.closedate}"></apex:outputLabel>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

What is Wrapper Class in Salesforce - Example 2 Output

dont miss out iconCheck out another amazing blog by Kirandeep here: Queueable Apex in Salesforce - Concept to Implementations

Example 3: Nested Wrapper Classes with sObjects in a Single Apex Class and creating a record in multiple sObjects

Apex Class:

public class Wstd1 {
    public account acc {set;get;}
}
public class Wstd2 {
    public contact con {set;get;}
    public Wstd1 aa {set;get;}
}
public class Wstd3 {
    public Wstd2 bb {set;get;}
    public Wstd3(){
        bb=new Wstd2(); 
        bb.con=new contact();
        bb.aa=new Wstd1();
        bb.aa.acc=new account();
    }
    public Void Create(){
        insert bb.aa.acc;
        insert bb.con;
    }
}

Visualforce Page:

<apex:page controller="Wstd3">
    <apex:form>
        <apex:pageblock>
            <apex:commandButton value="save" action="{!Create}"/>
            <apex:pageblocksection Title="account">
                <apex:inputfield value="{!bb.aa.acc.name}"></apex:inputfield>
            </apex:pageblocksection>
            <apex:pageblocksection Title="contact">
                <apex:inputfield value="{!bb.con.lastname}"></apex:inputfield>
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output:

What is Wrapper Class in Salesforce - Example 3 Output

Happy Salesforce Coding!

Popular Salesforce Blogs