7

Salesforce released the Process Change Event Messages in Apex Triggers in Summer '19, is this feature available in the managed package?

1
  • Not definitive, but during the London Salesforce Partner Forum (targeted at Partners creating managed packages and other AppExchange offerings) they went into detail on these technologies. I would, therefore, think the answer is "yes".
    – Phil W
    Commented Jul 3, 2019 at 7:15

1 Answer 1

4

Yes this feature is available for managed package .

In order to test this i created a simple package with the below code

Trigger code


trigger AccountChangeAsyncTrigger on AccountChangeEvent (after insert) {
List<AccountChangeEvent> changes = Trigger.new;

Set<String> accIds = new Set<String>();
List<Contact> lstcontacts = new List<Contact>();

//Get all record Ids for this change and add it to a set for further processing
for(AccountChangeEvent acc: changes){
    List<String> recordIds = acc.ChangeEventHeader.getRecordIds();
    accIds.addAll(recordIds);
}

for(Id accId: accIds) {
    Contact c = new Contact();
    c.LastName = 'Test';
    c.AccountId = accId;
    lstcontacts.add(c);
}

   insert lstcontacts;
}

Test Code


@isTest
public class TestAccountChangeAsyncTrigger {
@isTest
static  void testAsyncContactCreate() { 
    // Enable all Change Data Capture entities for notifications.
    Test.enableChangeDataCapture();

    Test.startTest();
    // Insert an account to generate a change event.
    insert new Account(Name='TestAccount', Phone='4155551212');
    Test.stopTest();
    // The stopTest() call fires the trigger with the test account change event.

    // VERIFICATIONS
    // Check that the change event trigger created a task.
    Contact[] contactList = [SELECT Id FROM Contact];        
    System.assertEquals(1, contactList.size(),
        'The change event trigger did not create the expected contact.');
   }
}

Created a managed package and installed the package in another environment and everything worked as expected .

NOTE: You will need to enable change data capture for the object on which you want this feature .

For custom objects include the custom object in the package that will also include the async trigger as a part of dependency .

FYI


In case you are following the comments there is alternate way to write tests for this

@isTest
public class TestAccountChangeAsyncTrigger {
@isTest
static  void testAsyncContactCreate() { 

    Test.startTest();

    // Insert an account to generate a change event.
    insert new Account(Name='TestAccount', Phone='4155551212');
    // Enable all Change Data Capture entities for notifications.
    Test.getEventBus().deliver();

    Test.stopTest();
    // The stopTest() call fires the trigger with the test account change event.

    // VERIFICATIONS
    // Check that the change event trigger created a task.
    Contact[] contactList = [SELECT Id FROM Contact];        
    System.assertEquals(1, contactList.size(),
        'The change event trigger did not create the expected contact.');
  }
}
16
  • Outta curiosity what happens when you call Test.getEventBus().deliver(); ? Commented Jul 3, 2019 at 19:38
  • System.AssertException: Assertion Failed: The change event trigger did not create the expected contact.: Expected: 1, Actual: 0 Commented Jul 3, 2019 at 19:41
  • +1. Interesting, how would I test async trigger is working Commented Jul 3, 2019 at 19:44
  • You mean in test class or org ? Commented Jul 3, 2019 at 19:44
  • I mean, I can change record , it will fire async change triggers, If I wanna unit test that how can I? as Test.getEventBus().deliver(); is not workin Commented Jul 3, 2019 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .