G-8E0CBM58L7

Wrapper Class in Apex – A Complete Guide for Salesforce Developers

  • Home
  • / Wrapper Class in Apex – A Complete Guide for Salesforce Developers
Wrapper Class in Apex

In Salesforce Apex programming, organizing data efficiently is crucial for building scalable and maintainable applications. One powerful concept that helps developers achieve this is the Wrapper Class in Apex. This guide will walk you through what a wrapper class is, its use cases, benefits, and how to implement it with real-life examples.

What is a Wrapper Class in Apex?

A wrapper class in Apex is a custom class that is used to wrap or contain multiple fields or objects into a single unit. Essentially, it’s a container class that can hold different data types or even complex objects like Account, Contact, or custom classes.

This concept becomes especially useful when dealing with dynamic data sets in Visualforce pages or LWC components, allowing developers to manipulate and display data efficiently.

Why Use Wrapper Class in Apex?

Using wrapper classes provides several advantages in Apex development:

  • Combines multiple objects or fields into one logical unit

  • Enables easy iteration over complex data in Visualforce pages

  • Helps in tracking additional states like checkbox selection, row-level actions, etc.

  • Enhances code readability and modularity

Real-Life Use Case

Imagine you want to display a list of contacts with a checkbox next to each for selection. A wrapper class helps you pair each contact with a Boolean field indicating whether it’s selected or not.

apex
public class ContactWrapper {
public Contact con { get; set; }
public Boolean isSelected { get; set; }
public ContactWrapper(Contact c) {
this.con = c;
this.isSelected = false;
}
}

In your controller:

apex
public with sharing class ContactController {
public List<ContactWrapper> contactList { get; set; }
public ContactController() {
contactList = new List<ContactWrapper>();
for(Contact c : [SELECT Id, Name, Email FROM Contact LIMIT 10]) {
contactList.add(new ContactWrapper(c));
}
}
}

You can now use contactList in your Visualforce page and allow users to select multiple rows easily.

When to Use Wrapper Classes in Salesforce

Use wrapper classes in Apex when:

  • You need to handle a combination of multiple standard/custom objects

  • You want to bind checkboxes or custom input to lists of records

  • You require complex logic or filtering in your UI that standard SOQL results can’t offer directly

Best Practices

  • Always define constructors in your wrapper classes for clean initialization

  • Use with sharing keyword in controller classes for respecting org-wide defaults

  • Keep wrapper classes lean — only include necessary fields

  • Add debug logs during iterations to ensure correct data mapping

Limitations of Wrapper Classes

Although powerful, wrapper classes can add complexity if not used wisely. Avoid over-nesting or combining too many objects, which may lead to performance or readability issues.

FAQs

Q1. Can I use wrapper classes in LWC or Aura Components?
Yes, wrapper classes can be used to format data sent to the frontend (like LWC or Aura) via Apex controllers.

Q2. Are wrapper classes Salesforce-specific?
While the term is common in many programming languages, wrapper classes in Apex are specifically tailored for Salesforce use cases.

Q3. Is there a governor limit impact while using wrapper classes?
Wrapper classes themselves don’t directly impact governor limits, but ensure you optimize loops and avoid SOQL queries inside them.

Conclusion

The Wrapper Class in Apex is a game-changer when it comes to handling complex datasets in Salesforce. Whether you’re developing Visualforce pages, Lightning components, or working with collections in Apex logic, wrapper classes allow you to package and manage data in a cleaner, more structured way.

By mastering this concept, you unlock greater control over your Salesforce data handling and UI presentation — making your development process more efficient and scalable.

Write your comment Here

nineteen − eighteen =