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: 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.
getObjectInfo
wire class in separate variables and then pass the variables into the wire for picklist$recordType1
vs.$recordType2
?getObjectInfo
show you getting both record type Ids? have you added console.logs in your picklist wires to see whatdata
is returned? You can edit your question with more details from debuggingthis.
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