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
UpendraUpendra 

Update a record after getting governor limit exceed exception

Hi Guys,

I have a custom object called "AsyncProcessJob", this object tracks all my async process in my system. I have to insert a record into the object then it executes some logic depending on some other field values. If the logic updates successfully then it sets the status of the record as "Success", if it fails then "Failed".

This logic fails when salesforce governor limit exceeds, in such cases it's unable to update the status as the execution stops completely.
 
So, I want to know is it possible to update the record after the governor limit exceeds. 
 
surya kanadhipatlasurya kanadhipatla
Upendra,
Yes/No :) ,we have Limits Class in salesforce which can be used to findout the current usage of these limits and margin left.

so you can code such that if current SOQLs used are already 100, don't perform any SOQL operation further and log a warning in the custom object, which may be possible in only certain scenarios.

Please see below links:

http://www.salesforcehacker.com/2014/05/that-which-doesnt-limit-us-makes-us_20.html
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_limits.htm
Justin J. YueJustin J. Yue
Governor limit runtime exception cannot be handled within Apex code, which means you cannot update your AsyncProcessJob object when you hit the limits. 
There're some design patterns can help you out. One simple solutions is to put the update of AsyncProcessJob into the @future method.
surya kanadhipatlasurya kanadhipatla
Upendra,
Salesforce will not allow any actions once governor limits are hit and Rolls back all the operations performed till that time in the same transaction.
so updating record after the governor limit exceeds is not possible but before hitting the limit is possible.

Work around is before hitting the limits i.e in case of SOQL 101 error, caputure the current usage (using Limits class) of limits before performing any SOQL operation and check if it reached 100 via apex code. once it reacched 100 can log a warning and stop performing any further SOQLs in certain scenarios only.
UpendraUpendra
@surya kanadhipatla, Thanks for the suggestion.

@Just In You,  Already using future job for process for doing the operation. You pointed out some design pattern, can you please mention here. 

 
Justin J. YueJustin J. Yue
Callback for asychronous process or promise in Apex. In short, you create a queueable apex job to process your business logic and chain another queueable apex to process the result. You can check https://success.salesforce.com/Sessions?eventId=a1Q3000000qQOd9EAG#/session/a2q3A000000LBdnQAG dreamforce session for more information about it