7

Is the whole data set processed within single trigger execution context? Or each 200 records has seperate execution context within trigger? Lets say we have 400 records and they are updated and processed by trigger in chunks of 200 records(chunk1 and chunk2). do these chunks share the same execution context(common limits) or each has seperate execution context:chunk1 has execution context 1(first set of limits) and chuck 2 has execution context 2(another set of limits)?

I am assking with regard to future call and 2200 records that are processed with trigger. Is it the case that we can hit the limit in 10 future calls(2200:200=11 > 10)?

2 Answers 2

10

There is no such thing as passing to a trigger; a trigger fires on a DML event. However, DML operations operate on a transaction chunk of 200 records at a time. So, let's say for example, you have a Visualforce page that edits 200 records, and an update trigger on that object, your trigger would operate on all 200 records at once, in one chunk. Another example is updating records through the API. Let's say your hit the SOAP API with an update to 200 records. The standard SOAP API allows you to specify the size of the batch but let's say your chunk size was 200, and you had an update trigger on that object, the trigger could be acting on a maximum of 200 records in one transaction chunk.

You trigger should be made bulk safe to be able to handle up to 200 records. When your trigger exercises a callout, you need to make sure you're handling up to 200 records and not going over the callout per transaction limit in your code.

Quote from the documentation:

For Apex saved using Salesforce.com API version 20.0 or earlier, if an API call causes a trigger to fire, the chunk of 200 records to process is further split into chunks of 100 records. For Apex saved using Salesforce.com API version 21.0 and later, no further splits of API chunks occur. Note that static variable values are reset between API batches, but governor limits are not. Do not use static variables to track state information between API batches.

3

It seems that all chunks share the same limits.

I ran the test: insert 2200 records with future call in trigger

trigger testtrigger on Account (after insert) {
    testtrigger.handle();
}
public class testtrigger {
    public static void handle() {handlefuture()}
    @future
    public static void handlefuture() {}
}

and got the following error: EXCEPTION_THROWN [13]|System.LimitException: Too many future calls: 11

4
  • 1
    That is correct. Also, static variables in classes are not reset on the different chunks due to being the same context, so watch out for that. Commented Apr 22, 2014 at 23:59
  • @PeterKnolle I'm confused. The documentation seems to say "static variables are reset between API batches", but you're cautioning that they are NOT reset. To be honest, I think I'd rather trust you than the Salesforce docs, but I wanted to reach out to double-check first! Can you confirm that the docs are wrong, or am I just not getting something? Thanks, Peter! Commented May 3, 2014 at 23:03
  • 2
    @VivekM.Chawla - The docs and I are both correct. ;) In between API calls the static variables are reset. I may not have been clear in my comment, but I was referring to if you update more than 200 records in one DML statement from Apex, the records are chunked to the trigger in lists of 200 and the static variables are not reset in between those chunks. Take a look at the code and discussion in this question. That's what I was referring to. Commented May 4, 2014 at 0:22
  • @PeterKnolle Thanks for getting back to me so quickly, and for expertly clarifying things! I'll take a look at the code and discussion you suggest, and go from there. Thank you! :-) Commented May 4, 2014 at 0:48

You must log in to answer this question.

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