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
sridhar sridharsridhar sridhar 

batch failing to update all the records with proper data

HI all , 
Serious PROD issue. Can any one provide solution ...
Below Batch class which update Fact_Revenue__C in Opportunity  from Revenue__c object . it runs daily basis 
Issue: many records are not populating correct values into Fact_Revenue__C . 

Suppose : Sum(ttlRmrev__C)  = $ 100 same has to populate in Fact_Revenue__C  in opportunity but for many records it is failing .. updaing less than the correct value.
global without sharing class batchrevenue implements Database.Batchable<sObject>{
    public String strQuery ='';
    public DateTime dtProvided ; 
    AggregateResult[] tlrevenue ;
     set<id> sopids;
    global batchableSumGroupBookingCodeRevenue(Date sldte){
        if(sldte == null){
            sldte =;
        dtProvided = DateTime.newInstance(sldte.year(),sldte.month(),,0,0,0);
            strQuery = 'Select Id From Opportunity where  glc_Code__C != null limit 2';
            strQuery = 'Select Id From Opportunity where  glc_Code__C != null ';
        sopids= new Set<Id>();   

 global Database.QueryLocator start(Database.BatchableContext BC)
    return Database.getQueryLocator(strQuery); 
global void execute(Database.BatchableContext BC, List<Opportunity> scopeOpty)
    if(sopids != null){
        sopids= new Set<Id>();
    for(Opportunity opty: scopeOpty)


public void tlrrmrev( set<id> sopids ){
        list<Opportunity> selOpportunity = new list<Opportunity>();
        tlrevenue =[select SUM(ttlRmrev__C) totalRevenue, Opportunity__c   Opportunity
                        From Revenue__c 
                        where Opportunity__c in : sopids GROUP BY Opportunity__c];
        for(AggregateResult agreeResult : tlrevenue){
            if(agreeResult.get('Opportunity') != null){
                selOpportunity.add(new Opportunity(id = (string) agreeResult.get('Opportunity'), Fact_Revenue__C = (decimal) agreeResult.get('totalRevenue')));
        if(selOpportunity.size() > 0){
            update selOpportunity;
    catch(exception ex){
         System.debug('Exception is :::' + ex.getMessage());



Alain CabonAlain Cabon

Do you have some examples of the different values?  0 instead of 100, 50 instead of 100

1) Your query locator is: Select Id From Opportunity where  glc_Code__C != null 

All the opportunities with glc_Code__C == null are ignored (out of scope).

2) The governor limits for the AggregateResult if the scope of your query locator is very high but you should have an error.

Queries that include aggregate functions are subject to the same governor limits as other SOQL queries for the total number of records returned. This limit includes any records included in the aggregation, not just the number of rows returned by the query. If you encounter this limit, you should add a condition to the WHERE clause to reduce the amount of records processed by the query.

3) The null values: the sum of null values is null if all the values of the calculation are null (in the past, the sum was null is only one value was null).

sridhar sridharsridhar sridhar
HI Alain .. Thanks for your kind Response.. 

From your list of raised points 

1. is required logic 
3. is fine 

2. i am not clear what you are trying to explain .... but soql returend records definetly not exceeding govern limits... hardly it will retrive very few revenue records for opportunity. 

if you think to put where in the case .. can you share  idea on this. 

Alain CabonAlain Cabon
The problem of the stored calculated values could be that you need a trigger moreover for the creations of opportunities in the current day even if your batch is daily.

Are there many wrong results for opportunities when their last modified dates are in the past days with glc_Code__C != null ?

You have probably already check the last modified dates of the opportunities in detail (after the end of the batch).

sridhar sridharsridhar sridhar
Hi Alain .. Thanks for Resopnse .. 

Batch is succesfully updating the record whern glc_Code__C != null but not with correct value... 
I can see lastmodified date changed every day after batch job completion (monitored it for couple of days).. but it was failing to update correct values. 
Alain CabonAlain Cabon

As I fear the same problem one day for myself, I am interested by the solution (I don't have it)

Another possibility is the change of values of glc_Code__C after the batch ended.

So you need a trigger if this value glc_Code__C changes 

Without a trigger on the updates of glc_Code__C, the stored sum could be false. 

sridhar sridharsridhar sridhar
Databse.statuful can help on this?? 

i wnat to log all the batch jobs .. How can i save debug logs from batch job to catch eroor which is causing fail to update records throug batch job ??