Whenever we try to perform any operation on some records then the trigger perform its action in the batches of 200 records. Am I correct here?

Also consider that first 200 records have got inserted and the next batch should check for the duplicate records in the first batch also. So will this happen in the trigger?

Like consider the combination of two fields A & B makes a unique record for the object. We have provided this condition in our trigger to check. But will the trigger check this in the first 200 batch or it will just check in the already present records?

This may seem stupid but I want to be clear on this thing. As far as I know trigger should check for the duplicates in the first batch too.

  • 3
    For that specific use case, you're better off creating a composite key field and setting it to unique. i.e. Let the database do your dupe checking.
    – Nick Cook
    Sep 29 '16 at 7:21
  • Actually there is already a trigger in place for this without composite key. There are many different multiple complex actions happening on that object. I do not want to make lot of changes in the code. The trigger is already checking for the duplicates based on 2 fields but my client doubts that it is not checking for duplicates in the batches we load in the same transaction.
    – Ruchi
    Sep 29 '16 at 7:41
  • Yes, you are correct. Although SF abstracts away the details so that you don't generally notice, it does always break down trigger processing to batches of 200. I think Nick Cook has it right that the best approach would be to use a composite field.
    – Aidan
    Sep 29 '16 at 9:08
  • As you are handling duplicate in trigger so it totally depends on your trigger code. How can we tell you about that? Sep 29 '16 at 9:47

Trigger batch duplicate checking involves two types of checks

  1. Testing for duplicates extrabatch - that is, are any of the records in the batch present in records already in the database (excluding, of course, the same record on an update)
  2. Testing for duplicates intrabatch - that is, are any of the records in the batch duplicates of each other

SFDC Duplicate Management will handle case #1 for free but not case #2. Thus, you could simplify the trigger to do only #2 and let SFDC Dup Mgt handle #1

The trigger will only run on the batch size (200) even if the caller did an insert statement of 1000 rows (hence, 5 batches - but still one transaction). Data Loader automatically creates separate transactions for each 3200 records encountered

  • 1
    For clarity, each group of 200 records will have governor limits reset between them and the records are committed between the groups correct. So if you are performing a query in the trigger the second group of 200 will return the results of the first group. Or am I completely slaughtering the basics?
    – Eric
    Sep 30 '16 at 3:15
  • So can we say that the trigger does not commit the records unless the full transaction is complete ? Or does it commits records in the batch of 200 ? If it commits in batches then the first 200 records will be present in the database for the next batch to check for duplicates is what I assume.
    – Ruchi
    Sep 30 '16 at 6:46
  • 1
    @Ruchi -- commits occur on the transaction. The scope of the transaction is defined by the caller. If Data Loader, the scope is the batch size (default 200), if Apex DML such as in a VF controller or web service, the scope is the controller's action method or web service's REST handler and the # of records in the transaction is the # of records in the DML statement
    – cropredy
    Sep 30 '16 at 16:52
  • Hi, Just to clarify , we have a trigger in before insert for Auto number generation and while doing a data loading 300 records in batch size of 50. The first 50 records get saved but the second 50 gets an error stating duplicate value. It seems the SOQL which fetch the latest auto number didn't consider the 50 processed record . Just wanted to know in case of data loader of multiple batch , is the commit happens after the entire set is over i.e 300 records and then commit???
    – Quddus
    Nov 4 '19 at 9:28
  • @Quddus - 300 Data Loader records in batches of 50 results in 6 transactions and hence 6 commits
    – cropredy
    Nov 4 '19 at 18:35

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.