+ 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