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
Abhishek Prasad 8Abhishek Prasad 8 

System.LimitException: Apex CPU time limit exceeded in Apex

Hi Friends,
I am stuck at this for quite some time. the error is given below:

System.LimitException: Apex CPU time limit exceeded
Error is in expression '{!Process}' in component <apex:commandButton> in page quoteforaccounts: Class.QuoteUtilities.setMaInfor: line 21, column 1

Here is my method: 
Please help me out
public static void setMaInfor(Quote__c eq, list<Quote_Si__c> qsList, list<Quote_Pr__c> quoteTerms, list<KA_Mar_Elect__c> elecMargins, list<KA_Mar_Gas__c> gasMargins) {
        String type;
        String product;
        Decimal margin;
        Quote__c equote;
        Decimal count = 0;
        Decimal runner = 0;
        Decimal bestRunner = 0;
        Decimal currentMultiplier = 0;
        Boolean firstRun = true;
        Opportunity op;
    
        //Get list of all quote term prices that are selected
        
        List<Quote_Pr__c> qtpList = quoteTerms;
        //Get list of all KA Margin Electricity records
        List<KA_Mar_Elect__c> marElec = elecMargins;
	
	// newBEIndex
        firstRun = true;
	for(KA_Mar_Elect__c me : marElec){
                        if(firstRun){
                            bestRunner = math.abs(me.margin_mwh__c - newBEMargin);
                            currentMultiplier = me.index_new__c;
                            firstRun = false;
                        }else{
                            runner = math.abs(me.margin_mwh__c - newBEMargin);
                            if(runner < bestRunner){
                                bestRunner = runner;
                                currentMultiplier = me.index_new__c;
                            }
                        }
                    }

}

 
Best Answer chosen by Abhishek Prasad 8
Abhishek Prasad 8Abhishek Prasad 8
Hi All,
Thanks for your kind suggestions. I did an extended analysis, and in the debug log, i saw that one of the trigger is being called infinitely. Used static flag to break the loop, and it solved my problem.

Thanks again guys.

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Abhishek Prasad,

Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, we'll shut them down as a long-running transaction.
We only count events that require application server CPU use. There are some things that use the app server CPU that we don't count, which are things beyond your control as a programmer. For example, the time spent in the database retrieving records won't count, nor will time spent waiting for a call out to return. You don’t control when your code needs compilation, so we don’t count that.

Please refer the below link for reference. I hope it will be helpful.

Please mark it as best answer if the information is informative.

Best Regards
Rahul Kumar

 
Abhishek Prasad 8Abhishek Prasad 8
@Rahul Kumar  
Thanks for your reply Rahul.
I have gone already gone through the page you have mentioned.
I have no for-inside-for loop in my code, nor i have soql inside for loop.
Still i am facing this issue.

Could you plz help me further?

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Abhishek,

Can you please post your full code ? Are your calling setMaInfor method from for loop ?
 
Abhishek Prasad 8Abhishek Prasad 8
Hi All,
Thanks for your kind suggestions. I did an extended analysis, and in the debug log, i saw that one of the trigger is being called infinitely. Used static flag to break the loop, and it solved my problem.

Thanks again guys.
This was selected as the best answer
Jonye KeeJonye Kee
The battery is about 45 inches by 29 inches Battery Backup Systems in San Jose (https://drummondelectric.com/battery-backup-systems/)​ with a thickness of 11 inches.
Leo BarnoskiLeo Barnoski
Do you ever use back up power generators in company (https://pandigroup.co.uk/) backup purpose?