Timeline for How do you acquire an exclusive lock on an object within an Apex before insert trigger?
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 2, 2023 at 16:13 | vote | accept | kennyc | ||
Oct 30, 2023 at 19:25 | answer | added | cropredy | timeline score: 1 | |
Oct 30, 2023 at 18:26 | comment | added | David Reed |
It sounds to me like you need to FOR UPDATE on the N Accounts that have Billings in scope of the trigger execution. That gives you the scope of lock you need to uphold this invariant.
|
|
Oct 30, 2023 at 18:05 | comment | added | kennyc | @cropredy... That's kind of a neat idea. It's something I'm familiar with in the world I normally live in but didn't consider for Salesforce. Thanks for sharing. | |
Oct 30, 2023 at 18:03 | history | edited | kennyc | CC BY-SA 4.0 |
Added reference to Order of Execution Overview.
|
Oct 30, 2023 at 16:59 | comment | added | cropredy | why not use a Mutex__c custom object for this use case and do a FOR UPDATE on it? that will single-thread the multiple clients of the trigger | |
Oct 30, 2023 at 7:52 | comment | added | Phil W |
YMMV but performing SOQL without a WHERE clause is likely to cause performance issues or even governor limit issues. I suggest you do, indeed, include per-Account date range comparison, via the use of dynamically generated SOQL that uses FOR UPDATE on the related objects. There are SOQL query length limits, but here you can have at most 200 unique related objects since you are in a trigger.
|
|
Oct 30, 2023 at 4:55 | history | asked | kennyc | CC BY-SA 4.0 |