Skip to content

Commit a0d9435

Browse files
authored
fix related object fields ApiName to Label (#2147)
1 parent 4794fd4 commit a0d9435

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

force-app/components/relatedList/lwc/tagRelatedList/tagRelatedList.js

+44-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import getRelatedList from '@salesforce/apex/TAG_RelatedListController.getRelate
33
import { NavigationMixin } from 'lightning/navigation';
44
import { getRecord } from 'lightning/uiRecordApi';
55
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
6+
import { getObjectInfos } from 'lightning/uiObjectInfoApi';
67
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
78

89
export default class TagRelatedList extends NavigationMixin(LightningElement) {
@@ -43,6 +44,31 @@ export default class TagRelatedList extends NavigationMixin(LightningElement) {
4344
this.getList();
4445
}
4546

47+
get relatedObjectNames() {
48+
if (!this.displayedFields) return [];
49+
const fields = this.displayedFields.replace(/\s/g, '').split(',');
50+
const related = new Set();
51+
fields.forEach(field => {
52+
if (field.includes('.')) {
53+
let relationship = field.split('.')[0];
54+
related.add(relationship);
55+
}
56+
});
57+
return Array.from(related);
58+
}
59+
60+
@wire(getObjectInfos, { objectApiNames: '$relatedObjectNames' })
61+
wiredRelatedObjectInfos({ data, error }) {
62+
if (data) {
63+
this.relatedObjectMetadata = data.results.reduce((acc, item) => {
64+
acc[item.result.apiName] = item.result.fields;
65+
return acc;
66+
}, {});
67+
} else if (error) {
68+
console.error('Error fetching related object metadata:', error);
69+
}
70+
}
71+
4672
@wire(getObjectInfo, { objectApiName: '$relatedObjectApiName' })
4773
objectInfo;
4874

@@ -309,18 +335,29 @@ export default class TagRelatedList extends NavigationMixin(LightningElement) {
309335
if (!this.popoverRecordData || !this.objectInfo.data) {
310336
return [];
311337
}
338+
312339
return this.combinedPopoverFields.map(fieldApiName => {
313-
// Look up the localized label (if available); if not, fallback to field API name
314-
let fieldLabel = this.objectInfo.data.fields[fieldApiName] ?
315-
this.objectInfo.data.fields[fieldApiName].label :
316-
fieldApiName;
340+
let fieldLabel;
341+
342+
if (fieldApiName.includes('.')) {
343+
let [relationship, childField] = fieldApiName.split('.');
344+
345+
if (this.relatedObjectMetadata[relationship] && this.relatedObjectMetadata[relationship][childField]) {
346+
fieldLabel = this.relatedObjectMetadata[relationship][childField].label;
347+
} else {
348+
fieldLabel = childField;
349+
}
350+
} else {
351+
fieldLabel = this.objectInfo.data.fields[fieldApiName]?.label || fieldApiName;
352+
}
353+
317354
return {
318-
apiName: fieldLabel, // Using the localized label here instead of the API name
355+
apiName: fieldLabel,
319356
value: this.resolve(fieldApiName, this.popoverRecordData)
320357
};
321358
});
322-
}
323-
359+
}
360+
324361
// Getter for popover style
325362
get popoverStyle() {
326363
if (this.popoverPosition) {

0 commit comments

Comments
 (0)