Newer Version Available

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

Match a Publish Result with the Event Published

Use the EventUuid field value to match the event status with the event published. The EventUuid field is a universally unique identifier (UUID) that identifies the event message and correlates the publish result of each event with the original publish call.

After getting the EventUuid field value, save the UUID along with event field values. If you save event field values, you can republish the same events if the publishing fails.

If you published the event using Salesforce APIs, the SaveResult returned contains the UUID in the Error message field. This example contains the save result of an event inserted using a REST API POST request.

1{
2  "id" : "e01xx0000000001AAA",
3  "success" : true,
4  "errors" : [ {
5    "statusCode" : "OPERATION_ENQUEUED",
6    "message" : "e981b488-81f3-4fcc-bd6f-f7033c9d7ac3",
7    "fields" : [ ]
8  } ]
9}

If you published the event in Apex, you can obtain the UUID by calling EventBus.getOperationId(saveResult).

This example gets the UUID from the event publish call using Apex.

Prerequisites: Before you can run this example, define a platform event with the label of Order Event and the following fields: Order Number of type Text(10) and Has Shipped of type Checkbox.

1// Publish a high-volume event message
2Order_Event__e evt = new Order_Event__e(
3    Order_Number__c='17',
4    Has_Shipped__c = false);
5Database.SaveResult sr = EventBus.publish(evt);
6// Inspect immediate result
7if (sr.isSuccess() == true) {
8    System.debug('Successfully enqueued event for publishing.');
9    // Get the UUID that uniquely identifies this event publish
10    System.debug('UUID=' + EventBus.getOperationId(sr));
11} else {
12   for(Database.Error err : sr.getErrors()) {
13       System.debug('Error returned: ' +
14                    err.getStatusCode() +
15                    ' - ' +
16                    err.getMessage());
17   }
18}
19
20// Debug message output:
21//|DEBUG|Successfully enqueued event for publishing.
22//|DEBUG|UUID=6ba5db7e-c27b-4a67-a3c5-cf425ffcaf53