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
Steve MaxSteve Max 

How can I batch - I'm getting over governor limits

The following 2 classes are breaching governor limits.  Any ideas how I can reduce the number of rows returned at once while achieving the same result?

 

     private void RetrieveUserKPIData()
    {       
        //Retrieve the collection of USER_KPI Records
        //IMPLEMENT BATCH CREATION HERE AS REQUIRED
        for (   User_Month_KPI__c[] UserKPIs :
                                            [
                                            select  
                                                u.OwnerId,
                                                u.active_accounts_actual__c,
                                                u.APNrent_month_revenue_actual__c,
                                                u.revenue_new_accounts_actual__c,
                                                u.new_accounts_signed_actual__c,
                                                u.APN_Month__c,
                                                u.APN_Year__c,
                                                u.Month_Start_date__c,
                                                u.Month_End_date__c,
                                                u.avg_rev_per_account_actual__c,
                                                u.IPI_bookings_count__c,
                                                u.Id
                                            from    User_Month_KPI__c u
                                            where   u.APN_Year__c = :APNYear
                                            and     u.APN_Month__c = :APNMonth
                                            ] )
        {
            for (User_Month_KPI__c UserKPI : UserKPIs)
            {
                this.getCurrentSet().put(UserKPI.OwnerId,UserKPI);
                UserKPI.Month_Start_date__c = APNMonthStartDate;
                UserKPI.Month_End_date__c = APNMonthEndDate;
                
                Records++;
            }
        }
        
        CurrentSet=0;
    }


    public BulkAccountKPIs (UserKPICollectionClass pUKP)
    {
            pUKP.SetToStart();
            Map <Id,User_Month_KPI__c> KM;
            
            while ( (KM = pUKP.getCurrentSetIncrement()) !=  null)
            {
                Set<Id> UserIds = KM.keySet();
                
                for (Id Uid : UserIds)
                {
                    User_Month_KPI__c K = KM.get(Uid);
                    K.avg_rev_per_account_actual__c = 0;
                    K.Total_Number_Accounts__c = 0;
                    K.Total_Account_Revenue__c = 0;                    
                }
                
                for (Account[] accounts : [select
                                    t.Rating,
                                    t.OwnerId,
                                    t.Sales_Current_Month__c,
                                    t.CreatedDate,
                                    t.Active__c
                            from Account t
                            where t.OwnerId in :UserIds]
                            )
                {
                    for (Account account : accounts)
                    {
                        User_Month_KPI__c K = KM.get(account.OwnerId);
                        Processed++;
                        
                        K.Total_Number_Accounts__c++;                        
                        
                        if (account.Active__c == true)
                        {
                            TotalActive++;
                               TotalSalesCurrentMonth = TotalSalesCurrentMonth + account.Sales_Current_Month__c;
                            
                        }
            }
                }

                for (Id Uid : UserIds)
                {
                    User_Month_KPI__c K = KM.get(Uid);
                    if (K.Total_Number_Accounts__c != 0)
                    {
                        
                        K.avg_revenue_per_new_acc_actual__c = 0;
                        if (
                                   (K.revenue_new_accounts_actual__c > 0)
                                && (K.new_accounts_signed_actual__c >0)
                                && (K.revenue_new_accounts_actual__c != null)
                                && (K.new_accounts_signed_actual__c != null)
                            )
                         {
                            K.avg_revenue_per_new_acc_actual__c =
                                K.revenue_new_accounts_actual__c / K.new_accounts_signed_actual__c;
                         }
                         
                         K.avg_rev_per_account_actual__c = 0;
                        if (
                                    (K.APNrent_month_revenue_actual__c > 0)
                                && (K.APNrent_month_revenue_actual__c != null)
                            )
                         {
                            K.avg_rev_per_account_actual__c =
                                TotalSalesCurrentMonth / TotalActive;
                         }
                    }
                }
                
            }
    }

patrospatros

Right now, no really good way without hacking around with some AJAX-based code to loop over a large set of code.

 

But, you might want to hold tight for the Summer 09 release and look into the new "Batch Apex" functionality. Very nice built-in capability to handle just this sort of thing.

 

http://blog.sforce.com/sforce/2009/05/batch-apex-a-powerful-new-functionality-in-summer-09.html