Newer Version Available
Get Notified of Asynchronous Publish Errors (Beta)
Error Event Notification Example
This example shows an AsyncOperationEvent message received for a high-volume event publish failure. The information about the failure is included in the OperationDetails object. The error status code for asynchronous publish failure is PLATFORM_EVENT_PUBLISH_FAILED. The event message that couldn’t be published due to a system error is in the SourceEvent object. The error notification message also includes the operationId field, which is the same operation ID that the publish call returns. Use this ID to match the error with its corresponding publish call.
1{
2 "schema":"MKsvE2J5292JLE7HNWz6Dg",
3 "payload":{
4 "OperationDetails":[
5 {
6 "Status":"FAILURE",
7 "Fields":[
8 "HighVolumePlatformEvent"
9 ],
10 "Category":"PlatformEventPublishError",
11 "Message":"The platform event message could not be published. Try again later.",
12 "StatusCode":"PLATFORM_EVENT_PUBLISH_FAILED"
13 }
14 ],
15 "CreatedById":"005xx000001X7gTAAS",
16 "OperationId":"f6477da8-f664-446a-aee8-df651f9c3a50",
17 "CreatedDate":"2019-02-26T00:48:46Z",
18 "SourceEvent":{
19 "com.sforce.eventbus.News_Event__e":{
20 "Location__c":"Mountain City"
21 }
22 }
23 },
24 "event":{
25 "replayId":1
26 }
27}Match the Error Notification with the Publish Call Using the Operation ID
When the publish call enqueues the event message in Salesforce and returns a successful response, it contains an operation ID. The operation ID uniquely identifies the publish operation and is used to match the error notification with the publish call. This example shows the response of a REST API sObject POST request to publish a platform event message. The response includes the operation ID in an errors array element in the message field with a status code of OPERATION_ENQUEUED.
1{
2 "id" : "e00xx0000000001AAA",
3 "success" : true,
4 "errors" : [ {
5 "statusCode" : "OPERATION_ENQUEUED",
6 "message" : "f6477da8-f664-446a-aee8-df651f9c3a50",
7 "fields" : [ ]
8 } ]
9}To get the operation ID in Apex, call the EventBus.getOperationId(SaveResult) and pass in the returned SaveResult that EventBus.publish() returns. This example publishes a News Event and stores the returned SaveResult. If the publish is successful, the operation ID is obtained with the EventBus.getOperationId(sr) call and written to the debug log using the System. debug() statement. For illustration purposes, the example gets the status code and message in the first Error object and writes them to the debug log. The message value is the same operation ID that the EventBus.getOperationId(sr) returns.
1// Publish a high-volume event message
2News_Event__e myEvent = new News_Event__e(
3 Location__c='Mountain City'
4);
5// Call method to publish event
6Database.SaveResult sr = EventBus.publish(myEvent);
7
8// Inspect publishing result
9if (sr.isSuccess()) {
10 System.debug('Successfully enqueued event for publishing.');
11
12 // Get asynchronous operation ID.
13 System.debug(EventBus.getOperationId(sr));
14
15 // The getOperationID call above is equivalent to the err.getMessage() call
16 for(Database.Error err : sr.getErrors()) {
17 System.debug('Error returned: ' +
18 err.getStatusCode() +
19 ' - ' +
20 err.getMessage());
21 }
22} else {
23 for(Database.Error err : sr.getErrors()) {
24 System.debug('Error returned: ' +
25 err.getStatusCode() +
26 ' - ' +
27 err.getMessage());
28
29 }
30}
31
32// Debug messages output:
33//|DEBUG|Successfully enqueued event for publishing.
34//|DEBUG|f6477da8-f664-446a-aee8-df651f9c3a50
35//|DEBUG|OPERATION_ENQUEUED - f6477da8-f664-446a-aee8-df651f9c3a50