Using Wrapper Class in Lightning Web Component to retrieve list of records of Opportunity Object in Salesforce LWC | How to retrieve list of records of Opportunity Object uses of Wrapper Class in Salesforce LWC – Lightning Web Component

8,297 views

Hey guys, today in this post we are going to learn about How to retrieve list of records of Opportunity Object uses of Wrapper Class in Salesforce LWC – 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 or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. To know more details about Wrapper Class Click Here →

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

Files we used to retrieve data from wrapper class in LWC →

lwcWrapperClass.html LWC HTML File Template HTML file to retrieve data from wrapper class in LWC
lwcWrapperClass.js LWC JavaScript File In the javascript file to fetch Opportunity through @@wire decorator in LWC
lwcWrapperClass.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage.
lwcWrapperClass.css Style CSS It is used to creaet custom table border, padding style CSS property.
lwcWrapperClassApp.app Lightning Application It is used to call the component to preview on browser.
lwcWrapperClassCtrl.cls Apex Controller It is used for fetching Opportunity records through wrapper class in LWC.

 

Final Output →

wrapper class to retrieve data in lwc -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

 

  • Find the below steps ▾

 

Create Lightning Web Component HTML →

Step 1:- Create Lightning Web Component : lwcWrapperClass.html

SFDX:Lightning Web Component >> New >> lwcWrapperClass.html

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

