[SalesForce] Get picklist values after dynamically fetching the record type id in lwc

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.

Best Answer

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;
Related Topic