function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
splitthebills1.3947274848456084E12splitthebills1.3947274848456084E12 

Batches of 70 objects not being inserted by Database.executeBatch

Hi everyone,

We have written some code to create objects and insert via Database.executeBatch processes. This has been working fine for a long time, but recently, the inserts have stopped working in certain (unknown) circumstances.

I've created some test code to see if i can find the issue, and all i can find is that if i pass batches of around 30 to Database.executeBatch, they are inserted, but if i pass batches of around 70, these are not inserted. Either way, the batchable process emails us to say there were no errors.

The code that does all this is pretty straightforward and follows the SF rules to avoid running out of SOQL statements, plus the script only creates 2 batches.

Below is the test code that works, but doesn't result in payments being inserted.

Set<Id> invoiceIds = new Set<Id>{'a1SD0000000gzQWMAY', 'a1SD0000000gzQeMAI', 'a1SD0000000gzQiMAI', 'a1SD0000000gzQqMAI', 'a1SD0000000gzTeMAI', 'a1SD0000000gzTjMAI', 'a1SD0000000gzVYMAY', 'a1SD0000000gzVgMAI', 'a1SD0000000gzVhMAI', 'a1SD0000000gzVkMAI', 'a1SD0000000gzVmMAI', 'a1SD0000000gzVpMAI', 'a1SD0000000gzVwMAI', 'a1SD0000000gzVxMAI', 'a1SD0000000gzVzMAI', 'a1SD0000000gzW0MAI', 'a1SD0000000gzWDMAY', 'a1SD0000000gzWMMAY', 'a1SD0000000gzWOMAY', 'a1SD0000000gzWPMAY', 'a1SD0000000gzWSMAY', 'a1SD0000000gzWhMAI', 'a1SD0000000gzWkMAI', 'a1SD0000000gzWwMAI', 'a1SD0000000gzWxMAI', 'a1SD0000000gzX0MAI', 'a1SD0000000gzX1MAI', 'a1SD0000000gzX5MAI', 'a1SD0000000gzX6MAI', 'a1SD0000000gzX8MAI', 'a1SD0000000gzXQMAY', 'a1SD0000000gzXSMAY', 'a1SD0000000gzXTMAY', 'a1SD0000000gzXpMAI', 'a1SD0000000gzY5MAI', 'a1SD0000000gzYAMAY', 'a1SD0000000gzYIMAY', 'a1SD0000000gzYJMAY', 'a1SD0000000gzYKMAY', 'a1SD0000000gzYLMAY', 'a1SD0000000gzYPMAY', 'a1SD0000000gzYTMAY', 'a1SD0000000gzYdMAI', 'a1SD0000000gzZBMAY', 'a1SD0000000gzZLMAY', 'a1SD0000000gzZMMAY', 'a1SD0000000gzZNMAY', 'a1SD0000000gzZkMAI', 'a1SD0000000gzZpMAI', 'a1SD0000000gzZqMAI', 'a1SD0000000gza1MAA', 'a1SD0000000gzaLMAQ', 'a1SD0000000gzacMAA', 'a1SD0000000gzapMAA', 'a1SD0000000gzb2MAA', 'a1SD0000000gzb9MAA', 'a1SD0000000gzbIMAQ', 'a1SD0000000gzbJMAQ', 'a1SD0000000gzbKMAQ', 'a1SD0000000gzbgMAA', 'a1SD0000000gzdvMAA', 'a1SD0000000gzeVMAQ', 'a1SD0000000gzebMAA'};

fw1__Payment__c[] payments = new List<fw1__Payment__c>{};
date mydate = date.parse('04/11/2014');
Bank_Account__c BankAccount = [SELECT ID FROM Bank_Account__c WHERE Name = 'Bank Account'];

for(String invoiceId : invoiceIds) {
fw1__Invoice__c i = [SELECT ID, fw1__Account__c, fw1__Contact__r.Id, fw1__Contact__r.Sort_Code__c, fw1__Contact__r.Account_Number__c,  fw1__Contact__r.Bank_Account_Holder__c, fw1__Total_Paid_Amount__c FROM fw1__Invoice__c WHERE ID = :invoiceId];
fw1__Payment__c p = new fw1__Payment__c();
p.fw1__Payment_Method__c = 'Other';
p.fw1__Invoice__c = invoiceId;
p.fw1__Payment_Date__c = mydate;
p.Name = 'Payment for INV-' + invoiceId;
p.fw1__Contact__c = i.fw1__Contact__r.Id;
p.fw1__Amount__c = i.fw1__Total_Paid_Amount__c;
p.fw1__Bank_Aba_Code__c = i.fw1__Contact__r.Sort_Code__c;
p.fw1__Bank_Account_Number__c = i.fw1__Contact__r.Account_Number__c;
p.fw1__Name_On_Account__c = i.fw1__Contact__r.Bank_Account_Holder__c;
p.Bank_Account__c = BankAccount.Id;
p.fw1__Account__c = i.fw1__Account__c;
payments.add(p);
}
PaymentInserter pi = new PaymentInserter(payments);
ID paymentInserterBatchProcessId = Database.executeBatch(pi);

The PaymentInserter looks like this:


global class PaymentInserter implements Database.Batchable<sObject>{
global fw1__Payment__c[] Payments;
global String error {get; set;}

global PaymentInserter(fw1__Payment__c[] p){
  Payments=p;
}

global fw1__Payment__c[] start(Database.BatchableContext info){
  return Payments;
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
        try {
            insert scope;
        } catch (System.CalloutException e){
            error = e.getStackTraceString();
        }     
}

global void finish(Database.BatchableContext BC){
        String body = 'Batch Process has completed. Bacth Job Id was ' + BC.getJobId() + '\n\n';
        if(error != '') {
            body += 'Error = \n\n' + error;
        }
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setToAddresses(new String[] {'finance@splitthebills.co.uk'});
  mail.setReplyTo('finance@splitthebills.co.uk');
  mail.setSenderDisplayName('Salesforce Apex Batch Process');
  mail.setSubject('Salesforce Apex Batch to Instert Payments has Completed');
        mail.setPlainTextBody(body);
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

}
Thanks

Andy
MagulanDuraipandianMagulanDuraipandian
Try like this

Replace "fw1__Payment__c[]" with List<(w1__Payment__c>

If this solves your problem, kindly mark it as the best answer.

Regards,
Magulan
http://www.infallibletechie.com
splitthebills1.3947274848456084E12splitthebills1.3947274848456084E12
Hi Magu

fw1__Payment__c[] is synonymous with new List<fw1__Payment__c> - i.e. both create a list of fw1__Payment__c.

Thanks

Andy