function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhi MehtaAbhi Mehta 

To Show Case Count On Account Via Batch Class

Can someone help me with the batch class, I need to show count of cases on their respective account. Please don't suggest trigger or reports.

Why i need batch is because, i need cases in past 365days. And need to update it daily.
Best Answer chosen by Abhi Mehta
Khan AnasKhan Anas (Salesforce Developers) 
Hi Abhi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
public class Batch_CountCasesOnAcc implements Database.Batchable<SObject> {
    
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([select Id from Account order by Name]);
    }
    
    public void execute(Database.BatchableContext context, List<Account> scope) {
        List<Account> accLst = new List<Account>();
            for (AggregateResult ar : [
                SELECT AccountId a, count(Id) c
                FROM Case
                WHERE AccountId in :scope
                GROUP BY AccountId
            ]) {
                accLst.add(new Account(
                    Id = (Id) ar.get('a'),
                    Number_Of_Cases__c = (Decimal) ar.get('c')
                ));
            }
        UPDATE accLst;
    }
    
    public void finish(Database.BatchableContext context) {
    }
}

Run the Batch class:
Database.executeBatch(new Batch_CountCasesOnAcc(), 200);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Abhi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
public class Batch_CountCasesOnAcc implements Database.Batchable<SObject> {
    
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([select Id from Account order by Name]);
    }
    
    public void execute(Database.BatchableContext context, List<Account> scope) {
        List<Account> accLst = new List<Account>();
            for (AggregateResult ar : [
                SELECT AccountId a, count(Id) c
                FROM Case
                WHERE AccountId in :scope
                GROUP BY AccountId
            ]) {
                accLst.add(new Account(
                    Id = (Id) ar.get('a'),
                    Number_Of_Cases__c = (Decimal) ar.get('c')
                ));
            }
        UPDATE accLst;
    }
    
    public void finish(Database.BatchableContext context) {
    }
}

Run the Batch class:
Database.executeBatch(new Batch_CountCasesOnAcc(), 200);

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Abhi MehtaAbhi Mehta
Thankyou Khan Anas.