0

I am trying to correct an issue with an apex class that I did not develop. I am hoping that someone can assist in finding the error.

Essentially, the issue is with a field called Current_Client_Count__c. There are accounts related to other accounts through a custom child relationship via a lookup field. The child relationship is "Accounts1." For my count field, I want all child accounts where the field on the child account called "Number_of_active_plans__c" is greater than zero and isn't null. There are other things happening with this class, and all of those roll-ups are properly calculating, except this particular portion. What is happening is ALL child accounts are being counted, regardless if the "Number of active plans" field is greater than 0 and not null.

Any assistance would be greatly appreciated. I am new to coding, so just trying to figure out how we can correct this with the existing code in place. Thanks!

Here is the full code:

global class RollUpsBatch implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {

        return Database.getQueryLocator([Select  id From Account WHERE RecordType.Name ='Broker Account']);
    }

    global void execute(Database.BatchableContext BC, List<Account> lstscope)
    {   
        date myFirstDate = date.today().toStartOfMonth();
        date myLastDate = myFirstDate.addmonths(-12); 
        list<Account> lstAccount = [Select X12_month_Total_Claims__c, X12_month_SNS__c, X12_month_Net_Premium__c, X12_month_Net_Non_Store__c,
        RecordType.Name,X12_month_Lux_Share__c,New_Opportunities_last_12_months_Total__c,New_Opportunities_last_12_month_Won__c,Current_Client_Count__c,
       New_Opportunities_last_12_months_Lost__c,Members_Proforma_Total_last_12_months__c, Prior_Last_Year_End_Memberships__c,Current_Memberships__c,
        Eligi__c,Open_New_Opportunities__c, (Select Number_of_Active_Plans__c from Accounts1__r),
        (Select RecordTypeId,Members_Proforma__c,CloseDate,StageName,Eligible_Employees_Original_Forecast__c From Opportunities__r WHERE RecordType.Name = 'New' and CloseDate>=: myLastDate)
         From Account where  id in:lstscope];


        set<Id> setGroupAccount = new set<Id>();
        map<Id,list<Account>> mapGroupAccount = new map<Id,list<Account>>();
        map<Id,list<Plan__c>> mapAccountPlan = new map<Id,list<Plan__c>>();
        map<Id,list<Plan__c>> mapAccountlstPlan = new map<Id,list<Plan__c>>();
        list<Account> lstAccountUpdate = new list<Account>();
        list<Account> lstAccOpposDetails = new list<Account>();
        for(Account objacc : lstAccount)
         {
            mapGroupAccount.put(objacc.id,objacc.Accounts1__r);
            for(Account objGroup: objacc.Accounts1__r)
            {
                setGroupAccount.add(objGroup.id);
            }

         }

         list<Plan__c> lstPlanobj = new list<Plan__c>();
         lstPlanobj = [select id,X12_Month_Net_Premium__c,X12_Month_Total_Claims__c,X12_Month_Lux_Frame_Claims__c,X12_Month_SNS__c,Current_Membership__c,
         Client_Account__c,Prior_Year_End_Membership__c,X12_Month_Net_Non_Store__c,X12_Month_Lux_Share__c,X12_Month_Total_Frame_Claims__c from Plan__c WHERE 
         Client_Account__c IN:setGroupAccount and Termination_Date__c>=: myLastDate];

         for(Plan__c objp : lstPlanobj){
            if(mapAccountPlan.get(objp.Client_Account__c)  == null)
                mapAccountPlan.put(objp.Client_Account__c,new list<Plan__c>{objp});
            else
                mapAccountPlan.get(objp.Client_Account__c).add(objp);
         }

         for(Account objacc:lstAccount){
            if(mapGroupAccount.containskey(objacc.id)){
                for(Account objgrp: mapGroupAccount.get(objacc.id)){
                    if(mapAccountPlan.containsKey(objgrp.id)){
                        for(Plan__c objp:mapAccountPlan.get(objgrp.id)){
                            if(mapAccountlstPlan.get(objacc.id)  == null)
                                mapAccountlstPlan.put(objacc.id,new list<Plan__c>{objp});
                            else
                                mapAccountlstPlan.get(objacc.id).add(objp);
                        }
                    }
                }
            }
         }

         system.debug('mapAccountlstPlan --->'+mapAccountlstPlan);

         for(Account obja:lstAccount){
             obja.X12_month_Net_Premium__c = 0;
             obja.X12_month_Total_Claims__c = 0;
             obja.X12_month_SNS__c = 0;
             obja.Current_Memberships__c = 0;
             obja.Prior_Last_Year_End_Memberships__c = 0;
             obja.X12_month_Lux_Share__c = 0;
             obja.X12_month_Net_Non_Store__c = 0;
             obja.New_Opportunities_last_12_months_Total__c = 0;
             obja.Members_Proforma_Total_last_12_months__c = 0;
             obja.New_Opportunities_last_12_month_Won__c = 0;
             obja.New_Opportunities_last_12_months_Lost__c = 0;
             obja.Current_Client_Count__c = 0;
             obja.Eligi__c = 0;
             obja.Open_New_Opportunities__c = 0;
             Decimal Sum_X12_Month_Total_Frame_Claims =0;
             Decimal Sum_X12_Month_Lux_Frame_Claims  =0;
                          system.debug('mapAccountlstPlan --->'+mapAccountlstPlan.keySet());
             if(mapGroupAccount.get(obja.id)!= null){
                 obja.Current_Client_Count__c = mapGroupAccount.get(obja.id).size();
             }
            if(mapAccountlstPlan.containsKey(obja.id)){
                Integer count=0;
                for(Plan__c objp:mapAccountlstPlan.get(obja.id)){
                    count++;
                    system.debug('mapAccountlstPlan.get(obja) ---->'+mapAccountlstPlan.get(obja.id));
                    if(objp.X12_Month_Net_Premium__c != null)
                        obja.X12_month_Net_Premium__c += objp.X12_Month_Net_Premium__c;
                    if (obja.Number_of_Active_Plans__c > 0 && obja.Number_of_Active_Plans__c != null)
                       obja.Current_Client_Count__c += 1;
                    if(objp.X12_Month_Total_Claims__c != null)
                        obja.X12_month_Total_Claims__c += objp.X12_Month_Total_Claims__c;
                    if(objp.X12_Month_SNS__c != null)
                        obja.X12_month_SNS__c += objp.X12_Month_SNS__c;
                    if(objp.Current_Membership__c != null)
                        obja.Current_Memberships__c += objp.Current_Membership__c;
                    if(objp.Prior_Year_End_Membership__c != null)
                        obja.Prior_Last_Year_End_Memberships__c += objp.Prior_Year_End_Membership__c;
                    if(objp.X12_Month_Net_Non_Store__c != null)
                        obja.X12_month_Net_Non_Store__c += objp.X12_Month_Net_Non_Store__c;
                    if(objp.X12_Month_Total_Frame_Claims__c != null){
                        Sum_X12_Month_Total_Frame_Claims += objp.X12_Month_Total_Frame_Claims__c;
                    }
                    if(objp.X12_Month_Lux_Frame_Claims__c != null){
                        Sum_X12_Month_Lux_Frame_Claims += objp.X12_Month_Lux_Frame_Claims__c;
                    }
                    /*if(objp.X12_Month_Total_Frame_Claims__c != null && objp.X12_Month_Lux_Frame_Claims__c != null)
                    obja.X12_month_Lux_Share__c += (objp.X12_Month_Total_Frame_Claims__c + objp.X12_Month_Lux_Frame_Claims__c );*/

                }
                system.debug('Sum_X12_Month_Lux_Frame_Claims'+Sum_X12_Month_Lux_Frame_Claims );
                system.debug('Sum_X12_Month_Total_Frame_Claims'+Sum_X12_Month_Total_Frame_Claims);
                if(Sum_X12_Month_Total_Frame_Claims >0){
                    obja.X12_month_Lux_Share__c = (Sum_X12_Month_Lux_Frame_Claims /Sum_X12_Month_Total_Frame_Claims) * 100 ;
                }
                system.debug('persent valuee'+obja.X12_month_Lux_Share__c);
                lstAccountUpdate.add(obja);
            }

             for(Opportunity objOpp :obja.Opportunities__r){
                 system.debug('---->'+objopp);

                 if(objOpp.StageName == 'Closed Lost' || objopp.StageName == 'Closed - No Decision' || objopp.StageName == 'Closed Won'){  
                    obja.New_Opportunities_last_12_months_Total__c += 1;
                 }     
                 if(objOpp.Members_Proforma__c != null){  
                    obja.Members_Proforma_Total_last_12_months__c += objOpp.Members_Proforma__c;
                 }
                 if(objOpp.StageName == 'Closed Lost' || objopp.StageName == 'Closed - No Decision'){
                     obja.New_Opportunities_last_12_months_Lost__c += 1;
                 }
                 if(objOpp.StageName == 'Closed Won'){
                     obja.New_Opportunities_last_12_month_Won__c += 1;
                 }
                 if(objOpp.Eligible_Employees_Original_Forecast__c != null){
                     obja.Eligi__c += objOpp.Eligible_Employees_Original_Forecast__c;
                 }
                 if(objOpp.StageName == 'Prospecting' || objopp.StageName == 'Engaged' || objopp.StageName == 'Best & Few' || objopp.StageName == 'High Probability'){  
                    obja.Open_New_Opportunities__c += 1;
                 }        
             }    
             lstAccOpposDetails.add(obja);
         }

         if(lstAccountUpdate != null && lstAccountUpdate.size()>0){
            update lstAccountUpdate;
         }
        if(lstAccOpposDetails != null && lstAccOpposDetails.size()>0){
            update lstAccOpposDetails;
        }

        system.debug('lstAccountUpdate ---->'+lstAccountUpdate);
        system.debug('lstAccountUpdate size ---->'+lstAccountUpdate.size());
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

1 Answer 1

1

The > 0 does not seem to be in the criteria

change:

(Select Number_of_Active_Plans__c from Accounts1__r)

to this

(Select Number_of_Active_Plans__c from Accounts1__r Where Number_of_Active_Plans__c > 0)

BUT: Keep in mind that this may change the functionality of the rest of the code. I did not review it completely for function.

Since your comments says it DOES affect your code then change THIS:

if(mapGroupAccount.get(obja.id)!= null){
                 obja.Current_Client_Count__c = mapGroupAccount.get(obja.id).size();
             }

to this:

if(mapGroupAccount.get(obja.id)!= null){
                Integer cnt = 0;
                for(Account a : mapGroupAccount.get(obj.id)){
                        if(a. Number_of_Active_Plans__c > 0)
                            cnt ++;
                } 
            obja.Current_Client_Count__c = cnt;
             }

The original code simple puts the size of the children Accounts as the value and since they are not filtered earlier in the code the value will not be correct. The updated code will, for that part only, count the number that have a Number_of_Active_Plans__c > 0.

If you have additional criteria the you will need to add it as well

5
  • In summary, have an actual developer make those changes who can also ensure it is done correctly and doesn't break functionality.
    – dphil
    Dec 17, 2014 at 21:01
  • Hi Eric - Yes, that does change the functionality of the rest of the code. All of the code works as needed without this in the criteria. This field should only be counting accounts where the number of active plans >0. All of my other roll up fields here need to look at data from the last 12 months, regardless of the # of active plans. Thank you for the input!
    – mooreannem
    Dec 17, 2014 at 21:15
  • @mooreannem - see updated answer
    – Eric
    Dec 17, 2014 at 21:59
  • @dphil - FYI I am an actual developer. The statement is there because of the fact that given the limited scope of the question and not knowing the actual business practices / rules / etc (there is a lot going on in that code), the OP needs to be aware that making any changes could affect other results that may not have been closely reviewed.
    – Eric
    Dec 17, 2014 at 22:02
  • @Eric I never said you aren't a developer and nothing I said implies that. Why are you being all defensive?
    – dphil
    Dec 18, 2014 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.