function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JO_DevJO_Dev 

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++; } } } }
Best Answer chosen by JO_Dev
Akhil AnilAkhil Anil
Hello Jo,

You don't have to lock the assocaited Contact and Account before inserting the Case. The locking issue occurs only when the record is updated and not when it's inserted. When you insert a record into system, you are basically pushing in a fresh new record and it's next to impossible for this record to have any conflicts with another user. The record locking for the Accounts and Contacts would have made sense had there been a Master-detail relationship between either of the objects with Cases. So, I think the code snippet that you have posted is not causing the issue. There must be an update operation happening for some record in the same transaction which you need to figure out and apply the FOR UPDATE keyword to the SOQL query associated with that update operation.

Hope that helps !

All Answers

JO_DevJO_Dev
Here is my formatted 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;
                // Added for related contact to overcome the 'UNABLE_TO_LOCK_ROW issues'
                List<Contact> contactList = [select id from Contact where id =: myCase.ContactId for update];
                // Added for related account to overcome the 'UNABLE_TO_LOCK_ROW issues'
                List<Account> accountList = [select id from Account where id =: myCase.AccountId for update];
                insert myCase;
            }
            catch (System.DmlException e)
            {
                repeat = true; cnt++;
            }
        }
    }
}
Akhil AnilAkhil Anil
Hello Jo,

You don't have to lock the assocaited Contact and Account before inserting the Case. The locking issue occurs only when the record is updated and not when it's inserted. When you insert a record into system, you are basically pushing in a fresh new record and it's next to impossible for this record to have any conflicts with another user. The record locking for the Accounts and Contacts would have made sense had there been a Master-detail relationship between either of the objects with Cases. So, I think the code snippet that you have posted is not causing the issue. There must be an update operation happening for some record in the same transaction which you need to figure out and apply the FOR UPDATE keyword to the SOQL query associated with that update operation.

Hope that helps !
This was selected as the best answer
Mike Jones 19Mike Jones 19

Hi Jo,

You can actually get an UNABLE_TO_LOCK_ROW for the account or contact record while inserting a case when you add the account/contact ids to the case usually in the before insert case trigger. Even though you are not updating the account/contact it will still lock the account/contact when it gets related to the case (probably when the case gets created after the before insert case trigger runs). 

If you lock the account and contact by using the "for update" param, that will cause the Record currently unavailable errors if you are inserting 3 or more cases tied to the same account and contact. I mean if you insert 10 records and they all process at the same time, the first one will lock it and the other 9 will wait 5 (or maybe it's 10) seconds and try again. The next record will lock it, and the remaining 8 will fail without retrying. I know you can't catch the unable_to_lock_row exceptions, but i think you can catch the record currently unavailable errors. Have you verified the repeater code was actually running? You also need to be careful with adding the soql queries in the loop and hitting the 100 soql query limit.