πΊ️ Displaying a Map with List Values in a Lightning Component
When working with complex data structures in Salesforce, using a Map with a List as its value is common in Apex. But displaying this structure in a Lightning Component (Aura or LWC) requires a few tricks. This blog walks through how to pass and render a Map<String, List<Object>>
in a user-friendly way.
π¦ Use Case
You're retrieving a list of Contacts
grouped by Account Name
, like:
Now, you want to display each account with its related contacts in a Lightning component.
⚙️ Apex Controller
π§© Aura Component Example
⚡ Client-side Controller (JS)
✅ Best Practices
-
Use
@AuraEnabled(cacheable=true)
for better performance. -
Always check for
null
values in JS to avoid runtime errors. -
For LWC, transform the map into an array format using Apex before returning.
π¬ Final Thoughts
Using a Map<String, List<Object>>
structure allows powerful grouping in Apex. With the right approach, rendering it in the Lightning UI is clean and intuitive.
No comments:
Post a Comment