3

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?

5
  • Can't immediately spot why it wouldn't be performing, but have you considered not doing the database update within your iteration ? Even in less restrictive batch context, doing so many separate DML actions is quite excessive and will slow down your batch. – Samuel De Rycke Dec 16 '13 at 14:56
  • DML action is outside the for loop.so is that still bad?..or if not there where do i move my update to – sfdc99999 Dec 16 '13 at 15:03
  • apologies, i'm absolutely blind today. However, you're updating your team collection, yet modifying the records you query within your batch execution, which are not the records in your new query, at least not in apex memory. – Samuel De Rycke Dec 16 '13 at 15:06
  • Do you see a log entry for the start() method? – Mike Chale Dec 16 '13 at 15:15
  • 1
    @Sdry loks like you are not the only one whos blind :). – sfdc99999 Dec 16 '13 at 15:19
5

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 ;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.