Publishing Platform Events
The Salesforce enterprise messaging platform offers the benefits of event-driven software architectures. Platform events are the event messages (or notifications) that your apps send and receive to take further action. Platform events simplify the process of communicating changes and responding to them without writing complex logic. Publishers and subscribers communicate with each other through events. One or more subscribers can listen to the same event and carry out actions. Here we will see how many ways you can able to publish the platform events in Salesforce. After a platform event has been defined in your Salesforce org, publish event messages from a Salesforce app using processes, flows, or Apex or an external app using Salesforce APIs.Here is the simple platform event object we will be using in the examples here
Here are the different ways you can able to publish the platform events in Salesforce
Option 1: Using Process Builder
You can able to publish the platform events using process builder. To publish event messages, add a Create a Record action to the appropriate process. Where you’d usually pick an object to create, select the platform event. Here is the simple process builder that will publish the platform event when an order is created
This process builder will publish the platform event whenever the order record is created.
Option 2: Using flow
Another great usage of the flow is you can able to publish the platform events using the flows. Use flows to publish event messages from a Salesforce app as part of some user interaction, an automated process, Apex, or workflow action. To publish event messages, add a Record Create or a Fast Create element to the appropriate flow. Where you’d usually pick an object to create, select the platform event. Here is the simple flow that we will be using it create a platform event on click on the button from the order record. Here is the Fast lookup screen that will fetch the order from the database.
Here is the record create screen that will be inserting the data into the platform event.
Final Flow looks like below
Now create a quick action from the flow and add it to the order page layout. once you click on the quick action then it will publish into the platform events.
Option 3: Using Apex
You can able to publish the platform event using apex. To publish event messages, call the EventBus.publish method. Here is the sample apex trigger that will create platform events.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
trigger Ordertrigger on Order (after insert) {
List<Order_Fulfillment__e> listOfOF = new List<Order_Fulfillment__e>();
for(Order o :Trigger.new){
Order_Fulfillment__e ofD = new Order_Fulfillment__e() ;
ofD.Is_Primary__c = true ;
ofD.Order_Date__c = System.today() ;
ofD.Order_Details__c = o.Description ;
ofD.Order_Id__c =o.Id ;
ofD.Sales_Price__c = o.TotalAmount ;
ofD.Status__c = o.Status ;
ofD.Type__c = o.Type;
listOfOF.add(ofD);
}
List<Database.SaveResult> results = EventBus.publish(listOfOF);
for (Database.SaveResult sr : results) {
if (sr.isSuccess()) {
System.debug('Successfully published event.');
} else {
for(Database.Error err : sr.getErrors()) {
System.debug('Error returned: ' +
err.getStatusCode() +
' - ' +
err.getMessage());
}
}
}
}
|
Option 4: Using External Apps
You can able to publish the platform events by using third-party apps with API support. For example, you can able to publish the platform events using SoapUI. Please refer to this link
Option 5: Using Salesforce APIs
External apps use an API to publish platform event messages. Publish events by creating records of your event in the same way that you insert sObjects. You can use any Salesforce API to create platform events, such as SOAP API, REST API, or Bulk API. When publishing an event message, the result that the API returns contains information about whether the operation was successful and the errors encountered. If the success field is true, the event was published for a standard-volume event. For a high-volume event, the publish request is queued in Salesforce and the event message might not be published immediately. If the success field
is false, the event publish operation resulted in errors, which are returned in the errors field. The returned result also contains the Id system field. The Id field value is not included in the event message delivered to subscribers. It is not used to identify an event message, and is not always unique. Subscribers can use the ReplayId system field, which is included in the delivered message, to identify the position of the event in the stream. To publish a platform event message using REST API, send a POST request to the following endpoint.
is false, the event publish operation resulted in errors, which are returned in the errors field. The returned result also contains the Id system field. The Id field value is not included in the event message delivered to subscribers. It is not used to identify an event message, and is not always unique. Subscribers can use the ReplayId system field, which is included in the delivered message, to identify the position of the event in the stream. To publish a platform event message using REST API, send a POST request to the following endpoint.
1
|
/services/data/v44.0/sobjects/Order_Fulfillment__e/
|
You can able to send the post request as shown below from workbench as shown below.
Comments
Post a Comment