I'm trying to create bulk contacts on a single account. I followed the batch apex approach since I will be inserting over 1000 contacts on a single account at once. I triggered the batch apex from after insert of a staging object and perform the operations. Since the batches will be running in parallel mode only one batch is getting executed and the rest of the 4 batches in this case(1000 records - 5 batches) are unable to insert the records with this error message. The Salesforce documentation specifies in case of bulk API we need to set the operations in series mode but I am unable to set it for batch apex. Is there any way that I can do to insert these contacts in series in batch apex context.
I set the batch size going into the trigger to 1000 just to check if it works but it didn't help.
Pseudo code
trigger on staging object (after insert)
{
database.execute(new batchapex(trigger.new));
}
batch class execute method
{
list<contact> clist = new list<contact>();
for(staging object s: scope)
{
contact c = new contact();
c.email = s.email;
c.accountid=s.accountid; clist.add(c);
}
insert clist;
}
I believe apex is splitting the records of 200 on calling batch apex. And 4 instances of batch apex are getting created if I try to insert 800 records. If the batch apex does not run in parallel mode why do 4 instances get created?