Get Picklist values in Lightning web components

Get Picklist values in LWC with default recordtype

get-picklist-values-lwc

Using getPicklistValues wire method to get any picklist field values in LWC without calling apex controller methods. 

Follow the below steps to get picklist values of any picklist field in LWC.

Step:1

Import getPicklistValues  wire method in your js file to get picklist values.

[import { getPicklistValues } from 'lightning/uiObjectInfoApi';]

Step: 2

Import getObjectInfo wire method in your js file to get default record type id.

[import { getObjectInfo } from 'lightning/uiObjectInfoApi';]

Step:3

Call getObjectInfo wire to get default recordtype id.

[// to get the default record type id, if you dont' have any recordtypes then it will get master

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })

    accountMetadata;]

Step:4

Now call getPicklistValues to get picklist values. It required two values. 

  1. recordTypeId: we have to pass recordtype id, if we dont' have record type then pass default record type id of master
  2. fieldApiName: It's API name of the picklist field. 

[@wire(getPicklistValues,

        {

            recordTypeId: '$accountMetadata.data.defaultRecordTypeId', 

            fieldApiName: INDUSTRY_FIELD

        }

    )

    industryPicklist;]

Let see full code of the component. 

GetPicklistValuesOfField.html

[<template>

    <lightning-card title="Get picklist values of Industry field" icon-name="standard:user">

        <div class="slds-m-around_medium">

            <template if:true={industryPicklist.data}>

                <lightning-combobox name="Industry" label="Industry" value={value}

                    options={industryPicklist.data.values} onchange={handleChange}>

                </lightning-combobox>

            </template>

            <p>Selected value is: {value}</p>

        </div>

    </lightning-card>

</template>]

GetPicklistValuesOfField.js

[import { LightningElement,wire } from 'lwc';

import { getObjectInfo } from 'lightning/uiObjectInfoApi';

import { getPicklistValues } from 'lightning/uiObjectInfoApi';

import ACCOUNT_OBJECT from '@salesforce/schema/Account';

import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';



export default class GetPicklistValuesOfField extends LightningElement {

    value ='';


    // to get the default record type id, if you dont' have any recordtypes then it will get master

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })

    accountMetadata;


    // now get the industry picklist values

    @wire(getPicklistValues,

        {

            recordTypeId: '$accountMetadata.data.defaultRecordTypeId', 

            fieldApiName: INDUSTRY_FIELD

        }

    )

    industryPicklist;


    // on select picklist value to show the selected value

    handleChange(event) {

        this.value = event.detail.value;

    }

}]

GetPicklistValuesOfField.js-meta.xml

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

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

    <apiVersion>49.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__AppPage</target>

    </targets>

</LightningComponentBundle>]

You can also find the above code in SWDC WORLD repo

COMMENTS

Name

Android Apps,1,Apex,7,Apex Methods,1,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,IDs and Urls Hacks,1,JS for LWC,5,Lightning Components,2,lightning Page,1,Lightning Web Components,14,Linux for Windows users,1,LWC,16,LWC-JEST,7,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Handy Commands,1,Salesforce Interview Questions,14,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,SLDS,4,SVG Image,3,Test Class,1,Triggers,1,uiObjectInfoApi,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Get Picklist values in Lightning web components
Get Picklist values in Lightning web components
Get Picklist values in Lightning web components ,Get Picklist values in LWC with default recordtype,LWC Picklist : Get Pick-list Values (With / Withou
https://1.bp.blogspot.com/-EybXhdA4p1A/X1EGplPwkXI/AAAAAAAAAcY/mZdNwzSAtDYdolwE616Lyq1RIpYvP2bxQCLcBGAsYHQ/s16000/get-picklist-values-lwc.PNG
https://1.bp.blogspot.com/-EybXhdA4p1A/X1EGplPwkXI/AAAAAAAAAcY/mZdNwzSAtDYdolwE616Lyq1RIpYvP2bxQCLcBGAsYHQ/s72-c/get-picklist-values-lwc.PNG
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://www.swdcworld.com/2020/09/get-picklist-values-in-lightning-web.html
https://www.swdcworld.com/
https://www.swdcworld.com/
https://www.swdcworld.com/2020/09/get-picklist-values-in-lightning-web.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy