Wednesday, 29 November 2023

Platform Events in Salesforce

 

Publishing Platform Events :

1) Create Platform event (same as sobject in salesforce)

Publish After Commit - to have the event message published only after a transaction commits successfully. With this option, a subscriber receives the event message after the data is committed. If the transaction fails, the event message isn't published.
Publish Immediately - to have the event message published when the publish call executes, regardless of whether the transaction succeeds. With this option, a subscriber might receive the event message before the data is committed.

Example :

test_event__e testevent = new test_event__e();
testevent.test1__c = 'test1237';

System.debug('EventUuid:'+testevent.EventUuid); //unique id generated

EventBus.publish(testevent);
System.debug('EventUuid:'+testevent.EventUuid);
System.debug('ReplayId :'+testevent.ReplayId); //will get replay id in database.error method of getMessage 


replay id - return when platform event is subscribed successfully

// Inspect publishing result for each event
for (Database.SaveResult sr : results) {
    if (sr.isSuccess()) {
        System.debug('Successfully published event.'+sr.getMessage());
        
    } else {
        for(Database.Error err : sr.getErrors()) {
            System.debug('Error returned: ' +
                        err.getStatusCode() + //operation_enqueue when message is published
                        ' - ' +
                        err.getMessage()); // will get replayId in logs
        }
    }       
}

Consideration of publishing event:

Field-Level Security
All platform event fields are read only by default, and you can’t restrict access to a particular field. Field-level security permissions don’t apply and the event message contains all fields.

No Associated Tab
Platform events don’t have an associated tab because you can’t view event records in the Salesforce user interface.
No SOQL Support
You can’t query event messages using SOQL.

When publishing platform events, DML limits and other Apex governor limits apply.

Event Retention in the Event Bus

High-volume platform event messages are stored for 72 hours (3 days). Standard-volume platform event messages are stored for 24 hours (1 day). You can retrieve past event messages when using CometD clients to subscribe to a channel.


Subscribing to Platform Events :


Receive platform events in processes, flows, Apex triggers, or CometD clients.

Subscribing Platform Events

Below are the several ways to receive the platform event:

  • Subscribe to Platform Event Messages with Flows
  • Subscribe to Platform Event Messages with Processes
  • Subscribe to Platform Event Notifications with Apex Triggers
  • Subscribe to Platform Event Notifications in a Lightning Component
  • Subscribe to Platform Event Notifications with CometD(Workbench)
https://jdspaceit.wordpress.com/2018/08/10/integration-dilemma-platform-events-or-rest-soap-apis/


=======================================================================

 PlatformEventUsageMetric -used to monitor platofrm events delivered and published  is used to check number of platform events PLATFORM_EVENTS_PUBLISHED 

No comments:

Post a Comment

Heap Size and Apex CPU Time Limit

  Heap Size - 1) Memory size for holding object , variables and records 2) CPU Time limit -  1) time consuming during whole transaction   ...