Whenever records are inserted in Contacts(usually inserted in 1000's), we have a batch job that checks related customer account in SFDC and populates the contact.accountId field and couple other fields. This so far worked fine. Now, we have a scenario where 1 million contact records (approximately 5000 batches) should be inserted into SFDC using Bulk API, and even with flex queue enabled, I am running out of available batches.
Trigger:
trigger conTrigger on Contact (after insert) {
ContactHandler.managecontacts(trigger.new);
}
ContactHandler.class
public static void managecontacts(List<Contact> acList) {
Database.executeBatch(new UpdateContactsBatch(acList));
}
UpdateContactsBatch.class
public class UpdateContactsBatch implements Database.Batchable<sObject>, Database.Stateful {
List<Contact> acList = new List<Contact>();
public UpdateContactsBatch (List<Contact> acList) {
this.acList = acList;
}
public Iterable<sObject> start(Database.BatchableContext BC) {
return [select id, accountId from contact where Id In: acList];
}
public void execute(Database.BatchableContext BC, List<AccountContacts__c> scope) {
ContactTriggerHandler.managecontacts_execute(scope);
}
public void finish(Database.BatchableContext BC) { }
}
One option I have is to turn off the trigger during this mass load and manually run the batch from anonymous window after inserting all contacts. (Tested this scenario and this is working fine in PreProd environment). In Production this requires 2 times deployment (one for activating trigger and one for deactivating. Donot prefer having custom settings to control trigger behaviour)
But would like to know if there is any other option to handle this scenario.
Problem: I changed the batch size to 5000 in my dataloader which is currently set to "Use Bulk API" but still seems like my trigger is getting 200 records at a time and for each load of records from trigger.new, a batch job will be queued