How to Define, Publish and Subscribe to Basic Platform Event

Platform events are an ideal feature for monitoring your own system as well as staying connected to any external systems you may be using.

When developing large scale applications or systems you may need to communicate or interact with external systems, Platform events are an excellent solution to this challenge.

 

At a very high level, platform events are quite simply custom notifications but are more secure and scalable then regular events.

To use platform events there must be two parties present for it to work, a sender & a receiver. 

 

There are some basic terms I will reference throughout this post: 

Event: An event is any change that occurs in any of your business processes within your system, eg. Opportunity closed, Order placed, Task created etc.

Event Message: An event message is quite simply the data which the event is sending so for example if a platform event is fired because an order has been placed, the event message for that platform message could contain data such as, Product Id, Colour, quantity, etc. This changes depending on what platform event is fired.

Event Producer: This is the system/Application that fires the platform event.

Event Channel: This is a stream of event’s where event producers' messages are sent and where event consumers can access those messages.

Event Consumer: This is the system/Application that is listening to the event and subscribes to the event channel to receive those messages.

Three main options for when to use Platform events would be:

  • Platform to External app
  • External app to Platform
  • Platform to Platform

I am now going to create a platform event called ‘Pexlify News’.

To create this event, you will need to navigate to ‘Setup’ on your salesforce org, Once here enter Platform events and select platform events.

Then create a new platform event. Here is the information I am going to use to create my platform event :

Once you have the Platform Event created, you will want to add a couple of custom fields to it as this will be the Event Message that we will be sent when an event is fired. I’ve added the below fields to my Platform Event:

Once finished adding these custom fields, congratulations, you have now defined your first Platform event.
Our next step is to publish it. To do this part I will be using a block of Apex code in the Execute Anonymous Window in the developer console.

Below is the block of apex code I used to publish my first Platform event:

Pexlify_News__e newsEvent = new Pexlify_News__e(       Post_Title__c = 'Mountain City',        Post_Date__c = system.today(),       Post_Content__c = 'Lightning components by nature are independent components. '+                         'This means they can be placed next to different component in different '+                         'places within Salesforce and can adjust to different widths on the screen. '+                         'Since it’s very likely our custom components would be placed right next to the '+  'Salesforce standard components, by making our components follow the same     design '+                         'principals our end solution would look cohesive.'); EventBus.publish(newsEvent);

Once you receive the Success log in the Logs section of the Developer console you will have successfully published your platform event.

Now we move on to our final step, subscribing to the platform event we have just created. There are many different ways that you can subscribe to platform events in salesforce:

Apex Triggers

  • Lightning web components
  • Aura components
  • Process builder
  • CometD

For the purpose of this blog post, I will be subscribing to the platform event using the process builder.

The first step in using the process builder to subscribe to the platform event I created earlier in the blog post is to simply create a new process.

The next step is to specify when I want the process to start, In the picture below you can see that I set up the process to start when any contact in the Org has a first name that matches the platform events ‘Post Creator’.

The next step is to define the criteria for the action group of this process, as you can see in the picture below, I set it up to have no criteria and just execute the actions.

The final step is to define what actions you want the platform event to take, to make this blog post simple I have decided to take the platform events ‘Post Content’ and make it the description of the contact. Once done activate the process.

Now the final step of this blog post is to check that the platform event is working as expected, I have contact with the first name of the ‘Post Creator’ and an empty description on his profile in my org. I will now fire the same platform event using the Execute Anonymous Window in the developer console from above. The code I will be using is the exact same as above.

Pexlify_News__e newsEvent = new Pexlify_News__e(       Post_Title__c = 'Mountain City',        Post_Date__c = system.today(),       Post_Content__c = 'Lightning components by nature are independent components. '+                         'This means they can be placed next to different component in different '+                         'places within Salesforce and can adjust to different widths on the screen. '+                         'Since it’s very likely our custom components would be placed right next to the '+  'Salesforce standard components, by making our components follow the same     design '+                         'principals our end solution would look cohesive.'); EventBus.publish(newsEvent);

As expected the contacts description has been updated from empty to the post content in the platform event. Congratulations, you have just defined, published & subscribed to your first platform event. This is just a small showing of how powerful platform events actually are.