Example: Publish Callback Class That Correlates Callback Results with Event Messages
This example callback class implementation shows how to retry publishing failed events. It’s based on a trigger on the Order object.
If event publishing fails, the onFailure method in the FailureCallbackWithCorrelation class is invoked. This method retries publishing failed events up to two times. A map holds the UUID values of each published event and maps it to the order record ID. This mapping is used to populate the event Order_Id__c field. Alternatively, you can use the record ID to obtain field data from the record and populate event fields. The example omits this detail for simplicity.
The examples in this section require a platform event, Order Event, to be defined with a Text(18) field of Order Id.
For each inserted or updated order record, the trigger publishes the Order_Event__e platform event with a populated EventUuid field.
To run the trigger, insert an order record. Because an order depends on an account and contract, create these records first. You can create the records in the user interface or via Apex or an API. An Apex snippet is provided for your convenience. You can run this snippet in the Developer Console, in the Execute Anonymous Window.