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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Batch not updating correct value

Hi, 

I wrote a below batch program which is working but bug i noticed is its updating common value to all the accounts which should not happen

example : account_ 1 has two opportunity with amount = 200
                 account_ 2 has one opportunity with amount = 100

When i run below batch it is update 200 as common to account_1 and account_2 which is a bug it should actiually update 200 for account_1  and 100 for account_1

Please suggest me how to get this fixed
global class batchpreamount implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Previous_Year_Closed_Opportunities__c   from Account';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        List<Id> accIds = new List<Id>();
        for(Account a : scope)
        {
                accIds.add(a.id);
        }
            // query outside for loop
        List<AggregateResult> groupedResults = [ 
        SELECT sum(amount)preamount from opportunity 
                                     WHERE accountid in:accIds and
                                           isDeleted = false AND forecastcategoryname = 'commit' AND 
                                           closedate = LAST_YEAR];
                                       
                      
                                
      if (groupedResults[0].get('preamount') != NULL)
      {
       Decimal totalSum = Decimal.valueOf ( groupedResults[0].get('preamount') + '');

        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = totalSum;
        }
            update scope;
        }
        
      if (groupedResults[0].get('preamount') == NULL)
      {
       
        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = NULL;
        }
            update scope;
        }   
        
        
      }  
    
    global void finish(Database.BatchableContext BC)
    {  
    }
}

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
Abhay RathoreAbhay Rathore
global class batchpreamount implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Previous_Year_Closed_Opportunities__c   from Account';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        List<Id> accIds = new List<Id>();
        for(Account a : scope)
        {
                accIds.add(a.id);
        }
            // query outside for loop
        List<AggregateResult> groupedResults = [ 
        SELECT sum(amount)preamount from opportunity 
                                     WHERE accountid in:accIds and
                                           isDeleted = false AND forecastcategoryname = 'commit' AND 
                                           closedate = LAST_YEAR];
                                       
                      
                                
      for (AggregateResult AR : groupedResults)
      {
       Decimal totalSum = Decimal.valueOf ( AR.get('preamount') + '');

        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = totalSum;
        }
            update scope;
        
        
      if (AR.get('preamount') == NULL)
      {
       
        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = NULL;
        }
            update scope;
        }  
}		
        
        
      }  
    
    global void finish(Database.BatchableContext BC)
    {  
    }
}
Try this. Hope this helps you

All Answers

Abhay RathoreAbhay Rathore
I think the problem is with groupedResults[0]. [0] is addressing the first value on the list. Replace groupedResults[0] by for loop.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Abhay for you reply I am bit confused with your statement 
 Replace groupedResults[0] by for loop.

can you please show me a example
 
Abhay RathoreAbhay Rathore
global class batchpreamount implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Name, Previous_Year_Closed_Opportunities__c   from Account';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
        List<Id> accIds = new List<Id>();
        for(Account a : scope)
        {
                accIds.add(a.id);
        }
            // query outside for loop
        List<AggregateResult> groupedResults = [ 
        SELECT sum(amount)preamount from opportunity 
                                     WHERE accountid in:accIds and
                                           isDeleted = false AND forecastcategoryname = 'commit' AND 
                                           closedate = LAST_YEAR];
                                       
                      
                                
      for (AggregateResult AR : groupedResults)
      {
       Decimal totalSum = Decimal.valueOf ( AR.get('preamount') + '');

        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = totalSum;
        }
            update scope;
        
        
      if (AR.get('preamount') == NULL)
      {
       
        for(Account a : Scope)
        {
            a.Previous_Year_Closed_Opportunities__c  = NULL;
        }
            update scope;
        }  
}		
        
        
      }  
    
    global void finish(Database.BatchableContext BC)
    {  
    }
}
Try this. Hope this helps you
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com
I am getting a First error: Invalid decimal: null
when running a batch program
sudhirn@merunetworks.comsudhirn@merunetworks.com
I fixed all the error but still its not working any suggestion? mass update is happening common to all accounts