dml – SOQL FOR UPDATE: Will related records also be locked?
The lock gets released when the transaction completes
This means when the transaction against Salesforce (e.g. async execution, REST API call, UI action such as save, quick action, call from LWC etc.) finishes. This may be long (in terms of processing) after the DML statement was executed. I.e. at the “end of the execution context”, as you put it.
I’m unsure if when I lock the queried records whether related records will also be locked.
Without FOR UPDATE, this depends on the relationship. If it is a master detail, or it is a lookup with a restrict deletion constraint (rather than set null), the referenced record is also locked.
With FOR UPDATE related records are also locked, as per the FOR UPDATE documentation:
When you perform a DML operation on one record, related records are locked in addition to the record in question.
You can find more detail about locking in the SOQL locking cheatsheet, a copy of which can be found here.
Read more here: Source link
