Skip to main content
Join the Agentforce Hackathon on Nov. 18-19 to compete for a $20,000 Grand Prize. Sign up now. Terms apply.

Adjust the Platform Event Trigger Batch Size

Learning Objectives

After completing this unit, you’ll be able to:

  • List the pros and cons of a small and large batch size of a platform event trigger.
  • Configure the trigger batch size with PlatformEventSubscriberConfig using Tooling API.

Platform Event Trigger Batch Size

By default, the trigger has a maximum batch size of 2,000 event messages that can be delivered for each trigger invocation. This batch size is larger than the batch size of regular Apex object triggers, which is 200. The larger batch size enables platform event triggers to scale and process large volumes of events faster. One side effect of the larger batch size is that it can increase the likelihood of hitting Apex governor limits, especially if the trigger performs DML or SOQL operations. In this case, setting a smaller batch size can help you avoid hitting Apex governor limits.

Even though you can use setResumeCheckpoint to have the trigger resume after limit exceptions, limiting the trigger batch size is another way to deal with limit exceptions by trying to avoid them.

Which Trigger Batch Size to Choose?

The trigger batch size depends on the event publishing rate. Reducing the trigger batch size can slow down event processing. If the trigger receives a large volume of events, a small batch size can cause event processing to get behind and the queue of incoming events to become large.

Events are stored in the event bus for 72 hours, so if a trigger runs slowly due to small batch sizes, the stored events may no longer be available in some cases. We don’t recommend you set batch sizes that are too small or are equal to 1. When you decide which batch size to set, take into consideration your event volume and event processing speed in the trigger. If your trigger hits Apex governor limits often, it can help to reduce the trigger batch size to a value that allows the trigger to avoid hitting limits.

Note

Note

In addition to setting the batch size, you can specify a user to override the default Automated Process running user. One of the benefits of using your own user is that records are created by this user, the OwnerId field is autopopulated to this user, and the debug logs are created by this user. We will skip doing that for the purpose of this module.

Set the Batch Size

To override the default batch size, use PlatformEventSubscriberConfig in Tooling API or Metadata API. In this example, you use Postman to run a Tooling API request and set the batch size to 100 for the OrderEventTrigger.

Connect Your Trailhead Playground Org with Postman

  1. Make sure you are logged in to your Trailhead Playground org.
  2. After you set up Postman by completing the Quick Start: Connect Postman to Salesforce project, open the Postman app, select a Workspace, and navigate to your fork of the Salesforce API collection.
  3. On the Authorization tab, scroll to the bottom and click Get New Access Token.
  4. Click Allow.
  5. In the Manage Access Tokens dialog, copy the instance URL to your clipboard.
  6. Click Use Token.
  7. On the Variables tab, in the _endpoint row, in the CURRENT VALUE column, paste the instance URL that you just copied, then click Save. You may need to close the documentation pane to see the Save button.
  8. Test that your connection is working.
  9. In Collections, select your fork of the Salesforce Platform APIs collection.
  10. Select REST to expand the REST APIs.
  11. Select GET Limits, then click Send.
  12. In the response window, the Status field should show as Status: 200 OK. If it does not, repeat the steps to get a new token.

Make a Tooling API Call to Configure the Trigger Batch Size

Before you can create the trigger configuration, you must get the ID of the trigger. You can get the trigger ID by performing a SOQL query.

  • In your fork of the Salesforce Platform APIs collection, expand Tooling, and click Tooling Query.
  • Click the Params tab.
  • Get the ID of the OrderShippingEventTrigger you created in the hands-on challenge of the previous unit. Under Query Params, for q, enter this query: SELECT Id FROM ApexTrigger WHERE Name='OrderShippingEventTrigger'.
  • Click Send.
  • The response contains the trigger ID. Copy the ID to use in the next step.
{
 "size": 1,
 "totalSize": 1,
 "done": true,
 "queryLocator": null,
 "entityTypeName": "ApexTrigger",
 "records": [
   {
     "attributes": {
       "type": "ApexTrigger",
       "url": "/services/data/v60.0/tooling/sobjects/ApexTrigger/01q8b0000002wXDAAY"
     },
     "Id": "01q8b0000002wXDAAY"
   }
 ]
}

Make a Tooling API Call to Configure the Trigger Batch Size

  • In your fork of the Salesforce Platform APIs collection, expand Tooling, and click Post Tooling SObject.
  • Click the Params tab.
  • Under Path Variables, for SOBJECT_API_NAME, enter PlatformEventSubscriberConfig.
  • Click Body and ensure that raw and JSON options are selected.
  • Replace the body with this JSON body. Replace the <Apex_Trigger_ID> placeholder with the trigger ID you copied earlier.
{
    "BatchSize": "100",
    "DeveloperName":"OrderShippingEventTriggerConfig",
    "MasterLabel":"OrderShippingEventTriggerConfig",
    "PlatformEventConsumerId": "<Apex_Trigger_ID>"
}

6. Click Send.

7. Verify you get a 201 Created response. The response looks similar to this example.

{
  "id": "3JK8b000000blKRGAY",
  "success": true,
  "errors": [],
  "warnings": [],
  "infos": []
}

Now your trigger is set to process batch sizes of 100 events at a time.

For more information, see PlatformEventSubscriberConfig in the Tooling API Developer Guide or PlatformEventSubscriberConfig in the Metadata API Developer Guide.

Note

Note

If a trigger is running and subscribed to a platform event, new configuration settings take effect after you suspend and resume the trigger from the platform event detail page, in the Subscriptions related list. For more information, see View and Manage an Event’s Subscribers on the Platform Event’s Detail Page in the Platform Event Developer Guide.

To learn more, see Configure the User and Batch Size for Your Platform Event Trigger with PlatformEventSubscriberConfig in the Platform Events Developer Guide.

Congratulations! You've learned a bundle of techniques for debugging platform event publishing and subscription. You also learned how to write robust platform event triggers and how to control the trigger batch size. You're now equipped with the tools and knowledge to write event-driven apps in Apex with confidence!

Resources

Salesforce ヘルプで Trailhead のフィードバックを共有してください。

Trailhead についての感想をお聞かせください。[Salesforce ヘルプ] サイトから新しいフィードバックフォームにいつでもアクセスできるようになりました。

詳細はこちら フィードバックの共有に進む