Data Capture Trigger Event Handler

To help with detecting changes to Salesforce records in close to real-time and synchronizing them with the appropriate records in an external data store you can use something like CDC - Change Data Capture. Salesforce records that have been modified are represented by change events that Change Data Capture publishes. A new record can be created, information can be updated on an existing record, a record can be deleted, or undeleted. Change Data Capture triggers can be used on all custom objects and subset of standard objects.


For purposes of this example we need to select an object for CDC event notifications. Go to Setup → Integrations → Change Data Capture and select Case standard object, just like that

Data Capture Data Capture

Next up you will have to create an apex trigger on that object change event and a plugin Breezz framework inside the trigger. It would look like this.

trigger CaseCDCTrigger on CaseChangeEvent (after insert) {
    forvendi.BreezzApi.TRIGGERS.handle();
}

Change Data Capture triggers always operate on after insert context as it serves as a type of one way road. Where all the changes to a certain object whether it is an update or delete operation are sent as a ‘package’ and managed on arrival. After that, create a new Breezz Trigger configuration. Object name will be same exact as with your apex trigger, which here is CaseChangeEvent.

Configure New Insert Trigger Configure New Insert Trigger

Create a new custom Step based on the class below and associate it with the group created by Breezz Trigger configuration. To test proper functioning of this Change Data Capture try to insert a new case and see if taks records containing case information was created.

 1public with sharing class CaseFollowUp extends forvendi.Step  {
 2
 3
 4    public CaseFollowUp() {
 5        super(CaseFollowUp.class.getName());
 6    }
 7
 8
 9    public override Boolean initRecordProcessing(Object record, Object optionalOldRecord) {
10        // cast incoming record to CaseChangeEvent object and extract header which contains
11        // information about type of change commited to record and values of changed fields
12        CaseChangeEvent caseEvent = (CaseChangeEvent) record;
13        Eventbus.ChangeEventHeader header = caseEvent.ChangeEventHeader;
14        List<Task> tasks = new List<Task>();
15       
16        if (header.changetype == 'CREATE') {
17        // if type == create which means case record has just been inserted create a followup task
18            for(String recordid : header.recordids){
19                Task tk = new Task();
20                tk.Subject = 'Follow up on new case, Source : ' + caseEvent.Origin;
21                if (caseEvent.ContactId == null) {
22                    tk.Description = 'Get contact information.';
23                    tk.ActivityDate = System.today().addDays(1);
24                } else {
25                    tk.Description = 'Get in touch';
26                    tk.ActivityDate = System.today().addDays(3);
27                }
28                tk.WhatId = recordid;
29                tk.OwnerId = header.CommitUser;
30                tasks.add(tk);
31            }
32            getContext().addToInsert(tasks);
33        }
34        return false;
35    }
36}

CaseChangeEvent.recordIds is a record ID or records for the modified records. This field typically holds a single record ID. Salesforce merges the change notification when the same change occurs simultaneously in many records of the same object type during a single transaction. Salesforce sends a single change event in this scenario for all affected records, and the recordIds field provides the IDs of all affected records. For showcasing in this example we will use assume there is not enough changes and just one record is inserted at a time. And ChangeEventHeader.CommitUser is used for assigning owner to the user that is committing the transaction.