Activity Forums Salesforce® Discussions Can a future method conflict with a queueable method in Salesforce?

  • Kumar

    Member
    January 19, 2017 at 1:59 pm

    Hi Sushant,

    You will have to use the FOR UPDATE clause to avoid this scenario.

    In Apex, you use FOR UPDATE to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems. While an sObject record is locked, no other client or user is allowed to make updates either through code or the Salesforce user interface. The client locking the records can perform logic on the records and make updates with the guarantee that the locked records won’t be changed by another client during the lock period. The lock gets released when the transaction completes.

    To lock a set of sObject records in Apex, embed the keywords FOR UPDATE after any inline SOQL statement. For example, the following statement, in addition to querying for two accounts, also locks the accounts that are returned:

    Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
    Note: You can’t use the ORDER BY keywords in any SOQL query that uses locking. For more info, see avoiding deadlocks.

     

    • This reply was modified 7 years, 3 months ago by  Kumar.
    • This reply was modified 7 years, 3 months ago by  Kumar.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos