[SalesForce] Would updating children records at the same time cause “UNABLE_TO_LOCK_ROW” error? How to avoid this

I have 3 objects with the below relationship:

  1. One Account(Master) Opportunity(Detail)
  2. Custom__c(Parent) (lookup on opportunity) Opportunity(Child)

Here's the scenario:

I have 1 Account A

Account A has children Opportunities: O1, O2, O3,…

O1, O2, O3 are child of Custom__c records C1,C2,C3 respectively.

I have a process builder on Custom__c which updates the related Opportunity.

When C1,C2,C3 are updated by their owners at the same time, it's like the owners are competing the access to the master Account record. And I kept getting error message of process builder : "unable to obtain exclusive access to this record or 1 records: ID of Master record},"

The relationship was designed by someone else. So I can't change.

The user experience is bad, how can I allow the users editing the records at the same time without receiving errors?

Update:

  1. Here I'm using a Process Builder, so I can't use "FOR UPDATE".
  2. My Process Builder is built upon "Custom__c" object which means if I turn the Process Builder into a trigger on Custom__c, my query would be on Opportunity object.

I will be using a query like this:

select Last_Submit_Date__c from Opportunity where id in : *** FOR UPDATE.

Would this query LOCK the parent record when it's a master-detail relationship?

Best Answer

One approach to try is to query for the Account records using the FOR UPDATE clause. Check out how Locking Statements work.

If you do a lot of heavy lifting in the Account trigger, this approach could cause timeouts. For example, if each save takes approximately 4 seconds, the race condition you describe (3 concurrent operations) might not resolve within the allotted 10 seconds. This situation will result in a QueryException. If you run into it you might want to spin up a @future or Queueable as a fallback.