A Wrapper Class is an Apex class that bundles together related data — typically an sObject plus extra computed/UI fields — for use in a Visualforce page, Lightning component, or Apex API.
Example: a Visualforce page that lists Opportunities with checkboxes for selection.
`apex public class OppWrapper { public Opportunity opp { get; set; } public Boolean selected { get; set; }
public OppWrapper(Opportunity o) { opp = o; selected = false; } } `
Then in your controller:
`apex public List<OppWrapper> wrappers { get; set; }
public OppListController() { wrappers = new List<OppWrapper>(); for (Opportunity o : [SELECT Id, Name, Amount FROM Opportunity LIMIT 100]) { wrappers.add(new OppWrapper(o)); } } `
In the page: <apex:column><apex:inputCheckbox value="{!wrapper.selected}"/></apex:column>.
Common use cases:
- UI selection state — checkbox, expanded row, hover, etc.
- Computed fields — derived totals, formatted display values.
- Multi-object aggregation — combine data from Account, Contact, Opportunity in one row.
- API response shaping — DTO-style classes for REST/SOAP web services.
In LWC, the same pattern uses JavaScript objects rather than Apex Wrapper Classes, but Apex still returns Wrapper Classes from @AuraEnabled methods.
