SK SKYVVA Documentation

10. How to use the Salesforce CDC change event?

In this unit, We will see how we can use Change Events to synchronize changes to the Salesforce records in your ERP(Enterprise resource planning) system. You can subscribe to change events the same way you subscribe to other events, like PushTopic events or platform events. You can use CometD or Apex triggers. CometD is a messaging library that enables listening to events through long polling and simulates push technology.  Even we can consume the CDC change event using a process builder.

Change Data Capture:-

A Change Data Capture event, or change event, is a notification that Salesforce sends when a change to a Salesforce record occurs as part of a create, update, delete, or undelete operation. The notification includes all new and changed fields and header fields that contain information about the change. So we use salesforce CDC when we needed a real-time notification.

So here we will learn How to Consume the change events using an apex trigger and do local action e.g. update an account:-

How to use it? Follow the given steps:- Step 1:- Subscribing to platform events:-

  1. From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  2. Click New.
  3. For Traced Entity Type, select Automated Process.
  4. Select the time period to collect logs and the debug level.
  5. Click on Save.

[su_box title="Note" box_color="#2a8af0" title_color="#000000"] Using the Event PlatForm for use the Salesforce CDC change event to send data out with the skyvva API ,The user type is  Automated Process.[/su_box] Step 2:-  Select Object for Change data capture:-

  1. From Setup, enter Change data capture in the Quick Find box.
  2. Select-Object.
  1. Save it.

Step 4:- Create an Apex trigger:-

  1. Click on the Submit button to generate a header script.
  2. After clicking on the submit button.
  3. We will add the code as shown in the picture given below.

We can use this code:- [aux_code language="javascript" theme="tomorrow" title="" extra_classes=""]trigger trgTestCDC on AccountChangeEvent (after insert) { List lstId = new List(); for (AccountChangeEvent event : Trigger.New) { EventBus.ChangeEventHeader header = event.ChangeEventHeader; System.debug('Received change event for ' + header.entityName + ' for the ' + header.changeType + ' operation.'); System.debug('RecordIds: ' + header.recordIds); System.debug('Account Name: ' + event.Name); System.debug('Account Phone: ' + event.Phone); if ( header.changetype == 'UPDATE') { lstId.addAll(header.recordIds); } } }[/aux_code]

Check the result on Debug logs:-

  1. From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  2. We can check results in debug logs.

####

####

####

####

Now you might be thinking why our chapter name is "How to use the Salesforce CDC change event to send data out with the SKYVVA API?" So here is the reason. The salesforce CDC change event to send data out with the SKYVVA API means, that we are using a combination of the real-time capability of salesforce change data capture and callout capability of SKYVVA. The callout capability of SKYVVA is best as we have different adapters that connect to many external systems for this our user doesn't need a programmer. This is the benefit of using SKYVVA invoke Callout2().

We provide these two modes to our user:-

When to use which mode:-

So here we will learn how to consume the event using an apex trigger and sending out data using Invoke callout2.

How to use it? Pre-requisite:-

Follow the given steps:- Step 1:- Subscribing to platform events:-

  1. From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  2. Click New.
  3. For Traced Entity Type, select Automated Process.
  4. Select the time period to collect logs and the debug level.
  5. Click on Save.

[su_box title="Note" box_color="#2a8af0" title_color="#000000"] Using the Event PlatForm for use the Salesforce CDC change event to send data out with the skyvva API ,The user type is  Automated Process.[/su_box] Step 2:- Register Licence key for Automated Process user:-

  1. From Setup, enter custom settings in the Quick Find box.

  1. Click on the Manage option.

  1. Click on the "NEW" button at the top.

  1. Enter the SKYVVA license key in the value block.
  1. Save it.

Step 3:-  Select Object for Change data capture:-

  1. From Setup, enter Change data capture in the Quick Find box.
  2. Select-Object.

  1. Save it.

Step 4:- Create an Apex trigger:-

  1. Click on the Submit button to generate a header script.
  2. After clicking on the submit button.
  3. It will show the screen as shown in this picture given below.

Step 5:-  Generate Trigger script by following steps given below:-

  1. Select and copy the generated script and paste it in the apex trigger which we created in step 4.
  2. Do it as shown in the picture given below.

  1. Save it.

Check the result on Message Monitoring:-

[su_box title="Note" box_color="#2a8af0" title_color="#000000"]For trigger after delete it is working when query selection with keyword "ALL ROWS". E.g SELECT BillingCity,BillingCountry,Name,Id,Description From Account ALL ROWS[/su_box]

Open this article in the interactive viewer →