简体   繁体   中英

System.QueryException: Record Currently Unavailable

This exception usually happens when a batch is being run or alerts are coming into our Salesforce instance too quickly. When inserting a case, we try to lock down the contact and account associated with the case before inserting the case to prevent the 'UNABLE_TO_LOCK_ROW' exception from happening.

Here is the exact exception:

'System.QueryException: Record Currently Unavailable: The record you are attempting to edit, or one of its related records, is currently being modified by another user. Please try again.'

Class.Utility.DoCaseInsertion: line 98, column 1

I've done a lot of research on the 'UNABLE_TO_LOCK_ROW' exception and 'Record Currently Unavailable' exception and I can't seem to find a great solution to this issue.

What I've tried to accomplish is a loop to attempt the insert 10 times, but I'm still getting the 'Record Currently Unavailable' exception. Does anyone else have a suggestion for this?

Below is the code:

Public static void DoCaseInsertion(case myCase) {
    try
    {
        insert myCase;
    }
    catch (System.DmlException ex)
    {
        boolean repeat = true;
        integer cnt = 0;
        while (repeat && cnt < 10) 
        {   
            try
            {
                repeat = false;
                List<Contact> contactList = [select id from Contact where id =: myCase.ContactId for update]; // Added for related contact to overcome the 'UNABLE_TO_LOCK_ROW issues'
                List<Account> accountList = [select id from Account where id =: myCase.AccountId for update]; // Added for related account to overcome the 'UNABLE_TO_LOCK_ROW issues'
                insert myCase;
            }
            catch (System.DmlException e)
            {
                repeat = true;
                cnt++;
            }
        }
    }
}

This basically happens when there is a conflicting modification being done by other user/process on a particular record that you are trying to access. Mostly it will happen when anykind of batch process is running in the background and locked the particular record you are trying to access(in your case Account). To get rid of this problem, you would need to check if there are any scheduled apex classes running in the background on Accounts/Cases and see if there is anything you can do to optimize the code to avoid conflicting behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM