4

I am trying to create a reusable method which retries DML whenever record lock is detected.

However, I noticed that even if you set database.update( collection, **false**) and there is at least one locked record in the collection, each of the records will get marked as locked.

To replicate:

trigger LeadTrigger on Lead (before update) {
    lead[] l = new lead[]{};
    if ( trigger.new[0].email != '[email protected]' && true )
        for (integer i = 0; i < 10000000; i++)
           l.add (new lead(email='a'+i+'@as.com'));
}

Modify any lead record to kick into very long process and at the same time execute anonymously:

lead[] la = [select id, email from lead ];
for (lead l: la)
    l.email = '[email protected]';
system.debug( database.update( la, false ) );

This returns

USER_DEBUG [12]|DEBUG|(Database.SaveResult[getErrors=(Database.Error[getFields=();getMessage=unable to obtain exclusive access to this record or 3 records: 00Q28000001PrsEewS,00Q28000001PrszzAS,00Q28000001PrsGeaS;getStatusCode=UNABLE_TO_LOCK_ROW;]);getId=null;isSuccess=false;], Database.SaveResult[getErrors=(Database.Error[getFields=();getMessage=unable to obtain exclusive access to this record or 3 records: 00Q28000001PrsEewS,00Q28000001PrszzAS,00Q28000001PrsGeaS;getStatusCode=UNABLE_TO_LOCK_ROW;]);getId=null;isSuccess=false;], Database.SaveResult[getErrors=(Database.Error[getFields=();getMessage=unable to obtain exclusive access to this record or 3 records: {...}

My expectation is that all but that one record would get updated.

1
  • I have used this this flag and it works for validation errors on records. May be its not designed to work with locking error. Commented Feb 5, 2016 at 5:29

1 Answer 1

4

You have to use the FOR UPDATE query statement to attempt to acquire the lock before performing an update. Of course, at this point, your entire transaction would fail anyways and you'd have to retry your action again. The allOrNone flag won't circumvent row locks, governor limits, or unhandled exceptions. It can only handle validation errors.

You must log in to answer this question.

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