[SalesForce] Apex Platform Events Subscriber not always triggered

I am currently trying to troubleshoot a Platform Events implementation that seems to behave inconsistently. Here's the current architecture:


I have an Apex Trigger on the Account SObject, where I publish some Platform events. I also have a trigger on that Platform Event SObject, responsible of creating records in Salesforce. I enabled Debug Logs for the Automated Process, subscribed to the Platform Event using Workbench and started creating some Accounts to generate some Platform Events.


Expected Outcome:

2 Contacts to be created for each Account created (due to the Platform Event Apex Trigger)

Actual Outcome:

While looking at the logs generated following the creation of an Account, every request is generating a Platform Event record successfully. However, it seems like the business logic associated to the Platform Event is executed some times – I can assess this by looking at the output data in Salesforce + missing Automated Process logs in Salesforce for some of the Platform Events created. I was able to assess that ALL the Events are generated successfully subscribing as well via Workbench and receiving every Platform Events I'd expect.


My questions:

  • Is there a reason why Workbench would get notified of an event, but not an Apex Trigger on the exact same Platform Events?
  • Is there a chance they are stuck in a queue that is due to be executed at a future time?
  • If the previous is possible, is it possible to replay the events that Salesforce skipped?

Thanks

Best Answer

Is there a reason why Workbench would get notified of an event, but not an Apex Trigger on the exact same Platform Events?

Yes. Triggers on platform events run asynchronously, so that may be a reason. From documentation, refer to the below excerpt.

Unlike triggers on standard or custom objects, triggers on platform events don’t execute in the same Apex transaction as the one that published the event. The trigger runs asynchronously in its own process under the Automated Process entity. As a result, there might be a delay between when an event is published and when the trigger processes the event.

For:

Is there a chance they are stuck in a queue that is due to be executed at a future time?

Yes, because of their asynchronous nature as stated above.

For:

If the previous is possible, is it possible to replay the events that Salesforce skipped?

I know if you are subscribed using CometD, you can always use the ReplayId available within last 24 hours to fetch the events. In this scenario, I would think that the events are not missed but its only a matter of time when those appear because of their asynchronous nature. Something from the documentation:

An Apex trigger processes platform event notifications sequentially in the order they’re received. The order of events is based on the event replay ID. An Apex trigger can receive a batch of events at once. The order of events is preserved within each batch. The events in a batch can originate from one or more publishers.

Related Topic