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 →
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 →
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
lwcWrapperClass.html [Lightning Web Component HTML]
<template>
<lightning-card>
<div class="slds-p-around_large">
<h3 slot="title" style="font-size:17px;">
<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>]
</h3>
<br/>
<table class="tblColPad" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;">
<thead>
<tr>
<th>Name</th>
<th>StageName</th>
<th>Lead Source</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<template for:each={oppData.data} for:item="items">
<tr key={items.Id}>
<td>
{items.Name}
</td>
<td>
{items.StageName}
</td>
<td>
{items.LeadSource}
</td>
<td>
{items.Type}
</td>
<td>
{items.Description}
</td>
</tr>
</template>
</tbody>
</table>
<!--Start RelatedTopics Section-->
<br/><br/>
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</lightning-card>
</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]
import { LightningElement,wire } from 'lwc';
import fetchOppData from '@salesforce/apex/lwcWrapperClassCtrl.fetchOppData'
export default class LwcWrapperClass extends LightningElement {
@wire(fetchOppData) oppData;
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Create LWC Style CSS →
Step 4:- Create Style CSS : lwcWrapperClass.css
SFDX:Lightning Web Component >> New >> lwcWrapperClass.css
lwcWrapperClass.css [Style CSS]
table.tblColPad th{background:#ddd;}
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]
public WITH sharing class lwcWrapperClassCtrl {
@AuraEnabled(cacheable=TRUE)
public static List<WrapperClass>fetchOppData()
{
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 wrapItemFld=NEW WrapperClass();
wrapItemFld.Name=opp.Name;
wrapItemFld.StageName=opp.StageName;
wrapItemFld.Description=opp.Description;
wrapItemFld.LeadSource=opp.LeadSource;
wrapItemFld.Type=opp.Type;
wrapperOppList.add(wrapItemFld);
}
RETURN wrapperOppList;
}
public class WrapperClass
{
@AuraEnabled
public String Name;
@AuraEnabled
public String StageName;
@AuraEnabled
public String Description;
@AuraEnabled
public String LeadSource;
@AuraEnabled
public String TYPE;
}
}
Create Lightning Application →
Step 6:- Create Lightning Application : lwcWrapperClassApp.app
From Developer Console >> File >> New >> Lightning Application
lwcWrapperClassApp.app [Component Application File]
<aura:application extends="force:slds">
<c:lwcWrapperClass/>
</aura:application>
Further post that would you like to learn in Salesforce
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
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 |