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
DrebinDrebin 

Apex CPU Time Limit PriceBookEntry update

Hi,

I have a class that updates the PriceBookEntry of all the products in each available currency, but I have an "Apex CPU Time Limit" error when I test the code.
public class Update_Product_Price implements Schedulable {
    
    public void Update_Price()
    {
        Decimal unitPriceEuro = 0;
        list<PriceBookEntry> listProductPrice = [SELECT Product2Id, UnitPrice, CurrencyIsoCode  FROM PriceBookEntry];
        list<Currency_Rate__c> listExchangeCurrency = [SELECT CHF__c, CNY__c, CZK__c, DKK__c, GBP__c, NOK__c, PLN__c, RUB__c, SEK__c, USD__c, ZAR__c  FROM Currency_Rate__c];
        
        PriceBookEntry pbe = new PriceBookEntry();
        Map<Id, PriceBookEntry> listOfPriceBookEntry = new Map<Id, PriceBookEntry>();
        
        for (PriceBookEntry StandardPrice : listProductPrice) {   
            
            unitPriceEuro = -1;
            if(StandardPrice.CurrencyIsoCode == 'EUR')
            {
                unitPriceEuro = StandardPrice.UnitPrice;
                System.debug('Currency code : ' +  StandardPrice.CurrencyIsoCode + ' Unit price : ' + StandardPrice.UnitPrice);
                System.debug(unitPriceEuro);                      
                
                for (PriceBookEntry StandardPrice_2 : listProductPrice) {
                    if(StandardPrice_2.CurrencyIsoCode == 'CHF' && unitPriceEuro != -1 || unitPriceEuro == null)
                    {     
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].CHF__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'CNY' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].CNY__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'CZK' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].CZK__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'DKK' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].DKK__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'GBP' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].GBP__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'NOK' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].NOK__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'PLN' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].PLN__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'RUB' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].RUB__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'SEK' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].SEK__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'USD' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].USD__c;
                    }
                    if(StandardPrice_2.CurrencyIsoCode == 'ZAR' && unitPriceEuro != -1)
                    {            
                        StandardPrice_2.UnitPrice = unitPriceEuro * listExchangeCurrency[0].ZAR__c;
                    }
                    pbe = StandardPrice_2;
                    listOfPriceBookEntry.put(pbe.Id, pbe);
                }
            }        
        }
        update listOfPriceBookEntry.values();
    }
    
    public void execute (SchedulableContext sc) {
        Update_Price();
    }
    
    
}

Can someone help me on this, I totally stuck right now.
Thanks
Rajesh Varma MudunuriRajesh Varma Mudunuri
Hi Drebin.
I can see that you have a nested for loop in your code. Avoid Nested for loops to improve code efficency.
  • Use soql for loops which uses querry locator to process records in batches.
  • Try using future method or queueable apex to raise your cpu time limts.
  • The larger the data set and more number of loops the more cpu time it takes.
Please go through the below url to find some best practices.
https://welkinsuite.com/blog/how-to-get-past-apex-cpu-limits/

Thanks
HARSHIL U PARIKHHARSHIL U PARIKH
I agree with Rajesh +

I would also recommened using Batch Apex and pass handful of PricebookEntry records at a time in Execute method. This is refresh the governor limit every time execute method executes and you will also get larger CPU time (60,000 MS) per every iteration.

Hope this helps!