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
TehNrdTehNrd 

Rollup Summary fields, @future, and UNABLE_TO_LOCK_ROW error

Here is the situation. I have a trigger on an opportunity that calls an asynchronous @future method which runs some logic and updates the opportunity. I also have several roll-up summary fields on the opportunity. When I run on updated on on OpportunityLineItem this executes the Opportunity trigger and queues up the @future method  which is throwing this error a lot: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record.


According to the documentation here, http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_concepts_core_data_objects.htm , it says transactions that included a summary field (I assume it means roll up summary field) are especially prone to deadlocks. This makes me think that roll-up summary fields are also an asynchronous action but I am still looking for confirmation. If you know please respond.

 

UPDATE: Documentation here, http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm , makes it sound like roll-up summary fields are a synchronous operation. I'm confused. I think roll-up summary calculation may be synchronous if you edit the parent and asynchronous if you edit a child?

 

This is my theory. Trigger on opp fires and queues up both the @future method and the roll-up summary recalculation. Both are asynchronous (assumption) and they are both trying to update the opp at the same time. So how to solve this problem? Below is one possible idea but I have no idea if it would even work.

 

 

try{
	update oppsToUpdate;
}catch(Exception e1){
	if(e1.getMessage().contains('UNABLE_TO_LOCK_ROW')){
		try{
			update oppsToUpdate;
		}catch(Exception e2){
			if(e2.getMessage().contains('UNABLE_TO_LOCK_ROW')){
				try{
					update oppsToUpdate;
				}catch(Exception e3){
					//error handing after 3 failed updated attempts
				}
			}
		}
	}else{
		//other error handling
	}
}

 

This would attempt to updated the opp 3 separate times if the error returned is unable to lock row. Some unknowns here that hopefully someone with direct saleforce.com knowledge can address is are these three updated statements truly unique and separate update transactions or will the record being updated appear locked for the entire duration of the class execution? The updates would also me milliseconds apart and I don't know if the delay would be enough for the record to unlock.

 

If anyone has suggestions for this tricky problem I am all ears.

 

Thanks,

Jason

Andrew2XAndrew2X

Sorry I have nothing to contribute, but dude, I feel your pain. I recently started getting this error on our application. There's precious little information in the documentation as far is working out a solution. I am about to try your suggestion of attempting to update the locked record three times, but since I can't reproduce the error--It's happening to one of our customers--I'll just have to wait and see if it fixes it for them.

 

I'll let you know the result.

TehNrdTehNrd

Others have noted this issue as well and it may have nothing to do with roll-up summary fields but rather something to do with master child relationships.

 

@mtclimber, the first time I noticed this was on Tuesday but this was a brand new apex class so I can't speak as to whether this behavior is new or not.

 

To paraphrase first post I perform an updated on a OpportunityLineItem (usually batch of 200) this executes the Opportunity trigger and queues up a @future method  which then tries to updated the opp and is throwing this error a lot: UNABLE_TO_LOCK_ROW.

 

My attempt above with multiple DML attempts did seem to help a little but did not completely prevent the error from occurring. It would be extremely helpful if the error messaged indicated what two transactions are trying to perform the updated at the same time.

 

-Jason

 

 

Andrew2XAndrew2X

Hi Jason,

 

My case does not involve roll-up summary fields. It's a straight upsert to a String field. The user clicks a link on a VF page, and a field gets updated, so it's a bit of a mystery why this error was occurring--there is no attempt to update this field multiple times, and the record is user-specific (only the Owner of the record can update it).

 

In any case, trying three times to update the field seems to have done the trick because the error hasn't appeared again--although it may have been something that Salesforce themselves fixed, and this change I made doesn't have any effect? Not sure.

 

Andrew

thecrmninjathecrmninja

Did you ever find the culprit with this issue?  I'm encountering the same problem.  I'm wondering if the Master in the master Detail is possible being edited and locking all of its children.

Andrew2XAndrew2X

No this issue was never resolved. Just today one of our customers got this error for the first time. And the field involved is not part of a master-detail relationship. It's a simple update to a field. No tricks.