+ Start a Discussion
subodh chaturvedi 17subodh chaturvedi 17 

Getting Issue In my batch class need to add latest Annual Revenue(12 Records) not Getting answer as Expected , it is Adding but not correctly

Hi 
I have a requirement Where
  1. Processed Lines Business is a  Custom Object where I have  a Field  called  “Total LOB Annual Revenue” on the records  & it should be populated with the sum of “Monthly Revenue” which is  also a field on the same record .
       2 If i have 12 Records Then the Sum of all 'Monthly Revenue' should be Populated on the Latest Month Record  ,suppose i have records from Sept-16 to Sept- 17  then on Sept 17 record  the latest 'Total LOB Annual Revenue' get Updated.

3. i am passing the parameter like Credit /debit/ etc so based on the LOB value it is filtering the Record ,so it is taking only those Record Which Lob parameter is passed.

Please let me know where i am doing wrong.



//Batch to update Total Revenue for Processed LOB.
global class BatchUpdateAI implements Database.Batchable<sObject>
{                  
                  
            private String strParameter;
            private List <ProcessedLinesofBusinessDetail__c> pLOBToUpdate = new list<ProcessedLinesofBusinessDetail__c>();

            public BatchUpdateAI(String strParam) 
            {
                strParameter = strParam;
            }
            
            
            global Database.QueryLocator start(Database.BatchableContext BC)
            {
               String query = 'select id, AsofDate__c, AsofDateCalc__c, LOB__c from ProcessedLinesofBusinessDetail__c';
               return Database.getQueryLocator(query);
            }

            global void execute(Database.BatchableContext BC,List<ProcessedLinesofBusinessDetail__c> scope)
            {                 
                  Double totalRev = 0.0;
                  
                  for(Account acc : [select id, (select id, AsofDateCalc__c, TotalAnnualRevenue__c, MonthlyRevenue__c from Processed_Lines_of_Business_Details__r where 
                                            LOB__c =: strParameter and AsofDateCalc__c = LAST_N_MONTHS:12 ) from Account])
                  {
                               totalRev = 0.0;
                               for(ProcessedLinesofBusinessDetail__c plob : acc.Processed_Lines_of_Business_Details__r) 
                               {
                                    if(plob.MonthlyRevenue__c != null)
                                        totalRev = totalRev + plob.MonthlyRevenue__c;
                               }
                               
                               for(ProcessedLinesofBusinessDetail__c temp : acc.Processed_Lines_of_Business_Details__r)
                               {
                                    temp.TotalAnnualRevenue__c = totalRev;
                                    pLOBToUpdate.add(temp);
                               }                  
                   }
                   if(!pLOBToUpdate.isEmpty() && pLOBToUpdate.size()>0)
                            update pLOBToUpdate;                  
            }

            global void finish(Database.BatchableContext BC)
            {
                
            }
 }
Jaap ScheperJaap Scheper
Try this: global class BatchUpdateAI implements Database.Batchable<sObject>, Database.Stateful