Newer Version Available
Publish and Receive Platform Events
Use EMP Connector to subscribe to a custom platform event that you defined
earlier.
To create the Low_Ink__e platform event, follow the steps in Define a Custom Platform Event.
-
Publish event messages in the Developer Console using Apex. In this example, you
publish the Low_Ink__e event messages.
- In Salesforce Classic, select .
- In Lightning Experience, click the quick access menu, and select Developer Console.
- Select .
-
In the new window and replace the contents with this Apex code snippet.
1// Create event instance. 2Low_Ink__e event = new Low_Ink__e(Printer_Model__c='XZO-5', 3 Serial_Number__c='12345', 4 Ink_Percentage__c=0.2); 5 6// Publish event. 7Database.SaveResult sr = EventBus.publish(event); 8 9// Inspect publishing result for each event 10if (sr.isSuccess()) { 11 System.debug('Successfully published event.'); 12} else { 13 for(Database.Error err : sr.getErrors()) { 14 System.debug('Error returned: ' + 15 err.getStatusCode() + 16 ' - ' + 17 err.getMessage()); 18 } 19} -
Click Execute. After the platform event is published, EMP
Connector receives an event notification, which is printed in the console. The output
is similar to this example.
1Subscribed: Subscription [/event/Low_Ink__e:-1] 2Received: 3{ 4 "schema": "5E5OtZj5_Gm6Vax9XMXH9A", 5 "payload": { 6 "CreatedById": "005RM000002Qu16YAC", 7 "CreatedDate": "2022-03-30T22:36:10.248Z", 8 "Printer_Model__c": "XZO-5", 9 "Serial_Number__c": "12345", 10 "Ink_Percentage__c": 0.2 11 }, 12 "event": { 13 "EventApiName": "Low_Ink__e", 14 "EventUuid": "8b16da8a-a48a-464a-aee4-ea5c01134b0e", 15 "replayId": 10315 16 } 17}