lwcWrapperClass.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card>
  3.         <div class="slds-p-around_large">
  4.         <h3 slot="title" style="font-size:17px;">
  5.             <lightning-icon icon-name="standard:opportunity" size="small"></lightning-icon> Retrieve records uses of  <span style="color: #ff8000;">        </span> in Lightning web components (LWC) [<a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">View more articles →</a>]  
  6.         </h3>
  7.         <br/>
  8.                 <table class="tblColPad" border="1" cellpadding="5" cellspacing="5"  style="border-collapse:collapse;">
  9.                     <thead>
  10.                     <tr>
  11.                         <th>Name</th>
  12.                         <th>StageName</th>
  13.                         <th>Lead Source</th> 
  14.                         <th>Type</th>            
  15.                         <th>Description</th>
  16.                     </tr>
  17.                 </thead>
  18.                 <tbody> 
  19.                     <template for:each={oppData.data} for:item="items">
  20.                         <tr key={items.Id}>
  21.                             <td>
  22.                                 {items.Name}
  23.                             </td>
  24.                             <td>
  25.                                 {items.StageName}
  26.                             </td>
  27.                             <td>
  28.                                 {items.LeadSource}
  29.                             </td>
  30.                             <td>
  31.                                 {items.Type}
  32.                             </td>
  33.                             <td>
  34.                                 {items.Description}
  35.                             </td>
  36.  
  37.                         </tr>
  38.  
  39.                     </template>
  40.                 </tbody>
  41.                 </table>
  42.  
  43.  
  44.                <!--Start RelatedTopics Section-->
  45.         <br/><br/>
  46.         <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  47.  
  48.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  49.  
  50.                     <br/><br/>
  51.                 <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  52.                     <div style="display:block; overflow:hidden;"> 
  53.                         <div style="width: 50%; float:left; display:inline-block">
  54.                             <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  55.                                 <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  56.                                 <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  57.                                 <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  58.                                 <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  59.                                 <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  60.                             </ul>
  61.                     </div>
  62.  
  63.                     <div style="width: 50%; float:left; display:inline-block">
  64.                             <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  65.                                 <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  66.                                 <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  67.                                 <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  68.                                 <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  69.                                 <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  70.                             </ul>
  71.                         </div>
  72.                        <div style="clear:both;"></div> 
  73.                        <br/>
  74.                         <div class="youtubeIcon">
  75.                             <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  76.                         </div>
  77.             </div> 
  78.         </div>
  79.  
  80.           <!--End RelatedTopics Section-->  
  81.  
  82.             </div>
  83.     </lightning-card>
  84. </template>

 

Create Lightning Web Component JavaScript →

Step 2:- Create Lightning Web Component : lwcWrapperClass.js

SFDX:Lightning Web Component >> New >> lwcWrapperClass.js

lwcWrapperClass.js [LWC JavaScript File]

  1.      import { LightningElement,wire } from 'lwc';
  2. import fetchOppData from '@salesforce/apex/lwcWrapperClassCtrl.fetchOppData'
  3.  
  4. export default class LwcWrapperClass extends LightningElement {
  5.     @wire(fetchOppData) oppData;
  6. }

 

Create Lightning Web Component Meta XML →

Step 3:- Create Lightning Web Component : lwcWrapperClass.js-meta.xml

SFDX:Lightning Web Component >> New >> lwcWrapperClass.js-meta.xml

lwcWrapperClass.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets> 
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.     </targets>
  10. </LightningComponentBundle>

 

Create LWC Style CSS →

Step 4:- Create Style CSS : lwcWrapperClass.css

SFDX:Lightning Web Component >> New >> lwcWrapperClass.css

lwcWrapperClass.css [Style CSS]

  1.    table.tblColPad th{background:#ddd;}
  2.   table.tblColPad th, table.tblColPad td {padding:5px !important;}

 

Create Apex Class Controller →

Step 5:- Create Apex Controller : lwcWrapperClassCtrl.cls

SFDX:Create Apex Class >> New >> lwcWrapperClassCtrl.cls

lwcWrapperClassCtrl.cls [Apex Class]

  1.     public WITH sharing class lwcWrapperClassCtrl {
  2.  
  3.     @AuraEnabled(cacheable=TRUE)
  4.     public  static List<WrapperClass>fetchOppData()
  5.     {
  6.  
  7.         List<WrapperClass> wrapperOppList=NEW List<WrapperClass>();
  8.         FOR(Opportunity opp:[SELECT Id, Name, Amount, AccountId, Account.Name, CloseDate, Description, StageName, LeadSource,TYPE FROM Opportunity LIMIT 10])
  9.         {
  10.  
  11.             WrapperClass wrapItemFld=NEW WrapperClass();
  12.             wrapItemFld.Name=opp.Name;
  13.             wrapItemFld.StageName=opp.StageName;
  14.             wrapItemFld.Description=opp.Description;
  15.             wrapItemFld.LeadSource=opp.LeadSource;
  16.             wrapItemFld.Type=opp.Type;
  17.             wrapperOppList.add(wrapItemFld);
  18.         }
  19.         RETURN wrapperOppList;
  20.     }
  21.  
  22.     public class WrapperClass
  23.     {
  24.         @AuraEnabled
  25.         public String Name;
  26.         @AuraEnabled
  27.         public String StageName;
  28.         @AuraEnabled
  29.         public String Description;
  30.         @AuraEnabled
  31.         public String LeadSource;
  32.         @AuraEnabled
  33.         public String TYPE;
  34.  
  35.     }
  36. }

 

Create Lightning Application →

Step 6:- Create Lightning Application : lwcWrapperClassApp.app

From Developer Console >> File >> New >> Lightning Application

lwcWrapperClassApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:lwcWrapperClass/>
  3. </aura:application>

 
Using wrapper class to retrieve data in lwc -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is a wrapper class in Salesforce?

A wrapper class is nothing but a collection of different Salesforce data types. In Salesforce, you can combine multiple data types and utilize them for various purposes. For example, there is a wrapper class that can access the account records and displays an in-page block table.

What is the need of wrapper class in Salesforce?

A Wrapper Class is often used by Salesforce Developers for creating new objects within the purview of Apex code. It helps the users in consolidating a set of different fields that are required the most during runtime.

How do I cover a test class for a wrapper class in Salesforce?

You can simply call the wrapper class with methods in the test class to increase the code coverage. It will cover your Wrapper class and methods. Hope this explanation will resolve your query.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment