Sub topic 4
Returning structured data with wrapper classes
This chapter assumes you can write basic Apex classes, SOQL queries and Lightning web component (LWC) JavaScript classes. You should also know what @AuraEnabled does on a method and how to import an Apex method into a component.
Why return a wrapper class
A method often needs to return more than one record type. A component might need an account name and a count of its contacts. You could return an Account and make the component count the children. You could also return a Map<String, Object>, but then both sides must agree on string keys, and a typo fails silently.
A wrapper class is an inner class that holds exactly the values the component needs. The component gets a plain JavaScript object with named properties. The shape is documented in the Apex code.
Marking members with @AuraEnabled
Only members marked @AuraEnabled are serialized and sent to the component. Unmarked members stay on the server.
public with sharing class AccountSummaryController {
public class Summary {
@AuraEnabled public String name;
@AuraEnabled public Integer contactCount;
}
@AuraEnabled(cacheable=true)
public static Summary getSummary(Id accountId) {
Account acc = [
SELECT Name, (SELECT Id FROM Contacts)
FROM Account
WHERE Id = :accountId
WITH USER_MODE
];
Summary result = new Summary();
result.name = acc.Name;
result.contactCount = acc.Contacts.size();
return result;
}
}The Summary class is a public inner class. Its two fields are marked, so the component receives { name: '...', contactCount: 3 }.
The method reads the contacts through a subquery and counts them in Apex. The WITH USER_MODE clause makes the query respect the running user's permissions.
What the component receives
The browser gets a JSON object, not an Apex instance. Methods on the wrapper do not travel to the client. Only the marked fields do.
Using the wrapper in a component
A wired property holds an object with data and error. Read the wrapper fields from data.
import { LightningElement, api, wire } from 'lwc';
import getSummary from '@salesforce/apex/AccountSummaryController.getSummary';
export default class AccountSummary extends LightningElement {
@api recordId;
@wire(getSummary, { accountId: '$recordId' })
summary;
get title() {
return this.summary.data
? `${this.summary.data.name} (${this.summary.data.contactCount} contacts)`
: '';
}
}The getter guards against data being undefined. A wired property has no data until the first response arrives.
Common errors
| Symptom | Likely cause |
|---|---|
| A field is `undefined` in JavaScript | The Apex member is missing `@AuraEnabled`. |
| Compile error about a member | The wrapper member is not `public` or `global`. |
| The property name does not match | JavaScript property names are case-sensitive. Use the exact Apex name. |
| Changes to the object are rejected | Wired data is read-only. Copy it first, for example with spread syntax. |
Design tips
- Keep wrappers small. Send only what the component displays.
- Use simple types such as
String,Integer,Boolean,IdandDate. You can also nest other wrapper classes or use lists of them. - Name the wrapper after its purpose, such as
Summary, not after the object. - Avoid putting logic in the wrapper. It runs on the server and cannot be called from the client.
Exercise
A component reads summary.data.contactCount but gets undefined, even though the Apex code sets result.contactCount = 5. The method is @AuraEnabled(cacheable=true) and the wrapper declares public Integer contactCount;. What is the most likely fix?
Show answer
Add @AuraEnabled to the contactCount field in the wrapper class. Without it the field is not serialized, so the component never receives it.
Exercise
Why is returning a wrapper class usually safer than returning a Map<String, Object>?
Show answer
The wrapper declares the names and types of its fields in Apex, so the compiler catches mistakes. With a map, a mistyped key compiles and fails silently at run time.