Can anybody explain the UNABLE_TO_LOCK_ROW error?

  • Error Message:

    Developer script exception from Nicomatic : quotepotential : quotepotential: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0Q90000005PoxtEAC; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] Trigger.quotepotential: line 14, column 1

    Apex script unhandled trigger exception by user/organization: 00590000001A6jh/00D90000000bovq

    quotepotential: execution of AfterUpdate

    caused by: System.DmlException: Update failed. First exception on row 0 with id a0Q90000005PoxtEAC; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

    Trigger.quotepotential: line 14, column 1

    these mails are getting to my mail..........

    trigger quotepotential on Quote_Line_Item__c (after insert,after update) {
        Set<Id> quoteIds = new Set<Id>();
        List<Quote__c> quotes = new List<Quote__c>();
        for (Quote_Line_Item__c record: Trigger.new) {
            if (record.Quote1__c != null){
                quoteIds.add(record.Quote1__c);
            }
        }
        for(AggregateResult ar:[
                    SELECT 
                        Quote1__c, 
                        SUM(Max_Batch__c)sumMax 
                    FROM Quote_Line_Item__c 
                    WHERE Quote1__c=:quoteIds 
                    GROUP BY Quote1__c
                    ]) {
             quotes.add(new Quote__c(
                 Id=(Id) ar.get('Quote1__c'),
                 Potential__c = (Decimal) ar.get('sumMax')
                 ));
        }
        if (quotes.isempty() == false) {
            update quotes;
        }
    }
    

    It is happening everytime this piece of code is run?

    no only certain times

    The Mohith Kumars answer is most likely correct. Other users are updating a record that you are also trying to update

  • http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_locking_statements.htm

    ROW_LOCK issue is very common if you have multiple users updating the record at the same time .Or say a batch job is running and is updating a record and same record another trigger or code snippet (usually a future method) is updating.

    Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
    

    Using FOR UPDATE keyword helps to achieve a lock a client end to prevent this locking issues .

    Here is a small article on sharing model to avoid this granular locking

    https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-avoid-getting-lock-errors-in-my-organization-1327109108393&language=en_US

    http://blogs.developerforce.com/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html

    Also read on data skews which may be another cause .

    what can i do to get rid of this error

    If the issue was because other users are updating the record that your trigger is trying to update then the error should no longer be happening, assuming no one else is editing Quote__c records.

    I am also getting same problem, if i put FOR UPDATE is it enough to get rid out of this or need i put any other code...

    @Mohith Shrivastava do you mind to update the broken links please. Thanks!

    @Bahman.A yes let me look

    Fixed the docs links

    Mohith, I think it would be a good idea to explicitly mention that related records can also be locked as a side-effect, such as when there is a master details relationship (updating a child locks the parent)...

    Thanks to you, i have fixed this!

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM