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
Ram chowdaryRam chowdary 

Batch apex to caliculateTotal amount sum in opportunities


Hi Folks, I have a use case that I need to display total  amount sum from opportunities object in finsh method... please help me ...

global class batchclass1 implements Database.Batchable<Opportunities>{
    
    global batchclass1(){}
        /* Batch class constructor */
    //Start method
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query='SELECT id, Amount from Opportunities';
         return Database.getQueryLocator(query);
        }

    //Excute method
    
global void execute(Database.BatchableContext BC, List<Opportunities> scope)
{
    //Excution logic
    List<Opportunities> opp= new List<Opportunities>
    AggregateResult[] gr= [SELECT SUM(Amount) FROM Opportunities];

    
}
      global void finish(Database.BatchableContext BC){
            // Finish logic
           system.debug('gr');
       }
    
    

}
Best Answer chosen by Ram chowdary
Chandra Sekhar CH N VChandra Sekhar CH N V
I see many sematic errors in your code. Please try the below one - 
global class batchclass1 implements Database.Batchable<sObject>,Database.Stateful{
    global Decimal sum;
    global batchclass1(){}
        /* Batch class constructor */
    //Start method
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query='SELECT id, Amount from Opportunity';
         return Database.getQueryLocator(query);
        }

    //Excute method
    
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
    //Excution logic
    //List<Opportunity> opp= new List<Opportunities>();
    AggregateResult[] gr= [SELECT SUM(Amount) optyamt FROM Opportunity];
    
    for(AggregateResult ag:gr){
        sum = (Decimal)ag.get('optyamt');
    }
    
}
      global void finish(Database.BatchableContext BC){
            // Finish logic
          system.debug(''+sum); 
       }
}

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
I see many sematic errors in your code. Please try the below one - 
global class batchclass1 implements Database.Batchable<sObject>,Database.Stateful{
    global Decimal sum;
    global batchclass1(){}
        /* Batch class constructor */
    //Start method
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query='SELECT id, Amount from Opportunity';
         return Database.getQueryLocator(query);
        }

    //Excute method
    
global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
    //Excution logic
    //List<Opportunity> opp= new List<Opportunities>();
    AggregateResult[] gr= [SELECT SUM(Amount) optyamt FROM Opportunity];
    
    for(AggregateResult ag:gr){
        sum = (Decimal)ag.get('optyamt');
    }
    
}
      global void finish(Database.BatchableContext BC){
            // Finish logic
          system.debug(''+sum); 
       }
}
This was selected as the best answer
Ram chowdaryRam chowdary
Thanks Buddy, now its working fine
chetan jogichetan jogi
@Chandra Sekhar CH N V May If we are not using list we queryied in start method then why should we take that query in scope veriable?