Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»How to Correctly Publish Platform Event Using Salesforce Apex

    How to Correctly Publish Platform Event Using Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniDecember 7, 2024Updated:January 15, 2025No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Publish Platform Event
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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.

    1. Salesforce Apex
    2. REST API
    3. SOAP API
    4. Flow
    5. External Systems using CometD Library and Salesforce Event Relay
    6. Developer Console
    7. 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.

    LabelAPIType
    Product CodeProductCode__cText
    QuantityQuantity__cNumber (2,0)
    Customer CodeCustomerCode__cText
    Mobile NumberMobileNumber__cText

    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

    • Publishing Platform Events
    • Test that a Platform Event was published?

    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

    apex architecture event bus integration platform event salesforce salesforce apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Manage Technical Debt in Salesforce
    Next Article Salesforce Winter ’25 Release: Top Flow Enhancements You Need to Know
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.