Salesforce Platform Events with .NET
tl;dr You can find a full working copy here https://github.com/abierhaus/salesforce-platform-events-dotnetcore
Using Salesforce REST-APIs is the most used way for most companies to integrate Salesforce in their existing IT landscape.
As in many integration cases, integration via APIs is time-consuming since a separate integration usually has to be developed and, above all, maintained for each system and use case.
It was worth taking a look at the Salesforce platform events for architects and developers, which are relatively new in the Salesforce Developer ecosystem.
Platform events are part of Salesforce’s enterprise messaging platform. The platform provides an event-driven messaging architecture to enable apps to communicate inside and outside of Salesforce.
In simple terms, the Salesforce Platform Events is a separate event bus within the Salesforce Cloud that receives a message with a payload used in Apex/ Process Builder/ etc.
In one of my projects, we used it to send the visitor events (call dashboard, view quotes, etc.) as an event to Salesforce within a self-developed ASP .NET Core customer portal and there notify the sales employee with chatter messages. This ensured that new events could be easily extended in the application and that no extension of the Salesforce API integration was required.
Furthermore, Platform Events can also easily announce external events from other systems in the Marketing Cloud.
In this article, I like to share a basic implementation of how you can send platform events using .NET (C#)
Prerequisites in Salesforce
To send a message with the Platform Events bus, you have to create an event. Without diving too much into Salesforce, we will create a simple event and process automation that creates a chatter entry.
- Go to Setup-Integration-Platform events.
- Create a new event
- Configure event like below
4. Within the new creates the event, create a field
5. Navigate to Process Automation — Flows and create a new flow. Select “Platform Event-Triggered-Flow” as the new flow option
6. Choose the previously created event flow
7. Choose an action. In my case, I create a simple new lead
8. Save the flow and activate it. A new lead is now generated every time a new event will come in. Process Automation in Salesforce is quite powerful. Even as a non-experienced Salesforce developer, you can get much out of it without the need to dive too deep.
9. Create a connected app to allow communication with your .NET App. See https://help.salesforce.com/articleView?id=sf.connected_app_create.htm&type=5
Creating the .NET Application
After you have successfully configured Salesforce, it is time to generate platform events over the API.
For this purpose, I create a simple and plain ASP.NET Core Web Application.
- Configure the following appSettings with the values from your Salesforce environment.
The Username and Password are your own or API User credentials. You can generate a new token on your “My personal information” page under “Reset my security token.”
The ClientId and ClientSecret are displayed in your newly generated “Connected App” Settings on the API (Enable OAuth Settings) tab.
2. Create a class called SalesforceEventService.cs. You will find the full working copy here https://github.com/abierhaus/salesforce-platform-events-dotnetcore/blob/master/SalesforceEventService.cs:
The underlying flow works like this:
a) Login to Salesforce with the credentials from the configuration/ app settings
b) Build up a dynamic object
c) Send the serialized JSON object with the access token to the Salesforce event endpoint
d) Receive and return the platform event result
Add a default HomeController with the following code that calls the service we will create later.
3. Don’t forget to register the Controller in your Startup.cs
4. Finally, register the Service in your Startup.cs
4. Run the application. You will receive a page with the result from the object