0

Good day Everyone,

I know there are lots of questions which is similar with my question. But I still post this because I did not find any case which is similar with mine.

I am working with some task that will add multiple queauables at the same time. In my queauable, I queried multiple records. Below those queries, are Update DML then Insert DML(some of the data I put in my dml's are result from my query). One thing I am wondering at the moment is that why the error is pointing on the dml insert line. Can you please explain the possible reason.

1 Answer 1

2

UNABLE_TO_LOCK_ROW is highly fact-specific. You need to read through Salesforce Platform Record Locking and Concurrency, the linked Record Locking Cheat Sheet, and perhaps the Record-Level Locking section and surrounding chapters of Designing Record Access for Enterprise Scale to get a sense of what the structure of issues that cause this error looks like.

The short summary is that your code is performing operations in parallel that access the same records, either directly or indirectly, in a way that causes them to be locked. (The Record Locking Cheat Sheet provides a nice reference for events that cause locks).

Because Salesforce locks these records to serialize access to them, your code is encountering a species of synchronization problem. One parallel operation acquires the lock and holds it long enough that the other parallel operation throws an exception before the lock is released so that it can acquire it.

Solutions are, again, highly fact-specific. Because you mention multiple Queueables, candidate solutions would definitely include either (a) designing the Queueables so that they operate on entirely distinct sets of records (note that, per the Cheat Sheet, "distinct" here may include "having distinct parents" as well), or (b) designing the Queueables to run in sequence instead of in parallel.

3
  • +1 Honestly, I was just waiting to see your answer! Dec 21, 2018 at 2:18
  • @david Reed If it's ok, can you please give some example or article which explain how to run queauable in sequence instead of parallel ?
    – Hope
    Jan 9, 2019 at 2:13
  • Take a look at Chaining Jobs in this document. As far as a long-form explanation, the best one I know is Advanced Apex Programming by Dan Appleman. It's full of good Queueable patterns.
    – David Reed
    Jan 9, 2019 at 2:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .