2

This may be a silly question, but I've been surprised by Salesforce enough to potentially look foolish for confirmation! Apologies, the question itself was difficult to phrase.

Fundamentally, since a trigger can execute once and include multiple records, the structure of it includes a list of records triggering it (rather than one context being just for one record). One obvious example of this being the case (multiple records opposed to one) is a bulk update where, of course, it's many records in one update rather than individually.

What I'm wondering is if there are multiple records in one context just because records were updated close enough to each other, rather than in a "bulk" update, even if technically under different create/update calls.

For context, we have an integration that takes a large, bulk batch of records from our source system, and upserts to SFDC. The way our integration member explained the upsert is that, yes, it's a bulk batch, but technically each record is sent out individually (but, of course, this moves fast so they are all created virtually at the same time). I am wondering if this would mean each record has its own trigger execution (so the "list" is just one record") since it's not technically a batch, or if salesforce receives them quick enough and considers them all together (and putting them all in the same context)...(or if maybe it's just asynchronous but still considered "batch" by salesforce). Regardless...

I suppose the root of my question has to do with how Salesforce establishes if something is in bulk to include in one context for the trigger, vs. an individual execution. Is it just within a small time frame, or exclusively "batch" updates?

Again, apologies if this question is silly or confusing. Trying to establish the nature of our use case and how our trigger is processing it.

  • 4
    If they're being sent as individual API calls, then they would each be in their own execution context. That would generally be considered a very poor design though - passing multiple records to the APIs for insert or update, to conserve API calls by using 1 call per 200 (or fewer) records rather than an API call per record is one of, if not the primary reason for making triggers bulkified. – Thomas Taylor Jul 22 at 19:27
  • It's interesting to consider poor vs good design from two angles (the way I see it). Poor design in regards to API calls would be individual; poor design for a trigger would be bulk because you could reach your SOQL query limit so easily even if you only have one query. That's actually the root of a problem I'm trying to solve with an existing trigger I just encountered that's reaching its limit, and I'm trying to figure out if its due to a bulk job (it has 5 queries in it - that won't take long to break in bulk!!) I understand generally you shouldn't have queries in triggers, but "never"? – Natalie Paige Jul 22 at 19:41
  • 2
    @NataliePaige You just have to query smartly. Your SOQL queries should be able to bring up all the records you would possibly need. Never SOQL query within a for loop and use Map<Id, My_Custom_SObject__c> liberally and you should be fine – Brian Miller Jul 22 at 19:48
  • 2
    If a SOQL query is running more than once per trigger execution, the trigger is not properly bulkified. Getting close to the limit with well-written triggers usually means triggers cascading across several objects. – Thomas Taylor Jul 22 at 20:10
  • 1
    This is a great conversation to prime my continued investigation to my issue. Thank you both!! – Natalie Paige Jul 22 at 20:15
3

If the external system is doing a simple REST API call into Salesforce (albeit many of them very quickly together, even in parallel), Salesforce will treat each call as a separate Transaction, and triggers will run on each item separately.

In general, multiple records are only processed together if a singular action was called on multiple records at once (like mass edit on a list view, uploads from a CSV, or custom VF / Aura / LWC components that intentionally call an insert/update on multiple SObject records at once).

There are a number of API protocols though that will ensure that bulk records will come into Salesforce, and thereby the trigger will handle them in bulk appropriately. See the Bulk API trailhead and REST API Composite documentation for some guidance.

2

In addition to @BrianMiller answer,

If one or more publishers publish Platform Event Foo__e as follows

  • T(0) - publish 10 Foo__e
  • T(1) - publish 3 Foo__e
  • T(2) - publish 6 Foo__e

then the trigger that subscribes to Foo__e could see anywhere from:

1 to all 19 (and anything in between) Foo__e in a single transaction as SFDC Platform Event subscription code will "batch" Platform Events into a single transaction using rules that are opaque.

  • This is wonderful - I didn't know Platform Events operate in this fashion – Brian Miller Jul 22 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.