Change Data Capture (CDC) enables receiving near-real-time updates for Salesforce records and synchronizing them with an external data store. CDC publishes change events that reflect modifications to Salesforce records, including new records, updates, deletions, and undeletions. These events can be subscribed to in a Lightning Web Component (LWC) using the Emp API.
To meet the business requirement of creating a task whenever a user manually changes an opportunity's status to "Closed Won," use CDC events and a generic LWC on the opportunity record page. This solution avoids the need for Apex triggers or record-triggered flows.
How to enable CDC?
- Go to setup
- Search Change Data Capture
- Add Entities
The
lightning/empApi
module provides access to methods for subscribing to a streaming channel and listening to event messages. All streaming channels are supported, including channels for platform events, PushTopic events, generic events, and Change Data Capture events. This component requires API version 44.0 or later.Note: Below code is for example purpose, you can modify it according to your requirements.
changeDataCaptureSubscriber.js
import { LightningElement, api } from 'lwc';
import { subscribe, unsubscribe, onError } from 'lightning/empApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class EmiApiCallout extends LightningElement {
channelName = '/data/CaseChangeEvent';
subscription = {};
@api recordId;
subscribed;
// Tracks changes to channelName text field
handleChannelName(event) {
this.channelName = event.target.value;
}
renderedCallback() {
if (!this.subscribed) {
this.handleSubscribe();
this.subscribed = true;
}
}
// Initializes the component
connectedCallback() {
// Register error listener
this.registerErrorListener();
}
// Handles subscribe button click
handleSubscribe() {
// Callback invoked whenever a new event message is received
const messageCallback = (response) => {
console.log('New message received: ', JSON.stringify(response));
// Response contains the payload of the new message received
this.handleMessage(response);
};
// Invoke subscribe method of empApi. Pass reference to messageCallback
subscribe(this.channelName, -1, messageCallback).then(response => {
// Response contains the subscription information on subscribe call
console.log('Subscription request sent to: ', JSON.stringify(response.channel));
this.subscription = response;
});
}
// Handles unsubscribe button click
handleUnsubscribe() {
// Invoke unsubscribe method of empApi
unsubscribe(this.subscription, (response) => {
console.log('unsubscribe() response: ', JSON.stringify(response));
// Response is true for successful unsubscribe
});
}
registerErrorListener() {
// Invoke onError empApi method
onError((error) => {
console.log('Received error from server: ', JSON.stringify(error));
// Error contains the server-side error
});
}
handleMessage(response) {
if (response) {
if (response.hasOwnProperty('data')) {
Console.log('data check here '+ response);
}
}
}
}
changeDataCaptureSubscriber.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
DEMO:
Comments
Post a Comment
Please Write your comment here.