1

I have a use case to run sequence of Batch to execute my logic. There I need to have a way to start the Batch and terminate it, So for that I keep a global variable to store the records and according to the record I was start the Batch in sequence. I will share my code below and if you have any idea please share with me to overcome this issue,

I am thinking that when we start the sequence batch their might be the global variable reset.

global class Main_BATCH implements Database.Batchable<SObject>, Database.Stateful {

    global String query;
    public static List<Object_Rule__mdt> ObjectAndFieldsRule;

    global Main_BATCH() {

        ObjectAndFieldsRule = new List<Object_Rule__mdt>();

        if (ObjectAndFieldsRule.size() == 0) {
            ObjectAndFieldsRule = [
                    SELECT soco__Action__c, soco__Order__c, soco__SObject__c, 
(SELECT soco__Action__c, soco__Field_Name__c, Object_Rule__c FROM 
Field_Rules__r)
                FROM Object_Rule__mdt
                ORDER BY soco__Order__c
        ];
    }

    this.query = 'Construct query according to the meta data Master and Child records'
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(this.query);
    }

    global void execute(Database.BatchableContext BC, List<SObject> sobjects){
    try {
            batchJobId = BC.getJobId();
            'Do the logic'
        } catch (Exception e) {

        }
    }

    global void finish(Database.BatchableContext bc) {

        Main_BATCH anonimizationMainBATCH = new Main_BATCH();
        ObjectAndFieldsRule.remove(0);
        if (ObjectAndFieldsRule.size() > 0) {
            Database.executeBatch(anonimizationMainBATCH, 200);
        }
    }
}

When I run the above Batch it act as an infinite Batch with the first meta data record.

1 Answer 1

2

Static variables are not stored in an object's state, and therefore won't be retained across transactions in your batch. You will need to make the variable an instance variable:

public List<Object_Rule__mdt> ObjectAndFieldsRule;

In addition, you need to use the same instance of your batch when chaining:

Database.executeBatch(this, 200);

This will "keep your place" with the state you had previously.

9
  • I have tried that as well but no luck. Feb 7, 2019 at 5:58
  • @Hariprasath I just realized you were creating a new copy. See edit.
    – sfdcfox
    Feb 7, 2019 at 6:00
  • yes it will terminate the infinite loop. But for the second Batch we need to again call the constructor to construct the query according to the 2nd metadata record. if we use the same batch does it call the constructor again with the previous context? Feb 7, 2019 at 6:13
  • @Hariprasath Why do you need to call the constructor again? You're not done processing the first list yet. You call .remove(0), then you're calling it again, which presumably means you wish to continue processing the list of records you queried in the constructor.
    – sfdcfox
    Feb 7, 2019 at 6:15
  • 1
    sfdcfox I moved the logic to start method and called the same Batch solved the issue. Feb 7, 2019 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .