Salesforce Platform Events Complete Guide: Real-Time Integration with Apex, REST, and Examples

What Are Platform Events in Salesforce?

Platform Events are custom event messages in Salesforce.

They follow event-driven architecture.

One system sends an event.

Many systems can listen and respond.

Platform Events vs Traditional Integration

Traditional integrations:

Are tightly connected

Are hard to scale

Can slow down systems

Platform check here Events:

Are asynchronous

Are loosely coupled

Scale very well

Platform Events vs Change Data Capture (CDC)

Feature

Platform Events

Change Data Capture

Purpose

Business events

Data changes

Custom Fields

Yes

No

Triggered By

Code or API

Record changes

Control

Full control

Automatic

Best Use

Integrations

Data sync

???? Use Platform Events when you control the message

???? Use CDC when you want to track record changes

Step 1: Create a Platform Event

Example Platform Event:

Payment_Event__e

Fields:

Payment_Id__c (Text)

Amount__c (Number)

Status__c (Text)

Step 2: Publish Platform Event Using Apex

Payment_Event__e paymentEvent = new Payment_Event__e(

Payment_Id__c = 'PAY-001',

Amount__c = 1000,

Status__c = 'Success'

);

EventBus.publish(paymentEvent);

✔ Event is sent

✔ Systems receive it asynchronously

Step 3: Platform Event Trigger (Subscriber)

trigger PaymentEventTrigger on Payment_Event__e (after insert)

for (Payment_Event__e evt : Trigger.new)

System.debug('Payment Status: ' + evt.Status__c);

✔ Runs in background

✔ Does not slow user actions

Publishing Platform Events from External Systems (REST API)

External systems can publish Platform Events using REST.

REST Endpoint Format

/services/data/v58.0/sobjects/Payment_Event__e/

Sample JSON Payload

"Payment_Id__c": "PAY-002",

"Amount__c": 1500,

"Status__c": "Pending"

Why This Is Powerful

External apps push events to Salesforce

Salesforce reacts instantly

No custom REST service needed

Subscribing to Platform Events Externally (CometD)

External systems can listen to events using:

CometD

Streaming API

This allows:

Real-time dashboards

Event-based microservices

Live notifications

Unit Testing Platform Events in Apex

Platform Events can be tested, but logic must be testable.

Best Practice

Move logic into a handler class.

Handler Class

public class PaymentEventHandler

public static void handle(List events)

for (Payment_Event__e evt : events)

System.debug('Handling event: ' + evt.Payment_Id__c);

Trigger

trigger PaymentEventTrigger on Payment_Event__e (after insert)

PaymentEventHandler.handle(Trigger.new);

Test Class

@IsTest

public class PaymentEventTest

@IsTest

static void testPlatformEvent()

Test.startTest();

Payment_Event__e evt = new Payment_Event__e(

Payment_Id__c = 'TEST-001',

Amount__c = 500,

Status__c = 'Success'

);

EventBus.publish(evt);

Test.stopTest();

System.assert(true);

✔ Event published

✔ Trigger executed

✔ Test passes

Error Handling in Platform Events

Salesforce provides:

Automatic retry

Durable event storage

Guaranteed delivery (within limits)

Best Practice

Log errors in custom objects

Avoid heavy logic in triggers

Use Queueable or Batch Apex if needed

Platform Events Best Practices

✅ Keep event payload small

✅ Name events clearly

✅ Avoid SOQL inside loops

✅ Use handler classes

✅ Monitor events using Event Monitoring

Real-Time Use Case Example

Order Completed Flow

Order marked as completed

Platform Event is published

Billing system creates invoice

Shipping system starts delivery

Notification system sends email

All systems work independently and in real time.

When NOT to Use Platform Events

❌ For simple UI logic

❌ For large data volumes

❌ For record validation

Use them only for event-based communication.

Conclusion

Salesforce Platform Events are a modern and powerful integration tool.

They help build scalable, real-time, loosely connected systems.

If you want clean architecture and strong integrations, Platform Events are a must ????

Leave a Reply

Your email address will not be published. Required fields are marked *