Platform
Apex CPU time limit exceeded: find the expensive work
Diagnose Apex CPU failures with transaction-level measurements, bulk tests and targeted changes to loops, queries and automation.
A CPU exception can point to the final operation rather than the expensive one. Measure the transaction before rewriting that line.
Start with a debug log from a representative failure in a sandbox. Record the entry point, record count and active automation. A single-record test can hide a nested loop that becomes expensive when an import saves hundreds of records.
Salesforce’s governor reference lists a 10,000 ms synchronous CPU budget and 60,000 ms for common asynchronous execution. Scheduled Apex has synchronous limits. Database wait time and callout waiting are different from application-server CPU, so elapsed request time alone doesn’t diagnose this error.
Measure the operation you suspect
Use the Limits methods around a service call or processing block. This diagnostic fragment measures the current transaction; it doesn’t create a separate budget for the method.
Integer cpuBefore = Limits.getCpuTime();
// Run the operation under investigation here.
Integer cpuUsed = Limits.getCpuTime() - cpuBefore;
System.debug(LoggingLevel.WARN, 'Operation CPU ms: ' + cpuUsed);
System.debug(LoggingLevel.WARN,
'Transaction CPU: ' + Limits.getCpuTime() +
'/' + Limits.getLimitCpuTime());
Compare the same operation with small and representative inputs. Keep the logging narrow: dumping entire collections makes logs harder to interpret and can expose customer data. Include the record shape in your investigation notes, especially the number of related records per parent.
Fix repeated work first
If every Account loops over every Contact, build a map from Account ID to its Contacts once. If a helper repeatedly describes the same field or transforms the same string, calculate the value once at the appropriate scope. These changes reduce repeated work without changing the transaction boundary.
Also trace the save through its flows and triggers. An update that writes back an unchanged value may restart expensive automation. Compare old and new values before issuing that update, and test the full save path rather than timing one class in isolation.
Moving work to Queueable Apex is a design choice when the caller can accept delayed completion. It needs a job status, retry policy and error owner. Moving an inefficient loop unchanged leaves the underlying growth problem in place.
Prove the result with the difficult records
Test a normal save, a bulk save and a parent with unusually many children. Confirm both the business result and the CPU consumed. Keep headroom for other automation; passing just below the ceiling is a fragile acceptance criterion.
A governor-limit failure cannot be recovered with an ordinary catch block. Reduce the work or split it across intentional transaction boundaries before it reaches that limit. For a larger background workload, review the batch-processing option and make partial completion visible to its owner.