Wrapper Class in Apex Salesforce

A wrapper class in Salesforce Apex is a custom class that allows you to combine different types of data into a single object. It's particularly useful when you need to work with complex data structures or display multiple pieces of information in a Visualforce page. 

Wrapper classes are commonly used in scenarios like:

Visualforce Pages: You can use wrapper classes to display data from multiple related objects in a single table or form on a Visualforce page.

Apex Controllers: Wrapper classes can be used to pass complex data structures between Apex controllers and Visualforce pages.

Lightning Components: In Lightning development, wrapper classes are often used to format data for display in Lightning components or to simplify the structure of data passed between components.

Integration: When working with external systems or APIs, wrapper classes can help structure data in a way that's easier to work with in Apex code.

Here's an example of how you can use a wrapper class with Visualforce in Salesforce:

Define the Wrapper Class in Apex:

First, define the wrapper class in your Apex controller. This class will contain properties for the data you want to display on the Visualforce page.

public class AccountWrapper {
    public Account acc { get; set; }
    public Contact con { get; set; }

    public AccountWrapper(Account acc, Contact con) {
        this.acc = acc;
        this.con = con;
    }

}

Retrieve Data and Populate Wrapper Objects:

In your Apex controller, query the data you want to display on the Visualforce page and populate instances of the wrapper class with the queried records.

public class MyController {
    public List<AccountWrapper> accountWrappers { get; set; }

    public MyController() {
        accountWrappers = new List<AccountWrapper>();

        List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 10];
        List<Contact> contacts = [SELECT Id, Name, Email FROM Contact WHERE AccountId IN :accounts];

        for (Integer i = 0; i < accounts.size(); i++) {
            accountWrappers.add(new AccountWrapper(accounts[i], contacts[i]));
        }
    }
}

Visualforce Page to Display Data:

Create a Visualforce page (MyPage) to display the data using the wrapper class.

<!-- MyPage.page -->
<apex:page controller="MyController">
    <apex:pageBlock title="Accounts and Contacts">
        <apex:pageBlockTable value="{!accountWrappers}" var="wrapper">
            <apex:column headerValue="Account Name">
                <apex:outputText value="{!wrapper.acc.Name}" />
            </apex:column>
            <apex:column headerValue="Contact Name">
                <apex:outputText value="{!wrapper.con.Name}" />
            </apex:column>
            <apex:column headerValue="Contact Email">
                <apex:outputText value="{!wrapper.con.Email}" />
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

In this Visualforce page, we iterate over the list of AccountWrapper instances and display the account name, contact name, and contact email in a page block table.

Access the Visualforce Page:

Access the Visualforce page (MyPage) through your Salesforce org's URL or through a custom tab in Salesforce.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach