0

I have a custom field on Contact called Level__c

I have created a custom lwc component on Account, which creates a new Contact. And in that particular form, I have used the Level__c field.

Please not that, I have deliberately not used a record edit form, and went forward with the below approach for practise.

I have tried to populate the picklist field called Level__c depending on record type using the following piece of code

In the html file

 <template if:true={LevelPicklistValues.data}>
    <lightning-combobox name="level"
                                label="Level"
                                value={value}
                                placeholder="-Select-"
                                options={levelPicklistValues.data.values}
                                >
    </lightning-combobox>
</template>

In the js file

  import { LightningElement, wire, track } from 'lwc';
  import { getPicklistValues, getObjectInfo} from 'lightning/uiObjectInfoApi';
  import level_field from '@salesforce/schema/Contact.Level__c';

  import CONTACT_OBJECT from '@salesforce/schema/Contact';

  export default class CreateContactWithApex extends LightningElement {
  @track value;

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

  @track particularRecordTypeIdFromName;

  @wire(getPicklistValues, { recordTypeId: particularRecordTypeIdFromName, 
                           fieldApiName: level_field
      })levelPicklistValues; 

  connectedCallback(){
    let recordTypeName = 'Record_Type_1';
    let recordTypeInfo = this.objectInfo.data.recordTypeInfos;
    for(var eachRecordType in recordTypeInfo){
        if(recordTypeInfo[eachRecordType].Name === recordTypeName){
            particularRecordTypeIdFromName = recordTypeInfo[eachrecordType].recordTypeId;
            break;
        }
    } 
}

From what I studied, I think connectedCallback is automatically called when the component is added to the DOM.

However, it throws me an error

particularRecordTypeIdFromName is not defined

My problem here is than I am not being able to fetch the recordId and then pass it to the wire function.

Very new to lwc and any help will really be appreciated.

1

Below are some of the different ways to get picklist values based on recordType.

To get picklist values for the default record type for the object:

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

  @wire(getPicklistValues, {
    recordTypeId: "$objectInfo.data.defaultRecordTypeId",
    fieldApiName: LEVEL_FIELD
  })
  levelPicklistValues;

To get picklist values for the record specific record type:

  @wire(getRecord, {
    recordId: "$recordId",
    fields: "RecordTypeId"
  })
  record;

  @wire(getPicklistValues, {
    recordTypeId: "$record.data.recordTypeId",
    fieldApiName: LEVEL_FIELD
  })
  levelPicklistValues;

To get picklist values for the specific record type:

@track recordTypeId;

@wire(getObjectInfo, { objectApiName: CONTACT_OBJECT })
wiredObjectInfo({error, data}) {
  if (error) {
    // handle Error
  } else if (data) {
    const rtis = data.recordTypeInfos;
    this.recordTypeId = Object.keys(rtis).find(rti => rtis[rti].name === 'Special RecordType Name');
  }
};

@wire(getPicklistValues, {
  recordTypeId: "$recordTypeId",
  fieldApiName: LEVEL_FIELD
})
levelPicklistValues;
3
  • this works, but I have two related question 1. The picklist values are getting automatically sorted are are nor displayed in the order in which they were inserted in the picklist field. Is there any way to prevent that? 2. Is there any way to dynamically fetch the picklist values with wiring the variable instead of the picklist value (as you have written in the 3rd piece of code, above)? @HR – Madhurima Dec 18 '20 at 5:47
  • option1: Reorder the picklist values in the field metadata – H R Dec 18 '20 at 20:24
  • option2: Use getter and sort options by label get levelOptions(){ let options = []; if(this.levelPicklistValues && this.levelPicklistValues.data.values){ options = this.levelPicklistValues.data.values; // sort options by label } return options; } – H R Dec 18 '20 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.