You need to sign in to do that
Don't have an account?
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,
There are multiple ways by which you can fetch picklist values:
- 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.
- 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.but if you will see getPicklistValuesByRecordType it accepts recordtypeId and ObjectName, so ACCOUNT_OBJECT is used which is reffering to Account.
So don't get confused by looking at Industry field in below statement.
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
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
getPicklistValues (https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_picklist_values) & getPicklistValuesByRecordType (https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_picklist_values_record)