Skip to content

Instantly share code, notes, and snippets.

View brianbier's full-sized avatar
:octocat:

Brian Bier brianbier

:octocat:
View GitHub Profile
@brianbier
brianbier / sfdx-throttle-platformevents.apex
Created November 4, 2021 18:04 — forked from c4tch/sfdx-throttle-platformevents.apex
Throttle Apex Platform Events to avoid overloading trigger DML
/**
* Entrypoint from the Trigger, dispatch the events here
* Bobby White
* The use case is universal... you're never going to be able to consume 2000 PE in a single Apex transaction if the PE are independent.
* You have to "clip" the executions into manageable chunks.
* Best case N = 200, in practice if the DML's will touch objects that are poorly governed, it could hit limits at a lower number like 25 or 50.
* Q: What is "Independent"?
* A: Let's say that you had a flood of PE's that had the applied to the same ExternalId, you might be able to consume these with a single DML (e.g. 10 meter readings for the same Meter, or a series of notifications for the same Client) -- if your use case doesn't demand that you preserve every state change, you could collapse them into one. My default assumption is that you need to design your PE trigger with the premise that the PE's stand alone and have no correlation to other PE's in the same invocation.
* I get you, so in the PE loop you have here you might collapse sim