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
Pradeep Pradhan 21Pradeep Pradhan 21 

Create a Batch Class, to Calculate the Total Annual Revenue of all the Account Records in the object, by dividing them to the various batches of size 4.

SwethaSwetha (Salesforce Developers) 
Hi Pradeep,
Can you elaborate on how to calculate Annual revenue? Do we have some formula for this ?

Thank you
Sanjai Balakrishnan 20Sanjai Balakrishnan 20
Import 800 account records into your org.
try this code...

public  class AccountBatchClass implements Database.Batchable<sobject>, Database.Stateful
{
     decimal sum;
    public Database.QueryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator('select AnnualRevenue from Account where CreatedDate=Today');
    }
    public void execute(Database.BatchableContext bc, List<Account> accList)
    {
        AggregateResult[] res=[Select SUM(AnnualRevenue)tot_annual from Account where CreatedDate=Today];
        for(AggregateResult ag:res)
        {
           sum=(Decimal)ag.get('tot_annual');
        }
    }
    public void finish(Database.BatchableContext bc)
    {
        System.debug('Sum of Annual Revenue : '+sum);
    }  
}

in Annonymous Window...

AccountBatchClass ac=new AccountBatchClass();
Id batchId = Database.executeBatch(ac, 200);
Id batchId1 = Database.executeBatch(ac, 200);
Id batchId2 = Database.executeBatch(ac, 200);
Id batchId3 = Database.executeBatch(ac, 200);

and check batch apex in logs.

Hope it will be helpful for u.
nabeel k 3nabeel k 3
thank you sanjai.it worked..