Newer Version Available

This content describes an older version of this product. View Latest

External Change Data Capture Packaging and Testing

You can distribute External Change Data Capture components in managed packages, including a framework for testing your Apex triggers. Special behaviors and limitations apply to packaging and package installation.
  • Include External Change Data Tracking components in a managed package by selecting your test from the Apex Class Component Type list. The trigger, test, external data source, external object, and other related assets are brought into the package for distribution.
  • Certificates aren’t packageable.If you package an external data source that specifies a certificate, make sure that the subscriber org has a valid certificate with the same name.

To help you test your External Change Data Capture–triggered Apex classes, here is a unit test code example of a trigger reacting to a simulated external change.

Example Trigger
1trigger OnExternalProductChangeEventForAudit on Products__ChangeEvent (after insert) {
2    if (Trigger.new.size() != 1) return;
3    for (Products__ChangeEvent event: Trigger.new) {
4         Product_Audit__c audit = new Product_Audit__c(); 
5         audit.Name = 'ProductChangeOn' + event.ExternalId;
6         audit.Change_Type__c = event.ChangeEventHeader.getChangeType();
7         audit.Audit_Price__c = event.Price__c;
8         audit.Product_Name__c = event.Name__c;
9         insert(audit);
10    }
11}
Apex Test
1@isTest
2public class testOnExternalProductChangeEventForAudit {
3    static testMethod void testExternalProductChangeTrigger() { 
4            // Create Change Event
5           Products__ChangeEvent event = new Products__ChangeEvent();
6            // Set Change Event Header Fields
7           EventBus.ChangeEventHeader header = new EventBus.ChangeEventHeader();
8           header.changeType='CREATE';
9           header.entityName='Products__x';
10           header.changeOrigin='here';
11           header.transactionKey = 'some';
12           header.commitUser = 'me';
13           event.changeEventHeader = header;
14           event.put('ExternalId', 'ParentExternalId');
15           event.put('Price__c', 5500);
16           event.put('Name__c', 'Coat');
17            // Publish the event to the EventBus
18           EventBus.publish(event);
19           Test.getEventBus().deliver();
20            // Perform assertion that the trigger was run
21           Product_Audit__c audit = [SELECT name, Audit_Price__c, Product_Name__c FROM Product_Audit__c WHERE name = : 'ProductChangeOn'+ event.ExternalId LIMIT 1]; 
22           System.assertEquals('ProductChangeOn'+ event.ExternalId, audit.Name); 
23           System.assertEquals(5500, audit.Audit_Price__c); 
24           System.assertEquals('Coat', audit.Product_Name__c); 
25    }
26}