Sunday, December 1, 2019

How to get Picklist values based on record type in Lightning Web Components(lwc)

To get picklist values based on record type we have to use getPicklistValuesByRecordType wire adapter.
Syntax
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';

In this demo, I am using Account object, based on record type id I am getting picklist values of Industry field

HTML Code
<template>
    <lightning-card title="Picklist Vlaues based on Record Type">
      <div if:true={options}>
            <lightning-combobox name="progress"
                                label="Industry"
                                value={value}
                                placeholder="-Select-"
                                options={options}
                                onchange={handleChange} ></lightning-combobox><br/>
        
            you selected industry: <b>{value}</b><br/><br/>
      </div>
    </lightning-card>
    
</template>

Javascript Controller
import { LightningElement, track, wire} from 'lwc';
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';

export default class PicklistValuesByRecordType extends LightningElement {
    @track value;
    @track options;

    @wire(getPicklistValuesByRecordType, { objectApiName: 'Account', recordTypeId: '012B0000000gTUOIA2' }) 
    IndustryPicklistValues({error, data}) {
        if(data) {
            this.options = data.picklistFieldValues.Industry.values;
        }
        else if(error) {
            window.console.log('error =====> '+JSON.stringify(error));
        }
    }

    handleChange(event) {
        this.value = event.target.value;
    }
}

Output

No comments:

Post a Comment