diff --git a/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.html b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.html new file mode 100644 index 0000000..cc340bc --- /dev/null +++ b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.html @@ -0,0 +1 @@ + diff --git a/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js new file mode 100644 index 0000000..3b67ed8 --- /dev/null +++ b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js @@ -0,0 +1,50 @@ +import { LightningElement, api } from 'lwc'; +import { + subscribe as subToPlatformEvent, + unsubscribe as unsubFromPlatformEvent, + isEmpEnabled +} from 'lightning/empApi'; +import { RefreshEvent } from 'lightning/refresh'; + +export default class RecordRefreshListener extends LightningElement { + @api recordId; + channelName = '/event/RecordUpdated__e'; + subscription = {}; + + async connectedCallback() { + if (!isEmpEnabled) { + console.warn('emp is not enabled in this context', this.recordId); + return; + } + + await this.subscribe().catch((error) => { + console.error('error', error); + }); + } + + async disconnectedCallback() { + await this.unsubscribe(); + } + + subscribe() { + const callback = (response) => { + const recordId = response.data.payload.RecordId__c; + if (this.recordId === recordId) { + this.dispatchEvent(new RefreshEvent()); + } + }; + + return subToPlatformEvent(this.channelName, -1, callback).then( + (response) => { + this.subscription = response; + } + ); + } + + unsubscribe() { + return unsubFromPlatformEvent(this.subscription, (response) => { + console.log('unsubscribed', JSON.stringify(response)); + this.subscription = null; + }); + } +} diff --git a/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js-meta.xml b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js-meta.xml new file mode 100644 index 0000000..c9fb191 --- /dev/null +++ b/force-app/main/default/lwc/recordRefreshListener/recordRefreshListener.js-meta.xml @@ -0,0 +1,26 @@ + + + 58.0 + true + Record Refresh Listener + Refresh when a Refresh Platform Event is fired. + + lightning__RecordPage + + + + + + Account + Contact + Lead + Opportunity + + + + diff --git a/force-app/main/default/objects/RecordUpdated__e/RecordUpdated__e.object-meta.xml b/force-app/main/default/objects/RecordUpdated__e/RecordUpdated__e.object-meta.xml new file mode 100644 index 0000000..3bf3af0 --- /dev/null +++ b/force-app/main/default/objects/RecordUpdated__e/RecordUpdated__e.object-meta.xml @@ -0,0 +1,9 @@ + + + Deployed + Used to signal that a record has been updated. + HighVolume + + Record Updated + PublishAfterCommit + diff --git a/force-app/main/default/objects/RecordUpdated__e/fields/RecordId__c.field-meta.xml b/force-app/main/default/objects/RecordUpdated__e/fields/RecordId__c.field-meta.xml new file mode 100644 index 0000000..75a6965 --- /dev/null +++ b/force-app/main/default/objects/RecordUpdated__e/fields/RecordId__c.field-meta.xml @@ -0,0 +1,14 @@ + + + RecordId__c + The Id of the updated record. + false + false + false + false + + 18 + true + Text + false +