Newer Version Available

This content describes an older version of this product. View Latest

Get Notified of Asynchronous Publish Errors (Beta)

When you publish a high-volume platform event, the event message is published asynchronously. When isSuccess() returns true, the operation is queued in Salesforce. If later the enqueued publish operation fails, you can be notified of the error by subscribing to the /event/AsyncOperationEvent channel.

The /event/AsyncOperationEvent beta channel is no longer available to new subscriber Salesforce orgs as of October 12, 2019. Some Salesforce orgs that subscribed to the channel between September 13, 2019 and October 12, 2019 will continue to have access to it. As a beta feature, the /event/AsyncOperationEvent channel is a preview and isn’t part of the “Services” under your master subscription agreement with Salesforce. Use this feature at your sole discretion, and make your purchase decisions only on the basis of generally available products and features. Salesforce doesn’t guarantee general availability of this feature within any particular time frame or at all, and we can discontinue it at any time. This feature is for evaluation purposes only, not for production use. It’s offered as is and isn’t supported, and Salesforce has no liability for any harm or damage arising out of or in connection with it. All restrictions, Salesforce reservation of rights, obligations concerning the Services, and terms for related Non-Salesforce Applications and Content apply equally to your use of this feature. You can provide feedback and suggestions for the /event/AsyncOperationEvent channel in the Trailblazer Community.

Note

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