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 | Visualforce Component | It is used for where this lightning web component you want to exposed. |
Live Demo
Other related post that would you like to learn in LWC
Create Apex Class Controller
Step 1:- Create Apex Controller : lwcPicklistController.cls
SFDX:Create Apex Class >> New >> lwcPicklistController.cls
lwcPicklistController.cls [Apex Class]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public with sharing class lwcPicklistController { //fetch picklist values from custom object in lwc @AuraEnabled(cacheable=true) public static List < customValueWrapper > pickListValueDynamically(sObject customObjInfo, string selectPicklistApi) { Schema.DescribeSObjectResult objDescribe = customObjInfo.getSObjectType().getDescribe(); map < String, Schema.SObjectField > customFieldMap = objDescribe.fields.getMap(); list < Schema.PicklistEntry > custPickValues = customFieldMap.get(selectPicklistApi).getDescribe().getPickListValues(); list < customValueWrapper > customObjWrapper = new list < customValueWrapper > (); for (Schema.PicklistEntry myCustPick: custPickValues) { customValueWrapper selectOptionValueWrapper = new customValueWrapper(); selectOptionValueWrapper.custFldlabel = myCustPick.getLabel(); selectOptionValueWrapper.custFldvalue = myCustPick.getValue(); customObjWrapper.add(selectOptionValueWrapper); } return customObjWrapper; } // wrapper class public with sharing class customValueWrapper { @auraEnabled public string custFldlabel {get;set;} @auraEnabled public string custFldvalue {get;set;} } } |
Create Lightning Web Component
Step 2:- Create Lightning Web Component : fetchPicklistValueLwc.html
SFDX:Lightning Web Component >> New >> fetchPicklistValueLwc.html
fetchPicklistValueLwc.html [Lightning Web Component HTML]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<template> <lightning-card title="Dynamically fetch picklist values from custom object in LWC" icon-name="custom:custom25"> <div class="slds-p-horizontal--medium"> <label class="slds-form-element__label">Target Actuals</label> <div class="slds-form-element__control"> <div class="slds-select_container"> <select class="slds-select" onchange={selectOptionChanveValue}> <option value="">---None---</option> <template for:each={selectTargetValues.data} for:item="selectOptItem"> <option key={selectOptItem.custFldvalue} value={selectOptItem.custFldvalue}> {selectOptItem.custFldlabel} </option> </template> </select> </div> </div> <br/> <b>Selected Picklist Value Is:-</b> <span style="color:brown; font-weight:bold;"> {picklistVal}</span> </div> </lightning-card> </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 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { LightningElement,track,wire } from 'lwc'; import pickListValueDynamically from '@salesforce/apex/lwcPicklistController.pickListValueDynamically'; export default class FetchPicklistValueLwc extends LightningElement { @track picklistVal; @wire(pickListValueDynamically, {customObjInfo: {'sobjectType' : 'scoreCard__c'}, selectPicklistApi: 'targetVSActuals__c'}) selectTargetValues; selectOptionChanveValue(event){ this.picklistVal = event.target.value; } } |
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 2 3 4 5 6 7 8 9 10 |
<?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> |
Further post that would you like to learn in LWC