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
Mohit KapoorMohit Kapoor 

I need help with bulkification of this code

I have tried alot but am not able to hit the limits for this code, the code provided above is in the most inert state and this is working, but the huge data calls is failing the soql limits.....

public class TW_Eval_FYB_FYE {

    public TW_Eval_FYB_FYE(ApexPages.StandardController controller) {
        }
        
    public static void Run() {
        list<TW_FY_Summary__c> theSummaries = new list<TW_FY_Summary__c>();
        list<TW_Monthly_Trend__c> theTrendsList  = new list<TW_Monthly_Trend__c>();
        set<TW_Monthly_Trend__c> theTrends  = new set<TW_Monthly_Trend__c>();
        list<TW_FY_Summary__c> updateList = new list<TW_FY_Summary__c>();
      
      date dateToday     = date.today();
      integer currMonth   = dateToday.month();
    string currYear    = string.valueOf(dateToday.year());

    //Take the Today() current month and match it to TW fiscal month
    if( currMonth >= 7 ) { currMonth = currMonth - 6; }
    else          { currMonth = currMonth + 6; }

        theSummaries    = [SELECT 
                                         Id, FYE_SF__c, FYB_SF__c, FYE_Headcount__c, 
                                         FYB_Headcount__c, FYE_Seats__c, FYB_Seats__c,
                                         FYB_All_in_Cost__c, FYE_All_in_Cost__c,
                                         Current_Headcount__c, Current_Seats__c, Fiscal_Year__c 
                                         
                                         FROM TW_FY_Summary__c 
                                         ORDER BY Id ASC ];
        
        theTrendsList    = [SELECT 
                                         Square_Feet__c, Seats__c, Headcount__c, GAAP_Ann_All_in_Cost_US__c, 
                                         TW_FY_Summary__c, MonthVal__c
                                         
                                         FROM TW_Monthly_Trend__c 
                                         WHERE MonthVal__c = 1 
                                           OR MonthVal__c = 12 
                                           OR MonthVal__c = :currMonth 
                                         ORDER BY TW_FY_Summary__c ASC ];
        
        for( TW_Monthly_Trend__c z : theTrendsList )
        { theTrends.add(z);  }
        
        //System.Debug( '-------------------------------------------------------------> TRENDS' + theTrends );
        
            for( TW_FY_Summary__c a : theSummaries )
            {
                
                for( TW_Monthly_Trend__c b : theTrends )
                {
                    while( a.Id == b.TW_FY_Summary__c )
                    {   
                        if( b.MonthVal__c == 1  ) 
                                { 
                                        a.FYB_SF__c = b.Square_Feet__c;
                                        a.FYB_Headcount__c = b.Headcount__c; 
                                        a.FYB_Seats__c = b.Seats__c;
                                        a.FYB_All_in_Cost__c = b.GAAP_Ann_All_in_Cost_US__c;
                                          if( b.MonthVal__c == currMonth && a.Fiscal_Year__c == currYear ) 
                                          {
                                            a.Current_Headcount__c = b.Headcount__c;
                                            a.Current_Seats__c = b.Seats__c;
                                          }
                                     theTrends.remove(b);          
                                }
                        
                        else if( b.MonthVal__c == 12 ) 
                                { 
                                        a.FYE_SF__c = b.Square_Feet__c;
                                        a.FYE_Headcount__c = b.Headcount__c; 
                                        a.FYE_Seats__c = b.Seats__c;
                                        a.FYE_All_in_Cost__c = b.GAAP_Ann_All_in_Cost_US__c;
                                          if( b.MonthVal__c == currMonth && a.Fiscal_Year__c == currYear ) 
                                          {
                                            a.Current_Headcount__c = b.Headcount__c;
                                            a.Current_Seats__c = b.Seats__c;
                                          }
                                        theTrends.remove(b); 
                                }
                                
            else if( b.MonthVal__c == currMonth && a.Fiscal_Year__c == currYear ) 
                                          {
                                            a.Current_Headcount__c = b.Headcount__c;
                                            a.Current_Seats__c = b.Seats__c;
                                            theTrends.remove(b);
                                          }
                                                             
                    }
                
                } //end trend loop
                            
            } //end summary loop
        
        update theSummaries ;
    }
}

 

Eugene NeimanEugene Neiman

Why don't you change this line

 

while( aSummary.Id == aTrend.TW_FY_Summary__c )

 

to 

 

if ( aSummary.Id == aTrend.TW_FY_Summary__c )

 

and while your at it, do this:

 

@future
public static void Run() {

Mohit KapoorMohit Kapoor

I am having issues with the call limits, so if and while wont matter much as the call for that also will be 1 per execution, and even after using @ future, am having controller limit exceed, its calling 200000, so am not what should I do..... am not sure here, maybe wrapper class but am not able to wrap this p to bring it in the 1000 call limit