Insights

10 Salesforce Platform Events Best Practices

Platform events are a powerful tool in Salesforce, but there are some best practices to keep in mind when using them.

Salesforce Platform Events are a powerful tool that can be used to build event-driven architectures. They allow you to asynchronously communicate between Salesforce orgs, external systems, and Lightning Components.

In this article, we will discuss 10 best practices for using Salesforce Platform Events. By following these best practices, you can build more scalable and maintainable event-driven architectures.

1. Use Platform Events to Integrate Salesforce with External Systems

Salesforce Platform Events are designed to be highly scalable and can handle a large number of events per second. This makes them ideal for integrating Salesforce with external systems that generate a lot of data, such as social media platforms or e-commerce websites.

Additionally, platform events are easy to set up and use. You don’t need to write any code to create or publish platform events, and there are many out-of-the-box integrations available.

Finally, platform events are flexible and can be used to integrate Salesforce with a variety of external systems. For example, you could use platform events to trigger a flow when a new product is added to an e-commerce website, or to update a contact record when a customer tweets about your company.

2. Create a Custom Event Type for Each Integration Scenario

When an integration scenario changes, you want to be able to update the event type without affecting other integrations. For example, if you’re integrating with a third-party system and the data format changes, you can update just the event type for that integration, without affecting other integrations.

Creating a custom event type for each integration also allows you to control which fields are sent to each integration. This is important because you might not want to send all of the fields in an event to every integration.

Finally, having a separate event type for each integration makes it easier to troubleshoot problems. If there’s a problem with one integration, you can easily disable or delete that event type without affecting other integrations.

3. Use the Same Namespace and Name for Your Event Type and Apex Class

When you use the same namespace and name, it makes it much easier to map your event type to your Apex class. This is because the Salesforce Platform Events system uses the fully qualified name of your Apex class when it looks up the class to execute your code.

If you don’t use the same namespace and name, you’ll have to manually specify the namespace and name of your Apex class when you create your event type, and this can lead to errors if you mistype the namespace or name.

It’s also a good idea to use the same namespace and name for your event type and Apex class because it makes it easier to read and understand your code. When you see the same namespace and name in both places, it’s clear that they’re related.

4. Use Standard Field Names in Your Event Types

When an event is triggered, Salesforce creates a record in the EventBusArchive object. This object contains all of the information about the event, including the standard fields and any custom fields you’ve added to your event type.

The problem is that if you use a custom field name in your event type, that name will be used in the EventBusArchive object as well. This can make it difficult to query or report on your platform events later on.

To avoid this issue, always use standard field names in your event types. That way, you’ll be able to easily query and report on your platform events later on.

5. Use the After Insert Trigger on Your Event Object

When a platform event is published, it’s stored in Salesforce as an EventBusEvent record. If you don’t have an After Insert trigger on your event object, there’s no way to know that the event has been published and no way to take action on it.

The After Insert trigger gives you the ability to do things like call an Apex method or send a notification to another system. Without it, you’re blind to when events are happening.

6. Use the Publish Method to Send Platform Events

The Publish Method is the recommended way to send Platform Events because it’s more efficient than the other methods. When you use the Publish Method, Salesforce creates a new Platform Event record and then asynchronously sends the event to all subscribers. This means that your code doesn’t have to wait for the event to be sent before continuing, which can improve performance.

It’s also worth noting that the Publish Method is the only way to guarantee that an event will be delivered to all subscribers. If you use the other methods (such as the create method), there’s a chance that the event could be lost if there are any errors.

7. Use the Subscribe Method to Receive Platform Events

The Subscribe method allows you to specify the name of the event, the duration for which you want to receive events (up to 24 hours), and the number of events you want to receive (up to 10,000). You can also specify a callback URL, which is the URL that Salesforce will call when an event is published.

This is a much more efficient way to receive platform events than polling the Salesforce API, and it’s also more reliable because you’re not reliant on the Salesforce API availability.

8. Use the ReplayId Parameter to Replay Old Events

When a new subscriber subscribes to a platform event, the first thing that Salesforce does is check to see if there are any old events that have not been delivered yet. If there are, Salesforce will deliver those events before it starts delivering new events.

The ReplayId Parameter is used to tell Salesforce which event is the oldest one that the subscriber has received. This way, Salesforce knows which events to replay and which ones to skip.

If you do not use the ReplayId Parameter, then Salesforce will replay all of the events from the beginning, which can cause problems if your subscriber has already processed some of those events.

To use the ReplayId Parameter, simply add it to the list of parameters when subscribing to the platform event. The value of the ReplayId Parameter should be the ID of the last event that the subscriber processed.

9. Use the Replay Option to Replay All Past Events

When you enable the Replay Option, all past events are automatically replayed in your org. This is useful for a number of reasons.

For one, it allows you to test your event-based triggers and processes without having to manually generate new events. This can save you a lot of time and effort, especially if you have a large number of events.

It also means that you can be confident that your event-based triggers and processes will work correctly when new events are generated in the future. If there are any issues with your event-based triggers or processes, they will be uncovered during the replay of past events.

Finally, using the Replay Option can help you troubleshoot issues that may have occurred in the past. If you suspect that an issue was caused by an event, you can use the Replay Option to see if the issue occurs again when the event is replayed.

10. Use the ReplayFromDateTime Parameter to Replay Newer Events

When subscribing to a Salesforce Platform Event, you have the option to provide a ReplayFromDateTime parameter. This parameter tells Salesforce which date and time to start replaying events from.

If you don’t provide a ReplayFromDateTime parameter, Salesforce will automatically replay all events that have ever been published for that event, which could be a lot of events. This could cause problems if your subscriber is not prepared to handle a large number of events at once.

By providing a ReplayFromDateTime parameter, you can control how many events are replayed, and you can ensure that your subscriber is only receiving events that it can handle.

Previous

10 Nutanix VMware Best Practices

Back to Insights
Next

10 SQL Alerts Best Practices