Salesforce Interview Questions & Answers Book is now available!

Grab Now

LWC

Get picklist values based on Record Types in LWC

Let's look at how to fetch picklist values based on record types in LWC.

By Teja, ex-Salesforce engineer · Updated March 30, 2021 · 1 min read

Am sure by now you are aware that we can create different business processes using picklist fields and record types.

As in, based on the record type we can selectively show values of the picklist field. And this is possible only with pick-list fields and not with other fields.

In LWC when you need to fetch values of a picklist (but not all the values) based on the record type there is a helper method that we can make use of and it's called getPicklistValuesByRecordType

Let's look at the snippet!

Here is the LWC controller file (check the comments).

import { LightningElement, wire } from 'lwc';

//import the named import to fetch the values of a picklist
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';

//import the object from which you want to fetch the field
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class Example extends LightningElement {

	/* Wire the method to a function.
       We need to pass the recordTypeId and 
       you can fetch is via Apex call 
     */
    @wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: 'XXXXXXXXX' })
    
    picklistValues({error, data}){
    	if(data){
        	console.log(data);
        }else if(error){
        	console.log(error);
        }
    }
}

Example.js using getPicklistValuesByRecordType named import

This is lot more handy than we trying to write the code to generate the values on the fly.

Hope this was helpful!

Learn it by building it

Every course, every code repository, the discussion group and certificates of completion — one subscription.

Keep me in the loop!

Email me when a new episode is published (promise, I will not spam you)

You're signing up to receive emails from Salesforce Casts