Skip to main content

Exploring Platform Events in Salesforce

article banner

Quick Glance

Real-time data sharing is crucial for maintaining seamless operations across systems. Salesforce’s Platform Events provide an effective solution for this, enabling smooth communication within the Salesforce environment and with external systems. Discover the benefits, challenges, and practical applications of platform events to see how they can improve your data integration and synchronization efforts.


 

What are Platform Events?

Platform events are secure and scalable messages that carry data between different systems. They follow a publish-subscribe model:

  • Publishers: Entities that send out event messages.
  • Subscribers: Entities that receive and act upon these messages.

Platform Events follow a publish-subscribe model. This means that publishers send event messages, and subscribers receive these messages in real-time. These events are defined with custom fields to hold the necessary data. Once set up, they can be published from within Salesforce using Apex, processes, or flows, or from external systems using Salesforce APIs.

Why use Platform Events?

  1. Real-Time Data Exchange: Platform events facilitate instantaneous data sharing, ensuring all connected systems are updated without delay.
  2. Decoupling Systems: They allow systems to operate independently while still communicating effectively, reducing dependencies.
  3. Scalability: Designed to handle high volumes of events, platform events are ideal for large-scale applications.
  4. Simplified Integration: They streamline the integration process between Salesforce and external systems, minimizing the need for complex point-to-point integrations.

Platform Events are part of Salesforce’s enterprise messaging platform, providing an event-driven messaging architecture to enable apps to communicate inside and outside of Salesforce. This architecture is crucial for modern applications that require real-time data synchronization and integration.

Architecture Explained with Diagram 

 

Key Concepts

  • Event: An event represents a change in state that is meaningful within a business process.
  • Event Message: An event message, also known as an event notification, contains data about the event.
  • Event Producer (Publisher): The event producer is the entity that publishes the event message. It initiates the communication by sending out the event notification.
  • Event Channel: An event channel is a stream of events where an event producer sends event messages, and event consumers read those messages.
  • Event Consumer (Subscriber): The event consumer is a subscriber to the event channel that receives messages from the channel.
  • Event Bus: The event bus is a multi-tenant, multi-cloud event storage and delivery service based on a publish-subscribe model. It allows the retrieval of stored event messages at any time during the retention window. The event bus operates on a time-ordered event log, ensuring that event messages are stored and delivered in the order they are received by Salesforce.

Pros and Cons of Platform Events

Pros:

  • Efficiency: Real-time updates enhance operational efficiency.
  • Flexibility: Decoupled systems can be updated or replaced without affecting others.
  • Scalability: Suitable for handling large volumes of data.
  • Ease of Use: Simplifies integration with external systems.
  • Custom Field Validation: Platform event records ensure that custom field attributes like Required and Default are enforced.
  • Supports for both Configuration as well as Customization: Platform Event can be published or subscribed by using Flows, Process Builder(Deprecated) and Apex.

Cons:

  • Read-Only Fields: All platform event fields are read-only by default, and field-level security permissions do not apply.
  • No UI Visibility: Platform events do not have an associated tab and can’t be viewed in the Salesforce user interface.
  • No SOQL Queries: You cannot query event messages using SOQL.
  • At-least-once Event Publishing and Duplicate Events: Asynchronous platform event publishing uses the at-least-once pub/sub model, not the exactly-once model. With the at-least-once model, there is a small chance of duplicate events, because if the system encounters an internal error when publishing the queued event, it retries the publishing.

Real-time Example: Retail Order Management

Consider a retail company that uses Salesforce to manage orders and an external inventory management system that needs real-time updates whenever an order is placed or modified.

Step-by-Step Implementation:

  • Define the Platform Event: Create an OrderEvent with fields like OrderId, Status and Quantity.
  • Publish the Event: When an order is placed or updated in Salesforce, publish an OrderEvent.
  • Subscribe to the Event in Salesforce: You can subscribe to platform events within Salesforce using Apex triggers, Process Builder, or Flow Builder.
  • Subscribe to the Event: The external inventory management system subscribes to the OrderEvent. Upon receiving the event, it updates the inventory accordingly. This can be achieved using a CometD client or the Pub/Sub API.
  • Real-Time Update: As soon as the order is placed in Salesforce, the inventory system is updated in real-time, ensuring accurate stock levels.

Error Handling

Handling errors effectively is crucial for maintaining the reliability of platform events. Here are some strategies:

  • Try-catch Blocks: Use try-catch blocks in Apex triggers to handle exceptions and ensure that errors are logged for further analysis.
  • EventBus.RetryableException: For transient errors, you can use EventBus. RetryableException to retry the event processing. 

    if (someCondition) { 

        throw new EventBus.RetryableException('Retrying event processing'); 

    }

  • Custom Error Handling: Create custom objects to log errors and monitor them through Salesforce reports and dashboards.

Conclusion

Salesforce platform events offer a robust solution for real-time data exchange and system integration. Despite their associated challenges, the advantages they provide—such as enhanced operational efficiency, improved data accuracy, and seamless communication across various systems—make them an invaluable asset for businesses. By effectively utilizing platform events, organizations can stay ahead in the competitive landscape, ensuring their operations are both timely and efficient.

Related reads.

WHAT WE DO.

Explore our wide gamut of digital transformation capabilities and our work across industries.

Explore