Last Updated on May 12, 2023 by Rakesh Gupta
Big Idea or Enduring Question:
- How to configure the user and batch size for Platform Event Trigger?
After reading this blog, you’ll be able to:
- Understand the batch size for platform event trigger
- Override the default running user for platform event trigger
- Create the PlatformEventSubscriberConfig via REST API
- and much more
Business Use case
Jestilla Zetkin is working as a Salesforce Architect at Gurukul On Cloud (GoC). Recently she faced challenges while working with platform events, mainly governer limits. She wants to learn how to configure the batch size for the platform event trigger. In this exercise, we will set up the batch size for the following:
- Apex Trigger
- LeadGenerationEventTrigger on Lead_Generation__e
- Batch Size: 50
- Running User: Integration User
What is Platform Events?
Event-driven architectures have become very popular in recent years, and for a good reason. It is based on the fire-and-forget pattern. Firing an event and letting each system handle business logic allows you to keep unrelated systems decoupled and can help simplify the architecture.
Platform events enable you to deliver secure, scalable, and customizable event notifications within Salesforce or from external sources. Apps can publish and subscribe to Platform Events on the Salesforce platform using Apex, Flow, or in an external system using CometD. If you want to learn more about Platform Event, check out the Trailhead Module.
What is the Default Batch Size for Platform Event Trigger?
By default, the platform event trigger runs as the Automated Process system user with a batch size of 2,000 event messages. Sometimes it is required to change the user and batch size to bypass governer limits or other challenges.
Salesforce allows you to override the default running user and batch size of a platform event Apex trigger with PlatformEventSubscriberConfig in Tooling API or Metadata API to configure the trigger.
You can specify any active user in the Salesforce org. The trigger runs in a system context with privileges to access all records regardless of the user’s object and field-level permissions. You can specify a custom batch size from 1 through 2,000. The batch size is the maximum number of event messages that can be sent to a trigger in one execution.
Understand PlatformEventSubscriberConfig Object
Let’s take a pause here, familiarize yourself with the PlatformEventSubscriberConfig Tooling API object in Salesforce. It represents configuration settings for a platform event Apex trigger, including the batch size and the trigger’s running user.
|BatchSize||A custom batch size, from 1 through 2,000, for the platform event Apex trigger. The batch size corresponds to the maximum number of event messages that can be sent to a trigger in one execution. The default batch size is 2,000 for platform event triggers.|
|DeveloperName||The unique name for the PlatformEventSubscriberConfig object.|
|MasterLabel||Label for PlatformEventSubscriberConfig. In the UI, this field is Platform Event Subscriber Configuration.|
|PlatformEventConsumerId||The ID of the platform event Apex trigger to configure.|
|UserId||The ID of the user that the platform event Apex trigger runs as. By default, the platform event trigger runs as the Automated Process entity. Setting the running user to a specific user has these benefits:
PlatformEventSubscriberConfig components have the suffix .platformEventSubscriberConfig and are stored in the PlatformEventSubscriberConfigs folder.
This PlatformEventSubscriberConfig component has the label LeadGenerationEventTriggerConfig. It contains the configuration of a platform event trigger, LeadGenerationTrigger, and specifies the batch size and user.
<?xml version="1.0" encoding="UTF-8"?> <PlatformEventSubscriberConfig xmlns="http://soap.sforce.com/2006/04/metadata"> <platformEventConsumer>LeadGenerationTrigger</platformEventConsumer> <batchSize>50</batchSize> <masterLabel>LeadGenerationEventTriggerConfig</masterLabel> <user>firstname.lastname@example.org</user> <isProtected>false</isProtected> </PlatformEventSubscriberConfig>
Guided Practice (We-do):
To add a configuration, perform a POST request as mentioned below:
- Open the Workbench and log in with your Trailhead playground username and password.
- Make sure that you’ve selected the Production for Environment option.
- Navigate to Utilities tab and select the REST Explorer option.
- Select the following options:
- Choose an HTTP method to perform on the REST API service URI below: POST
- URL: /services/data/v58.0/tooling/sobjects/PlatformEventSubscriberConfig
- Body: Provide the values in the request body. This example request configures an existing trigger with the batch size of 50 and specifies the ID of a running user.
- 01qB0000000Zcug, Apex trigger on Lead_Generation__e
- 005B00000015gt8, is user Id of Integration User
- Click Execute.
You can query retrieve the configurations in your org with SOQL. If querying from the Developer Console Query Editor, ensure you select Use Tooling API. This example query retrieves all configurations set up in your Salesforce org.
I want to hear from you!
What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.