From fbdd2905301fa6a2eb217d7a833583734259945c Mon Sep 17 00:00:00 2001 From: tschug Date: Tue, 8 Oct 2024 02:39:28 -0400 Subject: [PATCH] Permit the use of an Id from a field --- .../lwc/illustration/illustration.html | 2 +- .../lwc/indicatorBundle/indicatorBundle.html | 6 ++ .../lwc/indicatorBundle/indicatorBundle.js | 60 +++++++++++++++++-- .../indicatorBundle.js-meta.xml | 4 +- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/force-app/main/default/lwc/illustration/illustration.html b/force-app/main/default/lwc/illustration/illustration.html index 7883783..9169c4e 100644 --- a/force-app/main/default/lwc/illustration/illustration.html +++ b/force-app/main/default/lwc/illustration/illustration.html @@ -6,7 +6,7 @@

{heading}

-

{messageBody}

+

{messageBody}

diff --git a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.html b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.html index adc2087..b892cc9 100644 --- a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.html +++ b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.html @@ -92,6 +92,12 @@

+ diff --git a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js index 2a463f7..08bcd84 100644 --- a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js +++ b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js @@ -1,5 +1,6 @@ -import { LightningElement, api, wire } from 'lwc'; +import { LightningElement, api, wire, track } from 'lwc'; import { getRecord, getFieldValue } from 'lightning/uiRecordApi'; +import { getObjectInfo } from 'lightning/uiObjectInfoApi'; import { refreshApex } from '@salesforce/apex'; import KeyModal from 'c/indicatorBundleKey'; @@ -18,6 +19,13 @@ export default class IndicatorBundle extends LightningElement { @api indsSize = 'large'; @api indsShape = 'base'; @api showRefresh = false; + @api mappedField = ''; // API Field Name for the record + @api showFooter = false; + + targetIdField; // Syntax of template field: sObject.Field_Name__c + targetIdValue; + targetMessage; + bundleActive = true; // Set default active status hasHeader = false; // Hide header by default @@ -35,9 +43,16 @@ export default class IndicatorBundle extends LightningElement { errorOccurred = false; errorMessage = ''; showIllustration = false; - illustration = {}; + @track illustration = {}; connectedCallback(){ + if(this.mappedField == null || this.mappedField.trim() == ""){ + this.targetIdValue = this.recordId; + } else { + this.targetIdField = this.objectApiName + '.' + this.mappedField; + console.log(this.targetIdField); + } + if(!this.bundleName){ this.errorOccurred = true; this.showIllustration = true; @@ -48,7 +63,37 @@ export default class IndicatorBundle extends LightningElement { } } else { this.showIllustration=false; - this.illustration = {}; + // this.illustration = {}; + } + } + + // Check the record for an existing field value in order to initialize it. + @wire(getRecord, { recordId: '$recordId', fields: '', optionalFields: '$targetIdField' }) + record ({error, data}) { + if(error) { + console.log('ERROR'); + this.errorOccurred = true; + this.showIllustration = true; + this.illustration = { + heading : 'Errors Using the Mapped Field', + messageBody: 'Check the API Name for the custom field API name: ' + this.mappedField, + imageName: 'custom:setup' + } + } else if (data) { + if( JSON.stringify(data.fields) === '{}' ) { + this.errorOccurred = true; + this.showIllustration = true; + this.illustration = { + heading : 'Problem Using Mapped Field', + messageBody: 'Check the case-sensitive API Name for the mapped field API name: ' + this.mappedField, + imageName: 'custom:setup' + }; + } else { + this.targetMessage = 'This Indicator Bundle displays indicators based on the record id (' + data.fields[this.mappedField].value + ') in the mapped field \"' + this.mappedField + '\" from the ' + data.apiName + ' object.'; + this.targetIdValue = getFieldValue(data, this.targetIdField); + this.showIllustration=false; + this.illustration = {}; + } } } @@ -62,6 +107,10 @@ export default class IndicatorBundle extends LightningElement { return this.titleStyle == 'Lightning Card'; } + get displayFooter() { + return this.showFooter && (this.mappedField != null && this.mappedField.trim() != ""); + } + initCSSVariables() { if(this.showTitle && this.isStandardUsage && (this.bundle.CardIconBackground || this.bundle.CardIconForeground)) { @@ -169,9 +218,10 @@ export default class IndicatorBundle extends LightningElement { refreshApex(this.wiredData); } - // Get the field values for the current record based on the configured fields in CMDT + // Get the field values for the target record based on the configured fields in CMDT // Using 'optionalFields' ensures that if a user does not have access to a field, the indicator will not show. - @wire(getRecord, { recordId: '$recordId', optionalFields: '$apiFieldnameDefinitions' }) + // TODO: Add fields parameter to retrieve the record name for use when the targetIdValue is for another record. + @wire(getRecord, { recordId: '$targetIdValue', optionalFields: '$apiFieldnameDefinitions' }) wiredRecord(result) { const {error,data} = result; diff --git a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js-meta.xml b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js-meta.xml index 7553d86..35d6750 100644 --- a/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js-meta.xml +++ b/force-app/main/default/lwc/indicatorBundle/indicatorBundle.js-meta.xml @@ -10,12 +10,14 @@ - + + +