Fetch Picklist value in LWC using Schema class

 Create the Apex Class:


public with sharing class lwcPicklistController {

    

    //fetch picklist values from  SOBJECT

    @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 LWC

//ComponentName.html///

<template>

    <lightning-card title="Dynamically picklist values from Sobject" icon-name="custom:custom25">

       <div class="slds-p-horizontal--medium">

        <label class="slds-form-element__label">Industry</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> 

      


<br/><br/>

    </lightning-card>

</template>


//JSFILE Code


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' : 'Account'},

selectPicklistApi: 'Industry'}) selectTargetValues;

    

  selectOptionChanveValue(event){       

       this.picklistVal = event.target.value;

   }  

   

}


//MetaXML FIle


<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <!-- The apiVersion may need to be increased for the current release -->

    <apiVersion>52.0</apiVersion>

    <isExposed>true</isExposed>

    <masterLabel>Dynamic Picklist Value</masterLabel>

    <targets>

        <target>lightning__AppPage</target>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>


Push the change on org and then add the component in Record Page and verify the things





Comments

Popular Post

Show Hyperlink against the Record in Datatable in LWC

Send Email using Wrapper Classes in Salesforce.

Get the record Count of all countable Sobjects in Salesforce