PlatformEvent in Salesforce

Platform Event is based on Event-Driven architecture, which enables apps to communicate inside and outside of Salesforce. Platform events are based on the publish/subscribe model and work directly with a message bus, which handles the queue of incoming events and processes listening for them. This is built into real-time integration patterns in the Salesforce Platform, which helps reduce point-to-point integration.

Note : We should remove the loop parameter from loop_polling.py and client.py files. Otherwise, we will get an error.

Step 1: Create a platform event. Go to setup → Quick find box → platform events create a new platform event. Also, create a custom field called as Message__c

Step 2: We have to create a custom field to assign a message value.

Step 3: Create a custom notification type. Go to setup → quick find box → custom notifications.

Step 4: Create a flow for platform event triggers to subscribe to the platform event.

Step 5: Create a resource variable to store the recipientId (userId).

Step 6: Assign the user to the variable.

Step 7: Use action to send a notification. Mention the custom notification type ID.

Step 8: Publish platform events using Apex. Execute the below code snippet from the anonymous window.

 List<TestEvent__e> PlatformEventList= new List<TestEvent__e>();

        TestEvent__e PlatformEvent= new TestEvent__e( Message__c=’Test message’ );

        PlatformEventList.add(PlatformEvent);

        List<Database.SaveResult> results = EventBus.publish(PlatformEventList);

        for (Database.SaveResult sr : results) {

            if (sr.isSuccess()) {

                System.debug(‘Successfully published event.’);

            } else {

                for(Database.Error err : sr.getErrors()) {

                    System.debug(‘Error returned: ‘ + err.getStatusCode() );

                }

            }

        }

Result: