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
Emily PhillipsEmily Phillips 

Apex CPU time limit exceeded in Production but not sandbox

I have a scheduled flow that uses an apex action to use a simple invocable method. I'm simply passing the record Id from the flow to the apex class and deleting permission set assignments. The scheduled flow and class are able to process 300 records in a full copy sandbox environment but running into Apex CPU time limit exceeded in Production for about 100 records. 

Why is the same flow and class running into this error in production but not sandbox? 
VinayVinay (Salesforce Developers) 
Hi Emily,

It depends on various factors like number of records, execution time taken.  Best would be check debug logs and see which components is taken more time. You can figure out which code is consuming more time using analysis perspective in developer console log inspector.

https://help.salesforce.com/s/articleView?id=000387833&type=1
https://www.salesforceben.com/what-is-apex-cpu-time-limit-exceeded-how-do-you-solve-it/ 

Please mark as Best Answer if above information was helpful.

Thanks,
Emily PhillipsEmily Phillips
Hi Vinay, the apex class is very simple. It's about 20 lines and half of the class is soql and dml. I'm querying the permission assignments then deleting them so that shouldn't be counted against the cpu limit if I understand the limits correctly. The flow works fine in debug mode. I'm able to process records without an issue in debug. When the flow runs at its scheduled time, then I'm receiving the Apex CPU limit error. Any ideas as to why? It's my understanding that the flow runs an individual interview for each record in the batch. When I debug the flow, the average flow interview duration is about 2 to 5 seconds depending on the record.
VinayVinay (Salesforce Developers) 
Hi Emily,

I understand, however it might not be issue only with flow but also with dependent component that might invoke when flow is executed,  the only way to fix CPU limit error is checking logs and narrow down the scenerio and refactor login in component if it is taking too to execute. Kindly recheck if there are any other batch or component is being executig parallely.

Hope this helps...

Thanks,