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
Viru D.Viru D. 

aggregateResults are not calculating in after insert trigger

aggregateResult are not calculating in after insret trigger but they work fine in after update trigger.
However after insert all the records are available and I can able to print it in the log. 
I am seeing this first time. why aggregrate results are not avilable in after insert trigger ?
Giving log here for your reference.

Thanks in advance.


public static void Update_Forecasting_by_group(map<id,Monthly_Forecast__c> MapMontlyForecast ){
        if(Execonce_method2){
           Execonce_method2 = false;
           list<Forecast_by_group> List_ForcastbyGr = new list<Forecast_by_group>();
           map<string ,Forecast_by_group> map_ForcastbyGr = new map<string ,Forecast_by_group>();
           map<string,id> FBG_Key_id_map = new map<string,id>();
           list<Forecasting_By_Group__c> Upsert_list = new list<Forecasting_By_Group__c>();   
           set<string> Set_ProdGrpMkt = new set<string>();
           
           system.debug('==MapMontlyForecast.keyset()==' + MapMontlyForecast.keyset()); 
           
           for(Monthly_Forecast__c MFobj : [select id,Account_Product_Forecast__r.Product_Group_Marketing__c,Product_Group_Marketing__c, Forecast_for_Date__c, revenue__c, delta_revenue__c, Product_Name__c, Account_Name__c from Monthly_Forecast__c where id in : MapMontlyForecast.keyset()] ){
                 system.debug('== Monthly_Forecast__c field values ==' + MFobj);
                 if(MFobj.Account_Product_Forecast__r.Product_Group_Marketing__c != null ) 
                 Set_ProdGrpMkt.add(MFobj.Account_Product_Forecast__r.Product_Group_Marketing__c);
           }
           
            
           system.debug('==Set_ProdGrpMkt==' + Set_ProdGrpMkt );
           List<aggregateResult> results = [select sum(Revenue__c) Total_Revenue , SUM(Delta_Revenue__c) Total_Delta_Revenue, Product_Group_Marketing__c, Forecast_for_Date__c from Monthly_Forecast__c where (Forecast_for_Date__c = Last_N_Years:1 or Forecast_for_Date__c = this_year) and Product_Group_Marketing__c in : Set_ProdGrpMkt group by Product_Group_Marketing__c, Forecast_for_Date__c];
          //List<aggregateResult> results = [select sum(Revenue__c) Total_Revenue , SUM(Delta_Revenue__c) Total_Delta_Revenue, Product_Group_Marketing__c, Forecast_for_Date__c from Monthly_Forecast__c where Product_Group_Marketing__c != null group by Product_Group_Marketing__c, Forecast_for_Date__c]; 
           
           system.debug('==results==' + results);
           
           for(aggregateResult ar  : results){
                 List_ForcastbyGr.add(new Forecast_by_group( (double)ar.get('Total_Revenue'),(double)ar.get('Total_Delta_Revenue') , (string)ar.get('Product_Group_Marketing__c'), (date)ar.get('Forecast_for_Date__c')  ));
           } 
           
           system.debug('==List_ForcastbyGr==' + List_ForcastbyGr);
           
           for(Forecast_by_group FBGobj : List_ForcastbyGr){
             map_ForcastbyGr.put(FBGobj.Forecast_by_group_key,FBGobj);
           }
          
          system.debug('==map_ForcastbyGr==' + map_ForcastbyGr);
          
          for(Forecasting_By_Group__c FBGobj : [select id,Forecast_by_group_key__c from Forecasting_By_Group__c where Forecast_by_group_key__c in : map_ForcastbyGr.keyset()])
              FBG_Key_id_map.put(FBGobj.Forecast_by_group_key__c, FBGobj.id);
                        
         system.debug('==FBG_Key_id_map==' + FBG_Key_id_map);     
              
          for(string Key : map_ForcastbyGr.keyset()){
                system.debug('==Key==' + Key);  
                Forecast_by_group FBGOBJ =   map_ForcastbyGr.get(Key);
                if(FBG_Key_id_map.containskey(Key))
                   Upsert_list.add(new Forecasting_By_Group__c(id = FBG_Key_id_map.get(key), Total_Revenue__c = FBGOBJ.Total_Revenue, Total_Delta__c = FBGOBJ.Total_Delta_Revenue,Product_Group_Marketing__c = FBGOBJ.product_mkt_grp, Forecast_for_Date__c = FBGOBJ.forecast_date));
                else
                   Upsert_list.add(new Forecasting_By_Group__c(Total_Revenue__c = FBGOBJ.Total_Revenue, Total_Delta__c = FBGOBJ.Total_Delta_Revenue,Product_Group_Marketing__c = FBGOBJ.product_mkt_grp, Forecast_for_Date__c = FBGOBJ.forecast_date));
            }
          
          system.debug('==Upsert_list==' + Upsert_list);
          
              if(Upsert_list.size() > 0)
                 upsert Upsert_list;
        }
    }

