Event Framework

A lightweight, metadata-driven platform event framework for Salesforce

Apex Framework
100% Test Coverage
Metadata-Driven
Overview
A lightweight, dynamic, and comprehensive Salesforce platform event framework that provides a clean, extensible architecture for handling integration events across your org.

Platform events are a powerful tool for decoupling systems and enabling real-time integration, but without proper structure, they can become difficult to manage and debug. This framework provides a clean, metadata-driven approach to handling platform events that promotes:

  • Separation of concerns - Event handling logic is separated from dispatching logic
  • Metadata-driven configuration - Event handlers are configured through custom metadata
  • Extensibility - Easy to add new event handlers without modifying existing code
  • Testability - Comprehensive test coverage with mock handlers
  • Error handling - Robust error handling and logging throughout the framework
Architecture
The framework follows a dispatcher pattern where platform events are automatically routed to their appropriate handlers based on metadata configuration.
Platform Event → IntegrationEventDispatcher → Handler Mapping (Metadata) → Event Handler → Business Logic
Installation
Deploy this unlocked package directly using one of these methods

Install in Production:

Install in Sandbox:

Components
Key components of the Event Framework

IntegrationEventDispatcher

The core dispatcher that routes platform events to their appropriate handlers based on custom metadata configuration.

IntegrationEventDispatcher.dispatchEvents(events);

IEventHandler Interface

Defines the contract for all event handlers in the framework.

public interface IEventHandler {
  void handleEvent(Integration_Event__e event, Map<String, Object> eventData);
  void sendResponse(String message, String externalRecordId);
}

BaseEventHandler

Abstract base class that provides common functionality for event handlers.

public abstract class BaseEventHandler implements IEventHandler {
  public void sendResponse(String message) {
    EventUtility.sendResponse(message);
  }
}
Custom Metadata Configuration
Configure event handlers through custom metadata

Configure event handlers through custom metadata using Integration_Handler_Setting__mdt:

FieldDescriptionExample
Event_Type__cThe event type to handleAccountCreated
Apex_Handler_Class__cThe handler class nameAccountEventHandler
Active__cWhether the handler is activetrue
Implementation Example
How to create and use an event handler

1. Create an Event Handler

public class AccountEventHandler extends BaseEventHandler {
    
    public override void handleEvent(Integration_Event__e event, Map<String, Object> eventData) {
        try {
            String accountId = (String) eventData.get('accountId');
            String accountName = (String) eventData.get('name');
            
            // Your business logic here
            processAccountCreation(accountId, accountName);
            
            // Send response if needed
            sendResponse('Account processed successfully', accountId);
            
        } catch (Exception ex) {
            System.debug(LoggingLevel.ERROR, 'Error processing account event: ' + ex.getMessage());
        }
    }
    
    private void processAccountCreation(String accountId, String accountName) {
        // Your implementation here
    }
}

2. Publish Events

// In your trigger, flow, or other Apex code
Map<String, Object> eventData = new Map<String, Object>{
    'accountId' => account.Id,
    'name' => account.Name,
    'industry' => account.Industry
};

Integration_Event__e event = EventUtility.createEvent(
    'AccountCreated', 
    JSON.serialize(eventData),
    account.Id
);

EventUtility.publishEvents(new List<Integration_Event__e>{ event });
Platform Event Schema
Integration_Event__e platform event structure
FieldTypeDescription
Event_Type__cText(255)The type of event being published
Data__cLong Text AreaJSON data payload
Environment__cText(255)Environment identifier
Related_Id__cText(255)Related record ID
Best Practices
  • Event Naming: Use descriptive, consistent event type names (e.g., AccountCreated, OpportunityClosed)
  • Data Structure: Use consistent JSON structure for event data
  • Error Handling: Always include try-catch blocks in your handlers
  • Testing: Write comprehensive tests for all event handlers
  • Metadata Management: Use custom metadata to manage handler configuration
  • Logging: Use appropriate logging levels for debugging and monitoring