0

I have created the batch class which will store the count in other object with status "New" and "Closed" I have 60 Case records with 25 as "New" status and remaining with "Closed" status

when i am running batch of 25 then my count is coming 0 for "New".

because in 3rd batch there is no record with "New" status Values.Please suggest how to tackle this.

global class CaseBatchApex implements Database.Batchable<sObject>, Database.Stateful {
global List<string>lstcount;
global List<integer>lstcount1;

global Database.QueryLocator start(Database.BatchableContext bc) {

    return Database.getQueryLocator(
        'SELECT ID, status FROM case');
}
    global void execute(Database.BatchableContext bc, List<case> scope){

        integer count2;
       Map<case,string> casemap=new Map<case,string>();
      Map<case,string> casemap1=new Map<case,string>();
        System.debug('Scope>>>'+scope);
        for(case c:scope){
            if(c.status=='New'){
            casemap.put(c,c.Status);
            }
            else if(c.status=='Closed'){
            casemap1.put(c,c.Status);
            }
        }
       lstcount=new list<string>([select count() from case where status in:casemap.values()]);
        lstcount1=new list<integer>([select count() from case where status in:casemap1.values()]);
          CaseCount cse=new CaseCount();
        cse.id='a077F000001pAxb';
        cse.New_Status_count__c=string.valueof(lstcount.size());
        cse.Closed_Status_Count__c=string.valueof(lstcount1.size());
        update cse;  
}    

global void finish(Database.BatchableContext bc){

    }    

}

0

1 Answer 1

0

Well, it seems you are updating your CaseCount object record in every batch run, so if the 25 Case records with New status are in the previous batch run, you will overwrite the count in the third run to 0.

Try this:

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

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id, Status FROM Case');
    }

    global void execute(Database.BatchableContext bc, List<Case> cases){
        CaseCount caseCount = [SELECT Id, New_Status_count__c, Closed_Status_Count__c 
                               FROM CaseCount 
                               WHERE Id = 'a077F000001pAxb'];

        for(Case c : cases){
            if(c.status == 'New') {
                caseCount.New_Status_count__c = cse.New_Status_count__c + 1;
            } else if (c.status == 'Closed') {
                caseCount.Closed_Status_Count__c = cse.Closed_Status_Count__c + 1;
            }
        }

        update caseCount;  
    }    

    global void finish(Database.BatchableContext bc){} 
}

You must log in to answer this question.

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