[SalesForce] Batch apex execute method not invoked

I got a batch apex as below

global class Recalculatemembers implements database.batchable<sobject> {

    public final string query;
    
    //Get the query in constructor
    global Recalculatemembers(string q) {
        query = q;
    }

    //Get the records in start method
    global Database.querylocator start(database.batchablecontext bc) {
        return database.getquerylocator(query);
    }

    //get the members size in execute method and update to DB
    global void execute(database.batchablecontext bc, list<team__C> team) {
        for (team__c item : [select id, (select id from Team_Members__r) from team__c where id in :team])
            item.Total_Number_of_Members__c = item.Team_Members__r.size();
        update team;
    }

    //Final finish method
    global void finish(database.batchablecontext bc) {

    }
    
}

I schedule this by below class

global class schedule_recalculatemembersbatch Implements Schedulable {

    public string query;
    global void execute(SchedulableContext sc) {
        query = 'select id,Total_Number_of_Members__c from team__c';
        Recalculatemembers b = new Recalculatemembers(query); 
        database.executebatch(b);
    }
    
}

I trigger this via below execute anonymous code

schedule_recalculatemembersbatch sm = new schedule_recalculatemembersbatch();
sm.execute(null);

Batch processing doesnt give any error.But when i look into my debug log,i can see execute method in batch never gets called.
Query does return expected rows though.
any clue why?

Best Answer

You're not actually committing the modified objects to the database. But passing the objects you've gotten in the scope. While these may be the same database records, they are not the same within the apex stack.

Understand the difference ?:

//get the members size in execute method and update to DB
        global void execute(database.batchablecontext bc,list<team__C> team)
        {
               list<team__c> toBeUpdated = [select id,(select id from Team_Members__r) from team__c where id in :team];
               for(team__c item:toBeUpdated ){
                      item.Total_Number_of_Members__c = item.Team_Members__r.size();
               }
               update toBeUpdated ;
        }
Related Topic