Get picklist values based on Record Types in LWC

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

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!