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
sfadm sfadmsfadm sfadm 

System.LimitException: Apex CPU time limit exceeded issue


I have setup a new SalesForce environment and I have to transfer my Apex classes on it.

In order to deploy my code I use a test class with test methods.

A validation process is started when I try to deploy the changes.

This validation invokes all existing test methods and the environment throws the following exception:
System.LimitException: Apex CPU time limit exceeded

Oddly enough on the old environment there is no such exceptions

I'm sending you a method of my source code where the Apex CPU time limit exceeded is most likely to happen:
private List<LeadConversionContainer> checkByWebsite(Lead leadObj) {
        List<LeadConversionContainer> accountWebsites = new 
        String website = leadObj.Website;
        String leadId = leadObj.Id;
        List<Account> accWebsiteList = [SELECT Website, Url__c, Status__c FROM 
        if(website != null && !website.equals('') && accWebsiteList != null) {
            List<String> leadWebsites = extractWebsites(website);
            for (String leadUrl : leadWebsites) {
                String normWebsite = normalizeWebsite(leadUrl);
                String leadWebsite = normWebsite.trim();
                for(Account acc : accWebsiteList) {
                    String accountWebsite = acc.Url__c;
                    String accountId = acc.Id;
                    List<String> extractedAccountWebsite = 
                    for(String accWebsite :extractedAccountWebsite) {
                        String accountSite = normalizeWebsite(accWebsite);
                        if(accountSite != null) {
                            List<String> leadSplitWebsitesList = leadWebsite.split('[,;\\s]');
                            for(String leadSplitUrls :leadSplitWebsitesList) {
                                if(accountSite.equals(leadSplitUrls)) {
                                    boolean isAccountStatusActive = isAccountActive(acc);
                                    boolean isMerchantApplication = 
                                    if(isAccountStatusActive || isMerchantApplication) {
                                        String reasonToStopConversion = 'Lead Website coincide with 
                                        Account Website';
                                        LeadConversionContainer leadConvert = new 
                                        LeadConversionContainer(acc.Id, '', reasonToStopConversion);
        return accountWebsites;

Please advise how to solve the issue with my deploy and avoid such System.LimitException: Apex CPU time limit exceeded exception?
Best Answer chosen by sfadm sfadm
Amit Chaudhary 8Amit Chaudhary 8
Issue is coming because of nested for loop. Try to avoid nested For loop.

All Answers

Harshit Garg 6Harshit Garg 6

Here is an article from Salesforce on how you can address this Apex CPU timelimit exceeded error with best practices : (

Pls. mark as best answer if it answers your question.

Harshit Garg
Amit Chaudhary 8Amit Chaudhary 8
Issue is coming because of nested for loop. Try to avoid nested For loop.
This was selected as the best answer
sfadm sfadmsfadm sfadm
Hi Amit,

Could you please suggest an example code on what I need to modify in my code because although the reason of this limit exception is due to the nested for loop I do not see how to modify my code to avoid such exception.