3

Is it possible to get field info (esp. picklist values) in an LWC controller for dynamically specified fields?

The getPicklistValue docs demonstrates fetching data for an explicit field, but is there a way to decide the field at runtime?

E.g.

// NOT-REAL-CODE

import { LightningElement, api, wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import DYNAMIC_FIELD from '@salesforce/schema/Account.{userSpecifiedFieldName}';

export default class Example extends LightningElement {

    @api userSpecifiedFieldName;

    @wire(getPicklistValues, { recordTypeId: '012000000000000AAA', fieldApiName: DYNAMIC_FIELD})
    propertyOrFunction;
}

I know it's possible to do this using the Schema object in an APEX class, but since it requires going to the server this is expensive for my use-case.

If it's useful to anyone else, here's the docs on calling APEX from LWC and a useful article on using Schema to get picklist values.

2
  • 1
    99% sure the fieldApiName can be given just a string. Forget the import approach entirely, just pass in an appropriate API name string. You just need to use the correct form. I believe it is "ObjectAPIName.FieldAPIName".
    – Phil W
    Commented May 21, 2020 at 12:23
  • 2
    BTW, the wire still goes to the server. The benefit is that the result can be cached in the browser for a while.
    – Phil W
    Commented May 21, 2020 at 13:13

2 Answers 2

3

You can fetch the picklist fields from a dynamic string object name with its recordTypeId using getPicklistValuesByRecordType and then filter it based on your field name.

Example:

import { LightningElement,wire } from 'lwc';
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';
export default class DynamicPicklistOptions extends LightningElement {
  @wire(getPicklistValuesByRecordType, { 
    objectApiName: 'Account', 
    recordTypeId: '012000000000000AAA'
  })
  picklistFields;

  // extract values from payload
  findPicklistValuesByFieldName(fieldName){
    const picklistValues = this.picklistFields.data.picklistFieldValues[fieldName].values;
    console.log('picklistValues :: ' + JSON.stringify(values));
  }
}
5

The import schema statement is used for two features.

First, it provides compile-time safety against typos. Using 'Account.Name' in your source somewhere won't be caught until you go to test the code while importing @saleforce/schema/Account.Name can be caught during deployment, saving you time.

Second, the import schema statement protects your code from administrative changes. For example, if a custom field is specified, and an administrator tries to rename/delete this field, Salesforce can tell the administrator that this field is in use in that component.

Since import statements are a compile-time feature, you can't specify them dynamically, because the compiler wouldn't be able to determine if they were valid or not.

If you want to get fields dynamically without Apex, you can use the getObjectInfo wire service. Note that this will still call the server, but you don't have to write any Apex, and the results can be cached by the client. The getObjectInfo provides all the fields, their types, labels, picklist values, etc, everything you could want to know about an object.

3
  • My apologies for submitting a wrong edit. I should've paid more attention. Sorry! I don't know how to revoke my edit.
    – Bahman.A
    Commented Apr 19, 2021 at 16:50
  • 1
    @Bahman.A No worries, fixed.
    – sfdcfox
    Commented Apr 19, 2021 at 16:58
  • Thanks man, you ROCK!!
    – Bahman.A
    Commented Apr 19, 2021 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .