3

Imagine I have 5 users working in a training on the same Sobject type (Parent__c). They all have a page where they can create 50 child objects (Child__c) with the single click of a button.

Assume there exits a Trigger for Child__c. We all now Triggers work bulkified for up to 200 records.

How does Salesforce share the work between trigger instance

  • 5 trigger instance for 50 records each are fired
  • 3 trigger instance for 200, 200, 50 records are fired

This is relevant to me because my trigger fires a Batch job to prevent Limit execptions from subsequent triggers. If each user gets his own instance (Case 1) I might run into the 5 Batches in Parallel Limit. But even in Case 2 if more than 1000 records are saved in parallel I will hit my limit.

1 Answer 1

7

I am positive (based on my research and the feedback in the comments below) that these each are session based. As in, if 5 users save 50 records each, 5 triggers of 50 records each are run. My reasoning behind this thinking is:

  • Triggers fire in bulk and have their own Order of Execution
  • This Order of Execution includes multiple validation steps.
  • If Salesforce bulked up these records it would cause massive problems with validation. Let me show you a scenario:
    • You have 5 users. User A, B, C, D, and E.
    • Users A, B, and C all have valid data.
    • Users D and E have invalid data.
    • In a bulk processing scenario, all 5 would get errors returned for invalid data because of the transaction management of Triggers.
    • In a non-bulk processing scenario, User A, B, and C would process successfully. Users D and E would each receive separate errors on what is wrong in their record set.

I did a bit of research and I could not find any specific Salesforce documentation explaining this, but like I said, I am positive this is how it works (and in my opinion it makes sense to work like this because of transactions).

7
  • Sound like a good argumentation. Thanks Jesse. Maybe someone from Salesforce can confirm this officially ;) Mar 26, 2013 at 14:38
  • Another thing to watch out for is if you are processing a single list from a single User, from Apex, with more than 200 records SF will batch it up as lists of 200 to the trigger, so update listOfSize1001 will call the trigger 6 times (200 x 5 + 1 x 1). Mar 26, 2013 at 15:38
  • 2
    Consider what UserInfo.getUserId() returns in a trigger if multiple users data is processed in the same context.
    – Acuariano
    Mar 26, 2013 at 19:50
  • Acuariano: Can you elaborate a bit what you mean? Mar 27, 2013 at 9:50
  • 3
    Each API call/apex invocation has it's own transaction on the underlying database and doesn't share any part of this state with other API calls/apex invocations. Multiple users and multiple actions will never share state outside of 1) viewstate and 2) the database. Mar 28, 2013 at 4:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .