Wrapper Class with LWC

How to use Wrapper Class in Lightning Web Component


A wrapper class is a collection of different Salesforce data types. In Salesforce, you can combine multiple data types and utilize them for various purposes. A wrapper class is used mainly to wrap data collected from the present objects to a new object

Advantages of a Wrapper Class in Salesforce

  1. Data can be organized perfectly when it is nested.
  2. When considering the JSON structure, the wrapper class structure is comparable to the data visualization technique on a particular web page.

A wrapper class and a container class can be kept together, but it is better if they are stored separately. When they are kept independently, it is easy for them to be maintained and reused whenever required, thus preventing code duplication.

Use Case:- If we want to display opportunity information in LWC component, then first we need to SOQL in Apex class and create a Wrapper Class with relevant parameter that’s we want to display in LWC component. So here we don’t need to send List of sObject to LWC only Wrapper will manage it.


public with sharing class OpportunityController {

    public  static List<WrapperClass>getOppData()
        List<WrapperClass> wrapperOppList=new List<WrapperClass>();
        for(Opportunity opp:[SELECT Id, Name, Amount, AccountId, Account.Name, CloseDate, Description, StageName, LeadSource,Type FROM Opportunity LIMIT 10])
            WrapperClass wrapItem = new WrapperClass();
        return wrapperOppList;
    public class WrapperClass
        public String Name;
        Public Decimal Amount;
        public String StageName;
        public String Description;
        public String LeadSource;
        public String Type;

    public OpportunityController() {



        <div class="slds-p-around_large" style="background: azure">
        <h3 slot="title" style="font-size:20px;">
            <lightning-icon icon-name="standard:opportunity" size="small"></lightning-icon>Opportunity Records Retrieve By Wrapper CLass  <span style="color: #ff8000;">        </span> 
                <table  border="10">
                    <thead style="background-color: antiquewhite;">
                    <template for:each={opportunityList.data} for:item="items">
                        <tr key={items.Id}>




import { LightningElement,wire } from 'lwc';
import getOppData from '@salesforce/apex/OpportunityController.getOppData'

export default class WrapperClass extends LightningElement {

    @wire(getOppData) opportunityList;


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">


Leave a Reply