0

So I'm trying to get the picklist values based on 2 record type ids for the Account object. Everything works just fine, but the thing that bothers me is that I have to hard-code the record type ids.

@wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '012250000009XyOAAU' })
wiredValues({ data }){
    if(data){
        this.mhbkBranchValues = [];
        data.picklistFieldValues.Branch__c.values.forEach(
            element => this.mhbkBranchValues.push({
                label: element.label,
                value: element.value
            })
        );
        console.log('mhbkBranchValues', this.mhbkBranchValues);
        this.mergePicklistValues();
    } 
}

//Shared
@wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '012250000009XyPAAU' })
wiredValues2({ data }){
    if(data){
        this.sharedBranchValues = [];
        data.picklistFieldValues.Branch__c.values.forEach(
            element => this.sharedBranchValues.push({
                label: element.label,
                value: element.value
            })
        );
        console.log('sharedBranchValues', this.sharedBranchValues);
        this.mergePicklistValues();
    } 
}

mergePicklistValues(){
    if(this.mhbkBranchValues && this.sharedBranchValues){
        this.branchOptions = this.mhbkBranchValues;
        this.sharedBranchValues.forEach(
            element => {
                if(this.branchOptions.filter(e => e.label === element.label).length > 0){
                    console.log('existing value');
                } else {
                    this.branchOptions.push({
                        label: element.label,
                        value: element.value
                    });
                }
            }
        );
    }
    console.log('branch info ', this.branchOptions);
}

I also tried getting the record type id using getObjectInfo, but the problem is the values are undefined when the 2 picklist wire functions are run.

@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
wiredAccData({data}){
    if(data){
        console.log(data);
        const rtis = data.recordTypeInfos;
        this.mhbkRTID = Object.keys(rtis).find(rti => rtis[rti].name === 'MHBK');
        this.sharedRTID = Object.keys(rtis).find(rti => rtis[rti].name === 'Shared');
        console.log(this.mhbkRTID, this.sharedRTID);
    }
}

Is there a way of achieving this?

LATER EDIT: My code now looks like this:

mhbkRTID;
sharedRTID;

@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
wiredAccData({data}){
    console.log('obj', data);
    if(data){
        console.log('obj data', data);
        const rtis = data.recordTypeInfos;
        this.mhbkRTID = Object.keys(rtis).find(rti => rtis[rti].name === 'MHBK');
        this.sharedRTID = Object.keys(rtis).find(rti => rtis[rti].name === 'Shared');
        console.log('rt ids', this.mhbkRTID, this.sharedRTID);
    }
}

//MHBK
@wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$this.mhbkRTID' })
wiredValues({ data }){
    console.log('mhbk', data);
    if(data){
        this.mhbkBranchValues = [];
        data.picklistFieldValues.Branch__c.values.forEach(
            element => this.mhbkBranchValues.push({
                label: element.label,
                value: element.value
            })
        );
        console.log('mhbkBranchValues', this.mhbkBranchValues);
        this.mergePicklistValues();
    } 
}

//Shared
@wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$this.sharedRTID' })
wiredValues2({ data }){
    console.log('Shared', data);
    if(data){
        this.sharedBranchValues = [];
        data.picklistFieldValues.Branch__c.values.forEach(
            element => this.sharedBranchValues.push({
                label: element.label,
                value: element.value
            })
        );
        console.log('sharedBranchValues', this.sharedBranchValues);
        this.mergePicklistValues();
    } 
}

As you can see there are multiple debugs and this is what my console looks like: console output The getObjectInfo is re-evaluated and retrieves the values, but it doesn't re-evaluates the other 2 wire functions. The debugs under the red line are from a different wire function and from connectedCallback.

8
  • not exactly, as I need to get the values for 2 different record types for the same object Mar 18, 2022 at 11:45
  • The other question shows an example of one, but have you tried doing the same pattern which is why I shared it - store the record type Id from your getObjectInfo wire class in separate variables and then pass the variables into the wire for picklist $recordType1 vs. $recordType2? Mar 18, 2022 at 11:49
  • I have replaced the hard-coded id values with the following: '$this.mhbkRTID' and '$this.sharedRTID'. I don't get the undefined error anymore, but it doesn't seem to retrieve anything in the wire function. Mar 18, 2022 at 12:05
  • I just tested this with my own simple scenario and it worked fine with multiple record types and multiple calls to get picklistvalues. Have you debugged and check each part? ex. does the console log in your getObjectInfo show you getting both record type Ids? have you added console.logs in your picklist wires to see what data is returned? You can edit your question with more details from debugging Mar 18, 2022 at 12:51
  • 1
    remove this. from your variables passed to the wire. It would just be $sharedRTID and $mhbkRTID. The $ indicates it's dynamic and reactive - and references a property of the component instance Mar 18, 2022 at 13:42

0