0

we have a trigger in before insert for Auto number generation and while doing a data loading of 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 batches , is the commit happens to DB after the entire transaction set is over (i.e 300 records) and then commit???

I have seen lot of docs where it says that it comments in batches but I am not sure as I am seeing different results.

Also after doing lot of testing , I find that sometimes the second batch got successful and on the third batch it gives the duplicate value.

On checking the SOQL number of rows returned it seems the query doesn't returned the records which got inserted in the earlier batch.

Is it because of parallel inserting in data loader as I have not selected the serial mode insert.

Any idea?

4

Yes, it definitely is. When you use the Bulk API and do not check the Enable Serial Mode for Bulk API, Salesforce will process batches simultaneously which means that multiple batches will execute in parallel. Because of this, there could easily be two batches trying to get inserted with the same autonumber values because at the time they queried the database to get the latest autonumber values, they both got the same last number and built their autonumbers based off of that. The batch that gets inserted first will pass while the other one will ofcourse fail. Due to the nature of your data upload, I would recommend Enabling Serial Mode for Bulk API to avoid this race condition.

| improve this answer | |
  • Yes makes sense now . THanks – Quddus Nov 4 '19 at 20:02

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.