Saturday, October 26, 2024

What is Change Data Capture in Salesforce?

Change data capture is a streaming product also called CDC helps you to integrate your Salesforce data with external systems in real time for operation such as creation of a new record, updates to an existing record, deletion of a record, and undeleting of a record. This is very helpful in cases where we need to keep external system in sync with Salesforce in real time instead of doing periodic exports and Imports of data or repeated API calls.

 Availability for Standard and Custom objects for CDC and there syntax for usage with CDC :

 Change events are available for all custom objects defined in your Salesforce org and a subset of standard objects. A ChangeEvent object is available for each object that supports Change Data Capture. A change event isn’t a Salesforce object. It doesn’t support CRUD operations or queries. It’s included in the object reference so you can discover which Salesforce objects support change events. The name of a change event is based on the name of the corresponding object for which it captures the changes.

 Standard Object Change Event Name: <Standard_Object_Name>ChangeEvent

 As an example for Account we will use AccountChangeEvent.

 Custom Object Change Event Name: <Custom_Object_Name>__ChangeEvent

 As an example, for MyCustomObject__c we will use MyCustomObject__ChangeEvent.

 Security with Change Data Capture:

 Change Data Capture ignores sharing settings and sends change events for all records of a Salesforce object. Using Change Data Capture we can capture field changes for all records, CDC Deliver only the fields a user has access to based on field-level security. The fields that a change event doesn’t include are:

 The IsDeleted system field.

The SystemModStamp system field.

Any field whose value isn’t on the record and is derived from another record or from a formula, except roll-up summary fields, which are included.

Sample event message in JSON format for a new account record creation:

This example is an event message in JSON format for a new account record creation.

{

  "schema": "IeRuaY6cbI_HsV8Rv1Mc5g",

  "payload": {

    "ChangeEventHeader": {

      "entityName": "Account",

      "recordIds": [

        "<record_ID>"

      ],

      "changeType": "CREATE",

      "changeOrigin": "com/salesforce/api/soap/51.0;client=SfdcInternalAPI/",

      "transactionKey": "0002343d-9d90-e395-ed20-cf416ba652ad",

      "sequenceNumber": 1,

      "commitTimestamp": 1612912679000,

      "commitNumber": 10716283339728,

      "commitUser": "<User_ID>"

    },

    "Name": "Acme",

    "Description": "Everyone is talking about the cloud. But what does it mean?",

    "OwnerId": "<Owner_ID>",

    "CreatedDate": "2021-02-09T23:17:59Z",

    "CreatedById": "<User_ID>",

    "LastModifiedDate": "2021-02-09T23:17:59Z",

    "LastModifiedById": "<User_ID>"

  },

  "event": {

    "replayId": 6

  }

}

How to enable Change Data Capture?

To receive notifications on the default standard channel for record changes, select the custom objects and supported standard objects that you’re interested in on the Change Data Capture page.

From Setup, in the Quick Find box, enter Change Data Capture, and click Change Data Capture. The Available Entities list shows the objects available in your Salesforce org for Change Data Capture, You can select up to five entities, including standard and custom objects. To enable more entities, contact your Salesforce Account Representative to purchase an add-on license. The add-on license removes the limit on the number of entities you can select. Also, it increases the event delivery allocation for CometD and Pub/Sub API clients. With the add-on license, you can select up to 10 entities at a time in the Available Entities list. After selecting the first 10 entities, you can add more.

What is CDC in salesforce?

Note: You can also create a custom channel if you have multiple subscribers and each subscriber receives change events from a different set of entities. Also, use a custom channel with event enrichment to isolate sending enriched fields in change events on a specific channel. Custom channels group and isolate change events for each subscriber so subscribers receive only the types of events they need.

Subscribe to Change Events:

 You can subscribe to change events with CometD, Pub/Sub API, or Apex triggers.

 Let us try to understand with an example,

 Now, to view the CDC, open the workbench à Open queries tab à Streaming Push Topics.

 Click Generic Subscriptions à and type /Data/AccountChangeEvent and click Subscribe.

 The connection is established now as you can see in below image.

 

How to use Change Data Capture in Salesforce

As we have setup CDC for Account above. Let us try to update an account record and click Save.

Change Data Capture in Salesforce

Now, go back to workbench. You will be able to see the notification as shown below.

How to use Change Data Capture in Salesforce

No comments:

Post a Comment