Activity Forums Salesforce® Discussions How to resolve UNABLE TO LOCK ROW error in salesforce?

  • Surbhi

    Member
    June 10, 2016 at 5:23 am

    Hi Himanshu,

    The reason behind this error is: When you try to update a record and at the same time someone else is deleting that record.

    Please try to avoid these contradictory conditions for resolving this error.

    Thanks

  • Ajay Prakash

    Member
    February 18, 2018 at 4:52 am

    Reason and Resolution- 

    When you have multiple jobs running that update the same set of records, it's recommended to lock the records using the FOR UPDATE keyword. It will ensure that the record is locked and no other job can update the same record at that point in time. The conflicting job will then wait until the locking on the record is released and then perform the required operations. This will avoid conflicts between jobs that are running concurrently and will not throw up this exception.

    Locking Statements-

    In Apex, you can use FOR UPDATE to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems.No other client or user is allowed to make updates either through code or the Salesforce user interface if the record is locked.

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

     

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos