Handle corrupted data from Salesforce

Edit this page
Salesforce sends a notification when a change to a Salesforce record occurs as part of a create, update, delete, or undelete operation. However, if there is corrupt data in Salesforce, it sends a gap event instead of a change event, and these gap events should be properly handled to avoid discrepancies between Salesforce reports and internal dashboards. This tutorial demonstrates how to process Salesforce data and filter corrupt events, which allows a downstream application to appropriately process and reconcile those events for accurate reporting and analytics. For a more detailed explanation of this use case, read Streaming ETL SFDC Data for Real-Time Customer Analytics.

Run it

Setup your environment

1

Provision a Kafka cluster in Confluent Cloud.

Once your Confluent Cloud cluster is available, create a ksqlDB application and navigate to the ksqlDB editor to execute this tutorial. ksqlDB supports SQL language for extracting, transforming, and loading events within your Kafka cluster.

Execute ksqlDB code

2

ksqlDB processes data in realtime, and you can also import and export data straight from ksqlDB from popular data sources and end systems in the cloud. This tutorial shows you how to run the recipe in one of two ways: using connector(s) to any supported data source or using ksqlDB’s INSERT INTO functionality to mock the data.

If you cannot connect to a real data source with properly formatted data, or if you just want to execute this tutorial without external dependencies, no worries! Remove the CREATE SOURCE CONNECTOR commands and insert mock data into the streams.

When creating the initial STREAM or TABLE, if the backing Kafka topic already exists, then the PARTITIONS property may be omitted.

-- Substitute your parameter values in the connector configurations below.
-- If you do not want to connect to a real data source, remove the CREATE SOURCE CONNECTOR commands,
-- and add the INSERT INTO commands to insert mock data into the streams

-- Stream of changes to Salesforce records
CREATE SOURCE CONNECTOR IF NOT EXISTS sfdc_cdc WITH (
  'connector.class'            = 'SalesforceCdcSource',
  'name'                       = 'SalesforceCdcSourceConnector',
  'kafka.api.key'              = '<my-kafka-api-key>',
  'kafka.api.secret'           = '<my-kafka-api-secret>',
  'kafka.topic'                = 'sfdc.cdc.raw',
  'salesforce.username'        = '<my-sfdc-username>',
  'salesforce.password'        = '<my-sfdc-password>',
  'salesforce.password.token'  = '<sfdc-password-token>',
  'salesforce.consumer.key'    = '<sfdc-consumer-key>',
  'salesforce.consumer.secret' = '<sfdc-consumer-secret>',
  'salesforce.cdc.name'        = 'AccountChangeEvent',
  'output.data.format'         = 'JSON',
  'tasks.max'                  = '1'
);

SET 'auto.offset.reset' = 'earliest';

-- Register the stream of SFDC CDC Opportunities
CREATE STREAM stream_sfdc_cdc_opportunity_raw (
  replayid INT,
  changeeventheader STRUCT<
    changetype VARCHAR>
) WITH (
  KAFKA_TOPIC = 'sfdc.cdc.raw',
  VALUE_FORMAT = 'JSON',
  PARTITIONS = 6
);

-- Create a new stream with Replay ID and Change Event Header for just Gap Events
CREATE STREAM stream_sfdc_cdc_opportunity_change_log AS
  SELECT
    replayid,
    changeeventheader
  FROM stream_sfdc_cdc_opportunity_raw
  WHERE UCASE(changeeventheader->changetype) LIKE 'GAP%'
  EMIT CHANGES;

Test with mock data

3

If you are you not running source connectors to produce events, you can use ksqlDB INSERT INTO statements to insert mock data into the source topics:

INSERT INTO stream_sfdc_cdc_opportunity_raw (replayid, changeeventheader) VALUES (1235951, STRUCT(changetype := 'GAP_CREATE'));
INSERT INTO stream_sfdc_cdc_opportunity_raw (replayid, changeeventheader) VALUES (1235952, STRUCT(changetype := 'GAP_UPDATE'));
INSERT INTO stream_sfdc_cdc_opportunity_raw (replayid, changeeventheader) VALUES (1235953, STRUCT(changetype := 'UPDATE'));
INSERT INTO stream_sfdc_cdc_opportunity_raw (replayid, changeeventheader) VALUES (1235954, STRUCT(changetype := 'DELETE'));
INSERT INTO stream_sfdc_cdc_opportunity_raw (replayid, changeeventheader) VALUES (1235955, STRUCT(changetype := 'GAP_UPDATE'));

To validate that this recipe is working, run the following query:

SELECT * FROM stream_sfdc_cdc_opportunity_change_log EMIT CHANGES LIMIT 3;

Your output should resemble:

+----------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
|REPLAYID                                                                                            |CHANGEEVENTHEADER                                                                                   |
+----------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
|1235955                                                                                             |{CHANGETYPE=GAP_UPDATE}                                                                             |
|1235952                                                                                             |{CHANGETYPE=GAP_UPDATE}                                                                             |
|1235951                                                                                             |{CHANGETYPE=GAP_CREATE}                                                                             |
Limit Reached
Query terminated

Cleanup

4

To clean up the ksqlDB resources created by this tutorial, use the ksqlDB commands shown below (substitute stream or topic name, as appropriate). By including the DELETE TOPIC clause, the topic backing the stream or table is asynchronously deleted as well.

DROP STREAM IF EXISTS <stream_name> DELETE TOPIC;
DROP TABLE IF EXISTS <table_name> DELETE TOPIC;

If you also created connectors, remove those as well (substitute connector name).

DROP CONNECTOR IF EXISTS <connector_name>;