+ Start a Discussion
lovetolearnlovetolearn 

Update Failed from Scheduled Apex Job

Hi, 

 

I am trying to schedule an Apex class to run daily. The Apex class is supposed to update query for the number of Cases against a Member record, then update a field with this number. The Apex class ran at it's scheduled time (I checked the Schedule Jobs queue and it showed me that it indeed did run). However, the field was not updated. Please help me. Here is my code: 

global class accCaseCnt implements Schedulable{
    
    global void execute (SchedulableContext sc){
        updateAcc();
    }
    
    
    public void numAccCase(ID accID){
        Map<ID, Integer> accMap = new Map<ID, Integer>();
        
        Account[] acc = [SELECT Id FROM Account WHERE Id =: accID];
    
        LIST<AggregateResult> totalCases = [SELECT AccountID, Count(ID) cnts FROM Case WHERE AccountID =: accID GROUP BY AccountID];
    
        for(AggregateResult ar: totalCases){
            accMap.put(String.valueOf(ar.get('AccountID')), Integer.valueOf(ar.get('cnts')));
        }
    
        for(Account a: acc){
            acc.Total_Cases__c = accMap.get(acc.Id);
        }
    }
    
    public void updateAcc(){
        Integer i;
        Integer x = 0;
        
        List<Account>accList = new List<Account>([SELECT ID FROM Account]);
        
        for(i = 0; i<=accList.size(); i++){
            numAccCase(accList[x].id);
            x++;
        }
    }    
}

 Thank you.

jungleeejungleee

Hi,

 

I think the reason why its not updating is coz you have not added any DML statements like update thisList; in the code. Also I also think there will be chances your code will hit the governer limits   as you are calling the numAccCase method, which has 2 SOQL queries, inside the for loop.

 

Regards

Sam

maja madi