[SalesForce] How to publish/consume bulkified Platform Events

Use case:

  • External system wants to publish Platform Events to Salesforce.
  • Salesforce wants to consume the Platform Events in a bulkified manner

The Platform Events doc offers this REST call:

/services/data/v42.0/sobjects/Low_Ink__e/ with POST body

{
   "Printer_Model__c" : "XZO-5"
}

But this won't be bulkified. The incoming Apex trigger (or Process Builder) will receive events 1×1.

How to do this in a bulkified manner?

Best Answer

In V42.0, SFDC added a new REST composite collection resource that is bulkified and works with Platform Events!

/services/data/v42.0/composite/sobjects

{
   "allOrNone" : false,
   "records" : [{
      "attributes" : {"type" : "Low_Ink__e"},
      "Printer_Model__c" : "XZ0-5"
   }, {
      "attributes" : {"type" : "Low_Ink__e"},
      "Printer_Model__c" : "DSW-892a"
   }]
}

I had a simple trigger that consumed this Platform Event. Examining the debug log after submitting the above through Workbench yielded:

17:25:26.0 |USER_INFO|[EXTERNAL]|00536000000rMUG|autoproc@00dm0000000dalrea0|Pacific Standard Time|GMT-07:00
17:25:26.0 |EXECUTION_STARTED
17:25:26.0 |CODE_UNIT_STARTED|[EXTERNAL]|01q36000001pI4C|LowInkTrigger on Low_Ink trigger event AfterInsert
17:25:26.0 |USER_DEBUG|[8]|INFO|LowInkTrigger afterInsert entered for 2records

As you can see, one trigger fired with 2 records. Just as you would want.

Note the allOrNone property that allows for partial success.

  • POST is supported (new recs)
  • PATCH is supported (updates - although n/a for Platform Events)
  • DELETE is supported - also n/a for Platform Events

Note also that the running user is Automated Process, not the user you logged into Workbench with. As I did this in a fresh sandbox, I needed to be aware that the trigger needed to be recompiled.

Related Topic