Friday, June 7, 2019

lightning/uiObjectInfoApi Example (getPicklistValuesByRecordType)

getPicklistValuesByRecordType will fetch all picklist for a specific object.

getPicklistValuesByRecordType.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="getPicklistValues">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>   
    </targets>
</LightningComponentBundle>


getPicklistValuesByRecordType.html
<template>
    <lightning-card title="Get All Picklist Values.">
        <lightning-combobox name="accountSource"
                            label="Account Source"
                            value={selectedValue}
                            placeholder="Select"
                            options={accountSourceLOV}
                            onchange={performBusinessLogic} >
        </lightning-combobox>
        
        <lightning-combobox name="active"
                            label="Active"
                            value={selectedValue}
                            placeholder="Select"
                            options={activeLOV}
                            onchange={performBusinessLogic} >
        </lightning-combobox>
    </lightning-card>
</template>


getPicklistValuesByRecordType.js
import { LightningElement, track, wire} from 'lwc';
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class PicklistValuesDemo extends LightningElement {
    @track selectedValue;
    @track accountSourceLOV;
    @track activeLOV;

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    objectInfo;

    @wire(getPicklistValuesByRecordType, { recordTypeId: '$objectInfo.data.defaultRecordTypeId', objectApiName: ACCOUNT_OBJECT})
    allPicklistValues({data, error}){
        if(data){
            //Standard picklist field.
            this.accountSourceLOV = data.picklistFieldValues.AccountSource.values;
            //Custom picklist field.
            this.activeLOV = data.picklistFieldValues.Active__c.values;
        } else if(error){
            console.log("error === "+error);
        }
    }

    
    performBusinessLogic(event){
        this.selectedValue = event.detail.value;
        console.log("selectedValue === "+this.selectedValue);
    }
}


Output ==>


LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...