Share this article

Improve this guide

Apex CPU Time Limit Exceeded: How to Fix This Error

Often, excess nest loops will cause this error

4 min. read

Updated onOctober 4, 2023

updated onOctober 4, 2023

Share this article

Improve this guide

Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more

Key notes

Some of our readers have recently reported that while writing and testing some codes, they encountered the apex CPU time limit exceeded error message.

Salesforcehas a timeout limit for transactions based onCPU usageand once reached this error message comes up. In this article, we will discuss how to fix this issue.

What causes Apex CPU time limit exceeded error?

What causes Apex CPU time limit exceeded error?

There can be several reasons why you are experiencing the apex CPU time limit exceeded error. Below are some of the potential reasons:

Since you now know some of the possible reasons behind the apex CPU time limit exceeded error, let us proceed to how you can fix this problem.

How can I fix the apex CPU time limit exceeded error?

How can I fix the apex CPU time limit exceeded error?

Before trying any of the advanced solutions provided in this article, whenever you encounter the error, try to perform the following preliminary checks:

Having tried the above checks, and the issue persists, you can now explore the advanced solutions provided below.

1. Set LoggingEnabled to False

The LoggingEnabled setting uses more processing power than is necessary for day-to-day operations. Changing the settings to false reduces the likelihood of receiving the apex CPU time limit exceeded error.

2. Use Salesforce Flow instead of Process Builder

Process builder processing is reported by many users to always trigger the apex CPU time limit error.

According to Salesforce, process builders and workflow should not be used for automation, but usingSalesforce Flowcan save users from CPU timeout errors.

3. Use Map-based queries

To avoid extra loops, use map-based queries. Below is an example of a map query used for loop to get record ID which increases the CPU time:

List accList=[Select Id,Name from Account limit 100]; Set setIds=new Set(); for(Account acc: accList){ //More CPU time for sure due to looping setIds.add(acc.id); }

Using Map query saves the CPU a great deal of time and it has proven to be one of the best solutions for the apex CPU time limit exceeded error.

Alternatively, you can explore our dedicated guide on thebest ways to limit the CPU usage of a processfor further information that can help you prevent the error next time.

And that’s it on how to fix the apex CPU timelimit issue. If you have any questions or suggestions on the best fix to apply for this issue, do not hesitate to use the comments section below.

More about the topics:salesforce

Vladimir Popescu

Being an artist his entire life while also playing handball at a professional level, Vladimir has also developed a passion for all things computer-related.

With an innate fascination for research and analysis, and realizing many other people share his passion for this subject, he delved into writing Windows-related articles, so other people can also benefit from the acquired information.

When not writing kick-ass articles, Vladimir likes to spend his time doing Crossfit and creating art.

User forum

0 messages

Sort by:LatestOldestMost Votes

Comment*

Name*

Email*

Commenting as.Not you?

Save information for future comments

Comment

Δ

Vladimir Popescu