Fetching picklist values dynamically through apex class method and display selected picklist value in Salesforce lightning web component – LWC | How to get picklist values dynamically in lwc


Hey guys, today in this post we are going to learn about how to Fetch picklist values dynamically through apex class method and display selected picklist value in Salesforce lightning web component – LWC.

Files we used in this post example

lwcPicklistController.cls Apex Class Controller It is used for fetch the picklist value dynamically from server.
fetchPicklistValueLwc.html Lightning Web Component HTML Template HTML file for used to write HTML element for user interface.
fetchPicklistValueLwc.js LWC JavaScript File It is holding @Wire method function that is fetching the picklist value from database.
fetchPicklistValueLwc.js-meta.xml XML Meta File It is used for where this lightning web component you want to exposed.

Live Demo

Fetch picklist values Dynamic 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

Create Apex Class Controller →

Step 1:- Create Apex Controller : lwcPicklistController.cls

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

lwcPicklistController.cls [Apex Class]

  1.   public WITH sharing class lwcPicklistController {
  3.      //fetch picklist VALUES FROM custom object IN lwc
  4.      @AuraEnabled(cacheable=TRUE)
  5.      public static List < customValueWrapper > pickListValueDynamically(sObject customObjInfo, string selectPicklistApi) {
  6.         Schema.DescribeSObjectResult objDescribe = customObjInfo.getSObjectType().getDescribe();     
  7.         map < String, Schema.SObjectField > customFieldMap = objDescribe.fields.getMap();      
  8.         list < Schema.PicklistEntry > custPickValues = customFieldMap.get(selectPicklistApi).getDescribe().getPickListValues();
  9.         list < customValueWrapper > customObjWrapper = NEW list < customValueWrapper > ();
  10.         FOR (Schema.PicklistEntry myCustPick: custPickValues) {
  11.           customValueWrapper selectOptionValueWrapper = NEW customValueWrapper();
  12.              selectOptionValueWrapper.custFldlabel = myCustPick.getLabel();
  13.              selectOptionValueWrapper.custFldvalue = myCustPick.getValue();
  14.             customObjWrapper.add(selectOptionValueWrapper);
  15.         }
  17.         RETURN customObjWrapper;
  19.       }
  20.       // wrapper class 
  21.         public WITH sharing class customValueWrapper {
  22.           @auraEnabled public string custFldlabel {GET;SET;}
  23.           @auraEnabled public string custFldvalue {GET;SET;}
  24.         }
  25. }


Create Lightning Web Component →

Step 2:- Create Lightning Web Component : fetchPicklistValueLwc.html

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

✪ The Big Announcement: -
As per student's demands, I re-opened my eBook of "Salesforce Tutorial" Limited-time huge discount offer Absolutely 50% Off.
I am thinking to give you discount offer occasion of Gandhi Jayanti for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
👉 So Don't MISS it... (Access Right Now)
👉 Get Huge Discount Offer 50%: - Get eBook

fetchPicklistValueLwc.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card title="Dynamically fetch picklist values from custom object in LWC" icon-name="custom:custom25">
  3.        <div class="slds-p-horizontal--medium">
  4.         <label class="slds-form-element__label">Target Actuals</label>
  5.         <div class="slds-form-element__control">
  6.           <div class="slds-select_container">
  7.             <select class="slds-select" onchange={selectOptionChanveValue}>
  8.                   <option value="">---None---</option>                
  9.                     <template for:each={selectTargetValues.data} for:item="selectOptItem">
  10.                        <option key={selectOptItem.custFldvalue} value={selectOptItem.custFldvalue}>
  11.                          {selectOptItem.custFldlabel}
  12.                        </option>
  13.                   </template>
  14.             </select>
  15.           </div>
  16.         </div>
  17.         <br/>
  18.          <b>Selected Picklist Value Is:-</b> <span style="color:brown; font-weight:bold;"> {picklistVal}</span>
  19.        </div> 
  21. &nbsp;
  22. <br/><br/>
  23.    <!--Start RelatedTopics Section-->
  24. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  26.             <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;" data-aura-rendered-by="436:0"><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>
  28.             <br/><br/>
  29.             <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;" data-aura-rendered-by="436:0"><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>
  30.             <div style="display:block; overflow:hidden;"> 
  31.                 <div style="width: 50%; float:left; display:inline-block">
  32.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  33.                         <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>
  34.                         <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>
  35.                         <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>
  36.                         <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>
  37.                         <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>
  38.                     </ul>
  39.             </div>
  41.             <div style="width: 50%; float:left; display:inline-block">
  42.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  43.                         <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>
  44.                         <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>
  45.                         <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>
  46.                         <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>
  47.                         <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>
  48.                     </ul>
  49.                 </div>
  50.                <div style="clear:both;"></div> 
  51.                <br/>
  52.                 <div class="youtubeIcon">
  53.                     <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>
  54.                 </div>
  55.     </div>
  57. </div>
  59.   <!--End RelatedTopics Section-->
  60.     </lightning-card>
  61. </template>

Create LWC JavaScript File →

Step 3:- Create Lightning Web Component : fetchPicklistValueLwc.js

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

fetchPicklistValueLwc.js [LWC JavaScript File]

  1.   import { LightningElement,track,wire } from 'lwc';
  2. import pickListValueDynamically from '@salesforce/apex/lwcPicklistController.pickListValueDynamically';
  4. export default class FetchPicklistValueLwc extends LightningElement {
  5.  @track picklistVal;
  7. @wire(pickListValueDynamically, {customObjInfo: {'sobjectType' : 'scoreCard__c'},
  8. selectPicklistApi: 'targetVSActuals__c'}) selectTargetValues;
  10.   selectOptionChanveValue(event){       
  11.        this.picklistVal = event.target.value;
  12.    }  
  14. }


Create LWC Meta Data XML →

Step 4:- Create Lightning Web Component : fetchPicklistValueLwc.js-meta.xml

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

fetchPicklistValueLwc.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>


Fetch picklist values Dynamic in lwc -- w3web.net

Further post that would you like to learn in Salesforce




FAQ (Frequently Asked Questions)

What is dynamic picklist in Salesforce?

Dynamic Picklists are multiple-choice questions that get their list of choices directly from Salesforce. Values are populated from Salesforce when the form loads, so the choices in your picklist always stay current without having to refresh the schema.

What is restricted picklist in Salesforce?

You can make a custom multi-select picklist field into a restricted picklist only if the picklist has fewer than 500 values. A restricted picklist's values are limited to only those values defined by a Salesforce admin, which prevents users from loading redundant or erroneous values through the API.

How do you get picklist values dynamically in Apex?

To fetch the picklist values in Apex dynamically, we need to make an extra call to getGlobalDescribe() to get the Schema. SObjectType of that particular object. Then, we can use it to fetch the DescribeSobjectResult. Once we get the DescribeSobjectResult, we need to call fields.


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