System.LimitException: Apex CPU time limit exceeded
Salesforce has a timeout limit for transactions based on CPU usage. If transactions consume too much CPU time, Salesforce terminates the execution of long-running transactions. The maximum CPU time on the Salesforce servers is 10000 milliseconds for synchronous transactions or 60000 milliseconds for asynchronous transactions.
- Users could not see products in the cart after executing the Category Maintenance job.
- Users cannot execute the Update Product Constraints View and they receive an error - Apex CPU Time Limit Exceeded.
- Users encountered a "system.LimitException" error when they had more than 100000 products, defined a constraint rule with 98 condition products, and configured product scope as FieldSet. If there are a large number of fields defined in that Product Field Set, user encountered the error.
- Remove unnecessary code and loops.
- Use collections (Set, Hashmap, or List) efficiently.
- Avoid using a loop within another loop.
- SOQL query should be indexable.
- Avoid unnecessary re-initialization of variables.
- Use Aggregated SOQL (database operations are not counted in this limit, so avoid arithmetic operations in Apex).
- Check how much time workflow rules and process builders take.
- Define the following settings before running the Category Maintenance job.
APTS_UpdateViewUseDmlLimit = true
APTS_UpdateViewProductBatchSize = 50
- Optimize the constraint rule of ProductScope = Product Field Set to a lower number (this always breaks because of Salesforce limit).
- Avoid using many Custom Formula fields.
- To avoid "system.LimitException" error, create multiple constraint rules to reduce the number of condition products in a single constraint rule. Also, lower the number of fields in the Product Field Set.