Log :

10:18:58.0 (285221766)|USER_DEBUG|[77]|DEBUG|==MapMontlyForecast.keyset()=={a0T4F000000jXiUUAU, a0T4F000000jXiVUAU}
10:18:58.0 (285563121)|SOQL_EXECUTE_BEGIN|[79]|Aggregations:0|SELECT id, Account_Product_Forecast__r.Product_Group_Marketing__c, Product_Group_Marketing__c, Forecast_for_Date__c, revenue__c, delta_revenue__c, Product_Name__c, Account_Name__c FROM Monthly_Forecast__c 
10:18:58.0 (295157565)|SOQL_EXECUTE_END|[79]|Rows:2
10:18:58.0 (295594304)|USER_DEBUG|[80]|DEBUG|== Monthly_Forecast__c field values ==Monthly_Forecast__c:{Id=a0T4F000000jXiUUAU, Account_Product_Forecast__c=a2q4F000000NYSHQA4, Forecast_for_Date__c=2018-07-15 00:00:00, Revenue__c=9.7284, Delta_Revenue__c=0.0000, Product_Name__c=LXML-PB01-0040, Account_Name__c=THOUSAND LIGHTS LIGHTING (CHANGZHOU) LIMITED, CurrencyIsoCode=USD}
10:18:58.0 (295751548)|USER_DEBUG|[80]|DEBUG|== Monthly_Forecast__c field values ==Monthly_Forecast__c:{Id=a0T4F000000jXiVUAU, Account_Product_Forecast__c=a2q4F000000NYSHQA4, Forecast_for_Date__c=2018-08-15 00:00:00, Revenue__c=19.4568, Delta_Revenue__c=0.0000, Product_Name__c=LXML-PB01-0040, Account_Name__c=THOUSAND LIGHTS LIGHTING (CHANGZHOU) LIMITED, CurrencyIsoCode=USD}
10:18:58.0 (295831738)|USER_DEBUG|[86]|DEBUG|==Set_ProdGrpMkt=={Colors}
10:18:58.0 (298335806)|SOQL_EXECUTE_BEGIN|[87]|Aggregations:0|SELECT SUM(Revenue__c) Total_Revenue, SUM(Delta_Revenue__c) Total_Delta_Revenue, Product_Group_Marketing__c, Forecast_for_Date__c FROM Monthly_Forecast__c WHERE ((Forecast_for_Date__c = Last_N_Years:1 OR Forecast_for_Date__c = this_year) AND Product_Group_Marketing__c = :tmpVar1) GROUP BY Product_Group_Marketing__c, Forecast_for_Date__c
10:18:58.0 (345034833)|SOQL_EXECUTE_END|[87]|Rows:0
10:18:58.0 (345188715)|USER_DEBUG|[90]|DEBUG|==results==()  ----> here is nothing is calcuating.
10:18:58.0 (345266134)|USER_DEBUG|[96]|DEBUG|==List_ForcastbyGr==()
10:18:58.0 (345397915)|USER_DEBUG|[102]|DEBUG|==map_ForcastbyGr=={}
10:18:58.0 (345676009)|SOQL_EXECUTE_BEGIN|[104]|Aggregations:0|SELECT id, Forecast_by_group_key__c FROM Forecasting_By_Group__c 
10:18:58.0 (349200864)|SOQL_EXECUTE_END|[104]|Rows:0
10:18:58.0 (349389572)|USER_DEBUG|[107]|DEBUG|==FBG_Key_id_map=={}
10:18:58.0 (349486674)|USER_DEBUG|[118]|DEBUG|==Upsert_list==()
Raj VakatiRaj Vakati
Are you loading data from the data loader ? or this is happening at single record insert .. If you are uploading bulk data there may be delay on caliculation fields from the salesforce ...