How to solve the apex CPU Time limit Exceeded due to Process Builder?

Requirement Overview

In this post, I will be sharing a robust and proven way to mitigate this issue as we had already acquired this strategy to mitigate this issue. If you are already here, I assume you have already analyzed your logs and found out that your process builder is taking more time compared to other processes. If not please analyze your logs and then apply a fix.
If you will ask me which tool I used to analyze the log?
I used Apex Log Analyzer present in Visual Studio code which gives me a proper understanding of my CPU time usage. There is another way if you want to produce output without a log you can directly use System.assert(false, Limit.getCpuTime()) at the end of the apex code.

Process builder is taking more time to execute, we have to find either way to add an extra time limit or optimize the logic in such a way that it consumes less time. There is a workaround for the first one suggested but the second will need more analysis and a decent amount of effort as some existing logic might break.

Solution Approach

Try to find the process builder logic where you see the possibility to move it to the apex invokable method and then adding a future method to it. I think you seem a bit confused but let me clear up your ambiguity.
We know that Salesforce imposes a limit on the CPU usage that can be consumed in a given transaction, which is approximately 10 seconds for Synchronous, and 60 seconds for Asynchronous. Here we added our code to asynchronous context and that adds on 6X time to do my work


Invocable and future

For raw code, go to the git repo here


In summary, the asynchronous context in Salesforce helps in preventing CPU timeout issues by executing resource-intensive tasks in the background, distributing CPU consumption, adhering to governor limits, and enhancing overall system performance and scalability

I hope you find this post useful! Catch you in the next content.
And thank you for being an awesome reader.
Share and grow together !!

If you have any doubts you can comment down below, I will try to reply ASAP.

Also keep visiting us, for more such content!

Adil Ansari

Md.Adil is a Senior Salesforce developer, a Computer Science graduate working on the Salesforce platform and providing customer solutions using as a Salesforce consultant/developer. He is the founder of SalesforceBitsandBytes blogging site. He shares unique real time posts on different Salesforce implementations, the source code available on this SalesforceBitsandBytes blog is fully tested in the development environment.

Leave a Reply

Your email address will not be published. Required fields are marked *