3

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 != 'asdf@asdf.com' && 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 = 'asdf@asdf.com';
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.

  • I have used this this flag and it works for validation errors on records. May be its not designed to work with locking error. – AtulRajguru9 Feb 5 '16 at 5:29
3

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.

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.