I have some custom apex that I am invoking from a flow. The flow is sending a list of lists. The sub-list is a group of 5 sObjects with all the necessary fields to create new records. So I have a parent list where each item in the list is a list of 5 custom sObjects. I want to perform a DML operation to insert these records in batches of 5 records. I do NOT want salesforce to group these DML calls into the standard bulkification batch size of 200, because I have other processes running on insert for this custom object that will fail for 200 records...I want to initiate this triggered process only on 5 records at a time, and independently for each of 5 records. Here is my code:
public class NewAffiliations {
public class FlowInputWrapper {
@InvocableVariable(required=true)
public List<NU__Affiliation__c> affiliations;
}
@InvocableMethod(label='New Affiliations')
public static void createAffiliationDetails(List<FlowInputWrapper> inputList) {
Integer iterater = 0;
for(FlowInputWrapper batches : inputList) {
Savepoint sp = Database.setSavepoint();
Database.insert(batches.affiliations);
System.debug('Iterater' + iterater);
}
}
}
As you can see, I am performing the DML insert in a loop. inputList is the parent list, and batches represents each child list that contains 5 records. The desired outcome is that after each insert statement, the governor limits would reset so as to make all my triggered processes successful (note that the triggered processes are managed code from some apps we have installed, and therefore I do not have access to modify / fix the triggered code).
Some research indicated that the setSavepoint would cause a new transaction, therefore resetting governor limits. Is this true? Because I am getting a soql limit exceeded (it seems that the soql statements are adding up for each iteration).
How can I force a new transaction and reset of governor limits? How can I prevent salesforce from batching into chunks of 200, so that I can do 5 records like I want?