We started an initiative and were in process of testing a POC using CDC for the first time in Salesforce. We knew of the 5 object limit when utilizing this feature and we were able to work with this.
Initial tests with custom fields worked just fine. We have a change event trigger on the Contact object and when a field value is added/updated, it creates a serialized JSON string and sends it to an AWS API gateway so we can process/enrich/utilize it with lambdas.
All worked as expected until we added a formula field to the Contact object and didn't see it send in the change event payload. Turns out there is a consideration that we overlooked where formula fields are not supported.
We now need to handle this situation differently as the native CDC functionality won't work in this case.
Example Trigger:
/**
* ContactChangeTrigger
* Send POST request to AWS Change Event API after ContactChangeEvent
*/
trigger ContactChangeTrigger on ContactChangeEvent (after insert) {
// Iterate through each event message
for (ContactChangeEvent event : Trigger.New) {
// Get some event header fields
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
// Serialize to JSON
String json = JSON.serialize(event, true);
// Send async request
APIGateway.sendRequestAsync(json);
System.debug('Sent asynchronous POST request!');
}
}
APEX Callout for AWS:
@future(callout=true)
public static void sendRequestAsync(String content) {
// Attempt to send data to the AWS endpoint
try {
APIGateway.dt = Datetime.now();
APIGateway.d = APIGateway.getDate();
APIGateway.timestamp = APIGateway.getTimestamp();
APIGateway.content = content;
AWS_Configuration__c awsConfig = AWS_Configuration__c.getOrgDefaults();
APIGateway.REQUEST_PATH = awsConfig.Request_Path__c;
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:AWS' + APIGateway.REQUEST_PATH);
req.setMethod(APIGateway.METHOD);
req.setHeader('Content-Type', APIGateway.CONTENT_TYPE);
req.setHeader('X-Amz-Date', APIGateway.timestamp);
req.setBody(content);
Http http = new Http();
http.send(req);
} catch (Exception e) {
// Handle
}
}
Since we were relying on change events to send over the fields that changed, that needs to be handled differently.
I came across this example of a trigger to determine fields that changed. I am wondering if there is a better way that can avoid hardcoding checks of every field and somehow only send over the serialized fields that changed in someone based on old
and new
in the trigger?
Essentially, I need to do a delta on the field changes to be able to send over the changed data.
Worse case, I don't have to do the delta here and can just send over all of the new data and then possibly handle that in the lambdas instead.
Any thoughts on a good approach? CDC would have worked just fine if it supported formula fields, but the lack of support there now forces me to take another path.