function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Biswojeet Ray 11Biswojeet Ray 11 

LWC schema Field

/* eslint-disable no-console */
import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues, getPicklistValuesByRecordType, getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_SOURCE from '@salesforce/schema/Account.AccountSource';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class PicklistDemo extends LightningElement {
    @track pickListvaluesByRecordType;
    @track accountsource;

    @wire(getPicklistValues, {
        recordTypeId : '012000000000000AAA',
        fieldApiName : ACCOUNT_SOURCE
    })
        wiredPickListValue({ data, error }){
            if(data){
                console.log(` Picklist values are `, data.values);
            }
            if(error){
                console.log(` Error while fetching Picklist values  ${error}`);
            }
        }
    @wire(getPicklistValuesByRecordType, {
        recordTypeId : '012N0000000x2HE',
        objectApiName : ACCOUNT_OBJECT
    })
        wiredRecordtypeValues({data, error}){
            if(data){
                console.log(' Picklist Values ', data.picklistFieldValues.Industry.values);
                this.pickListvaluesByRecordType = data.picklistFieldValues.Industry.values;
                this.accountsource = data.picklistFieldValues.AccountSource.values;
            }
            if(error){
                console.log(error);
            }
        }

}


I have one question.

When we are sending parameter to getPicklistValues as per field, Then we are importing the field by schema and sending that as a parameter. we are not using directly the field API name.

Like - import ACCOUNT_SOURCE from '@salesforce/schema/Account.AccountSource';

using here - @wire(getPicklistValues, { recordTypeId : '012000000000000AAA', fieldApiName : ACCOUNT_SOURCE })

But when we are fetching a particular field's value from "getPicklistValuesByRecordType", We are using the API name of the field.

We are not importing the field by the schema.

Like this - this.pickListvaluesByRecordType = data.picklistFieldValues.Industry.values;

Here we are using the direct API name of the "Industry" field. We are not importing this field and use it.

 

Why SO??????

 

Thanks in Advance,

Santosh Kumar 348Santosh Kumar 348
Hi Biswojeet Ray,

There are multiple ways by which you can fetch picklist values:
  1. getPicklistValues: It accepts 3 parameters menioned below:
    • ​​​​​​​recordTypeId—(Required) The ID of the record type. Use the Object Info defaultRecordTypeId property.
    • fieldApiName—(Required) The API name of the picklist field on a supported object.
    • propertyOrFunction—A private property or function that receives the stream of data from the wire service. If a property is decorated with @wire, the results are returned to the property’s data property or error property. If a function is decorated with @wire, the results are returned in an object with a data property and an error property.
  2. getPicklistValuesByRecordType: It also accepts 3 paramter mentioned below:
    • objectApiName(Required) The API name of a supported object.
    • recordTypeId—(Required) The ID of the record type. Use the Object Info defaultRecordTypeId property,
    • propertyOrFunction—A private property or function that receives the stream of data from the wire service. If a property is decorated with @wire, the results are returned to the property’s data property or error property. If a function is decorated with @wire, the results are returned in an object with a data property and an error property.
So now let's compare the both as if you will see getPicklistValues accepts field API Name, so ACCOUNT_SOURCE is used which is referring to AccountSource field.
 
import ACCOUNT_SOURCE from '@salesforce/schema/Account.AccountSource';

but if you will see getPicklistValuesByRecordType it accepts recordtypeId and ObjectName, so ACCOUNT_OBJECT is used which is reffering to Account.
 
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

So don't get confused by looking at Industry field in below statement.
if(data){
      console.log(' Picklist Values ', data.picklistFieldValues.Industry.values);
      this.pickListvaluesByRecordType = data.picklistFieldValues.Industry.values;
      this.accountsource = data.picklistFieldValues.AccountSource.values;
          
 }

 
Basically getPicklistValuesByRecordType returns a collection of picklist values for all the picklists of a specified record type. So to access the specific picklist Industry is used.

Now you must be thinking about the uses of both, so just conclude 
  • To retrieve picklist values for a specific field, use getPicklistValues.
  • To retrive all picklist values at once use getPicklistValuesByRecordType.

If it helped then can you please mark it as the best answer so that it can be used by others in the future.

Regards,
Santosh Kumar