3

I have a WSDL Apex service that consumes XML and creates a series of objects.

  1. XML received and parsed, queries for and finds a single account "Potential Customer" to relate potential customer contacts too. (I am aware this is a horrible practice in general, not to mention at scale, but not the topic of this question).

  2. Looks for contact with customer name, does not find it. Initiates create sequence relating up to the "Potential Customer" account. DML_BEGIN kicks off contact creation and triggers.

  3. CODE_UNIT_STARTED for before insert trigger kicks off for contact, CODE_UNIT_FINISHED.

  4. DML_END for Contact insert, next line is DmlException UNABLE_TO_LOCK_ROW.

If the user hitting the WSDL service sends two of these requests within the 14 second lock period for two customers, ex: John Smith and Bob Miller where they both relate up to the same Potential Customer account record, could this cause the UNABLE_TO_LOCK_ROW error since they relate up to the same account? Does locking of rows cascade down like that?

I know if a related record, such as tasks are being inserted, a lock is placed on the account, but do not know if this happens in reverse.

OR... perhaps the lock row error is not on the contact, but on the account, and I am erroneously believing it's on the contact because it happens after the contact before insert trigger completes?

Error:

10:25:23.504 (5504947775)|DML_END|[251]
10:25:23.504 (6505152170)|EXCEPTION_THROWN|[251]|System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

Related SF docs:

http://resources.docs.salesforce.com/194/0/en-us/sfdc/pdf/record_locking_cheatsheet.pdf

https://help.salesforce.com/articleView?id=000338933&type=1&mode=1

https://developer.salesforce.com/blogs/engineering/2014/07/record-locking-cheat-sheet.html

  • 1
    I'm not sure I follow the "know if this happens in reverse" bit. Is this a scenario where two contacts are being created and associated to the same already existing account? – Kris Goncalves Dec 26 '19 at 20:04
  • Correct. Two separate requests to the same WSDL service, at separate but potentially very close times (within seconds) creating two separate contacts that relate up the same account record. – S.B. Dec 26 '19 at 22:28
1

You're correct in that it's the account being locked based on an operation on a child object.

The 2nd link you provided and the example with Tasks/Accounts is basically the same scenario you're describing as the Account is the "parent" in both situations (Task and Contact).

The Account would get locked by the operation on the Contact being associated to it. Since they're separate transactions, you see that error message as they collide.

If you could find a way to get multiple requests in the same transaction batch, then you could avoid this situation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.