Play Games

Search This Blog

Wednesday, April 6, 2022

How to publish platform event from Apex - Salesforce Globe For You

Solution: EventBus.publish() can be used to publish the platform events from Apex

Example: In this example,as soon as account is created, the platform event is fired using account trigger.

Platform Event: Account_Event__e


Account Trigger: AccountObjectTrigger

trigger AccountObjectTrigger on Account (after insert) {

    List<Account_Event__e> lstPlatformEvent = new List<Account_Event__e>();

    for(Account acc:trigger.new) {

        Account_Event__e objEvent = new Account_Event__e();

        objEvent.Account_Id__c = acc.Id;

        lstPlatformEvent.add(objEvent);

    }

    if(!lstPlatformEvent.isempty()) {

        EventBus.publish(lstPlatformEvent);

        System.debug('Event:'+ lstPlatformEvent);

    }

}

No comments:

Post a Comment