Skip to main content
We have 3 Batch Apex classes that are setup to run once a month

The basic function of these is to get records from below objects based on certain field value and update that field value

1. Account: get records where Active_CA__c=true, and change it to false

2. Account Payment: get records where Closed__c=false and change it to true

3. Collection: get records where CA_Closed__c=false and change it to true

The classes were thoroughly tested in sandbox and worked as expected when they were called from a flow

A scheduled flow ran the code at 3AM on 6th May 2021. All three classes ran without any failures. In reality the fields on only the Account records were updated and fields on Account Payment and Collection records were not updated.

We called these classes from another flow and ran the flow manually at 5:53AM. This time the remaining two classes worked as expected and the field values were updated.

1. Account: 0 batches executed as the previous execution updated the field values

2. Account Payment: same number of batches executed as before, field values updated

3. Collection: same number of batches executed as before, field values updated

Is it possible to understand why the field values were not updated during the first execution but everything worked 2nd time?

Here are the classes:

1. 

global class ClearAccountActiveCA implements Database.Batchable<sObject> {

  @InvocableMethod(label='Clear Active CA on Account' description='This will clear Active CA and Next CA Close Date from Account at the end of the cycle')

  public static void executeBatchMethod(List<String> lst) {Database.executeBatch(new ClearAccountActiveCA(), 100);}

  

    global Database.QueryLocator start(Database.BatchableContext BC)

    {

           

        return Database.getQueryLocator('SELECT Id, Active_CA__c, Next_CA_Close_Date__c FROM Account WHERE Active_CA__c=true OR Next_CA_Close_Date__c!=null');

    }

     

    global void execute(Database.BatchableContext BC, List<Account> accList) {      

        for(Account acc : accList) {                     

           acc.Active_CA__c = false;

           acc.Next_CA_Close_Date__c = null;   

        }

        

        try {

            update accList;

         

        } catch(Exception e) {

            System.debug(e);

        }

         

    }   

     

    global void finish(Database.BatchableContext BC) {

    }

}

2.

global class CloseAccountPayment implements Database.Batchable<sObject> {

  @InvocableMethod(label='Close Account Payment' description='This will update and lock Account Payment at the end of the cycle')

  public static void executeBatchMethod(List<String> lst) { Database.executeBatch(new CloseAccountPayment(), 100);}

  

    global Database.QueryLocator start(Database.BatchableContext BC)

    {      

       return Database.getQueryLocator('SELECT Id, Closed__c FROM Account_Payment__c WHERE Closed__c=false');

    }

       

    global void execute(Database.BatchableContext BC, List<Account_Payment__c> apList) {      

        for(Account_Payment__c ap : apList) {                     

           ap.Closed__c = true;      

        }

        

        try {

            update apList;

         

        } catch(Exception e) {

            System.debug(e);

        }

         

    }   

     

    global void finish(Database.BatchableContext BC) {

    }

}

3.

global class CloseCollection implements Database.Batchable<sObject> {

  @InvocableMethod(label='Close Collection Activity' description='This will close collection activity at the end of the cycle')

  public static void executeBatchMethod(List<String> lst) {Database.executeBatch(new CloseCollection(), 100);}

  

    global Database.QueryLocator start(Database.BatchableContext BC)

    {

        return Database.getQueryLocator('SELECT Id, Collection_State__c, Collection_Status__c, CA_Closed__c FROM Collection__c WHERE CA_Closed__c=false');

    }

     

    global void execute(Database.BatchableContext BC, List<Collection__c> caList) {      

        for(Collection__c ca : caList) {   

          

          if (ca.Collection_Status__c!='Bad Debt Account' && ca.Collection_Status__c!='Aging Account' && ca.Collection_State__c=='Open') ca.Collection_Status__c='Account Not Paid';                

          if(ca.Collection_State__c=='Open') ca.Collection_State__c = 'Closed - No Payment';

          ca.CA_Closed__c=true;   

        }

        

        try {

            update caList;

         

        } catch(Exception e) {

            System.debug(e);

        }

         

    }   

     

    global void finish(Database.BatchableContext BC) {

    }

}

The two instances when the classes were run:

Batch apex successfully completed but also didn't perform any field update
1 resposta
  1. 14 de mai. de 2021, 10:51
    Hi Pratik,

    Please try to put debug logs to see if any other automated process is running in the backend on scheduled time at 3AM, which could be a reason behind rollback of changes for the other two changes.

    Looking at the debug logs for scheduled time could give more insight on what is happening at that particular time and can be tracked to make changes accordingly.

    Hope above information helps, Please mark as Best Answer so that it can help others in the future.

    Thanks.
0/9000