Platform Event is a special object for real-time communication between Salesforce and external systems or internal processes. It is part of event-driven architecture and supports publish-subscribe communication. Platform events deliver notifications and data changes in and out of the Salesforce environment. This model is based on the publisher-subscriber model; publishers broadcast events while subscribers respond.
Different Ways to Publish Platform Events
We need to publish platform events to communicate with third-party services. There are multiple ways to publish in Salesforce. Below are 7 ways to publish platform events.
- Salesforce Apex
- REST API
- SOAP API
- Flow
- External Systems using CometD Library and Salesforce Event Relay
- Developer Console
- Integration Tools like Postman and Workbench.
This post will explain how to correctly publish platform events using Salesforce Apex.
Use Case:
ABC Corp is selling products on e-commerce site. It handles all process from product management to delivery of products to end user. Each process is handled by different team or company. These teams required some kind of notification when products are purchased by customer so that they can start processing on it. These team/business can use same or different types of applications.
Solution
Based on above mentioned use case, we will create a platform event Product Purchase Event for product purchase. As soon as customer buy any product, Salesforce publish this platform event. We will required below fields in this platform event object.
Label | API | Type |
---|---|---|
Product Code | ProductCode__c | Text |
Quantity | Quantity__c | Number (2,0) |
Customer Code | CustomerCode__c | Text |
Mobile Number | MobileNumber__c | Text |
Platform events are used as it will provide retry functionality and 72 hours of message storage for durability. We can use asynchronous and synchronous ways to publish platform events.
Asynchronous Processing
Salesforce recommand asynchronous way to publish events in Salesforce Apex. It recommand to use EventBus class to publish event that publish event asynchronously. This class has publish method that publishes events to the EventBus. It will take platform records as input and send them to subscribers.
We will use below apex code to publish platform event for above mentioned fields. Based on fields code can vary.
EventBus class hides all complexity of event processing and developer only focused on his/her business logic implementation. It handle retry functionality as well.
Snchronous Processing
As Platform Event is special kind of object, we can also utilize insert command to create platform events.
Disadvantage of Snchronous Processing
- As this is snchronous ways to insert record, if we publish many records in one go then system might slow down and user has to wait for operation completion.
- Developer has to maintain security and retry functionality in this way of publish platform events.
- As it does not leverage the benefits of event-driven architecture, it might lead to potential performance issues.
Advantage of Snchronous Processing
- Developer are familier with DML statements so it is easy for them to use this approach.
- Developers has also benefit of controlling transaction and putting custom logic before publishing it.
This approach can throw error Argument must be of internal sObject type when it will be used in future callout. To avoid such errors use EventBus.Publish method.
Comparision of both approach
If compared by the number of API calls and used resources, EventBus.publish is defined as the more efficient tool for Platform Event publishing, especially in those cases when real-time processing and asynchronous communication are crucial. It is also designed for event driven system and will not pose a problem for bulk publishing.
On the other hand, Database.insert could be more appropriate in a situation, where you can include more complex logic or modify contents of the event data before their publishing. But it may imply latency in the account of the fact that such architecture is synchronous in nature.
Summary
Choice between EventBus.publish
and Database.insert
depends on the specific requirements of application. For real-time, high-volume event publishing, EventBus.publish
is typically the preferred method. For scenarios requiring more control and customization, Database.insert
may be the better option, albeit with potential performance trade-offs.
References
Related Posts
- Efficient Ways to Debug Salesforce Platform Events
- Ultimate Guide to Monitoring Platform Events using Streaming Monitor
- Publish Platform Events from ASP.NET
Similar Posts
- Best Practices to Avoid Hardcoding in Apex for Cleaner Salesforce Code
- Salesforce DevOps for Developers: Enhancing Code Quality and Deployment Efficiency
- Best Code Analysis Tools For Salesforce Development
- DML or SOQL Inside Loops
- Limiting data rows for lists
- Disabling Debug Mode for production
- Stop Describing every time and use Caching of the described object
- Use Filter in SOQL
- Avoid Heap Size
- Optimize Trigger
- Bulkify Code
- Foreign Key Relationship in SOQL
- Defer Sharing Rules
- Avoid Hardcode in code
- Use Platform Caching
- Avoid Batch Apex and Use Queueable Class
- Queueable Vs Batch Apex In Salesforce