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
willjwillj 

UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record

Please someone help on how to resolve this error.  What's the best practice on handling it?  Appreciate response.  Thanks

 

------------

 

Apex script unhandled trigger exception by user/organization: 005U0000000NmC0/00DU0000000KA1N

 

trac_Attachment: execution of AfterInsert

 

caused by: System.DmlException: Update failed. First exception on row 0 with id 00QU0000005c15jMAA; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

 

Trigger.trac_Attachment: line 112, column 1

 

trigger trac_Attachment on Attachment (before delete, after insert, after update) {
	String BASE_URL = Url.getSalesforceBaseUrl().toExternalForm() + '/servlet/servlet.FileDownload?file=';
	String OWNER1 = 'Owner1_Sign.png';
	String OWNER2 = 'Owner2_Sign.png';
	String PG1 = 'Owner1_PG_Sign.png';
	String PG2 = 'Owner2_PG_Sign.png';
	Map<Id,Lead> leadMap = new Map<Id,Lead>();

	Set<Id> touchedLeadIds = new Set<Id>();
	for (Attachment lp : trigger.isdelete ? trigger.old : trigger.new) {
		touchedLeadIds.add(lp.parentId);
	}

	leadMap = new Map<Id,Lead>([SELECT IsConverted, DirtyFlag__c, Conga_Owner1_PG_Sign_URL__c, Conga_Owner2_PG_Sign_URL__c,
												    Conga_Owner1_Sign_URL__c, Conga_Owner2_Sign_URL__c
											 FROM Lead
											 WHERE Id IN :touchedLeadIds]);
	
	for(Lead i : leadMap.values()){
		if(i.IsConverted == false && i.DirtyFlag__c == true) i.DirtyFlag__c = false;
		else if(i.IsConverted == false) i.DirtyFlag__c = true;
	}	
	
	// find relevant opp ids
	Set<Id> oppIds = new Set<Id>();
	Set<Id> leadIds = new Set<Id>();
	for(Attachment a : trigger.isdelete ? trigger.old : trigger.new) {
		if (a.name == OWNER1 || a.name == OWNER2 || a.name == PG1 || a.name == PG2) {
			if (((String)a.parentId).startsWith('006')) {
				oppIds.add(a.parentId);
			} else if (((String)a.parentId).startsWith('00Q')) {
				leadIds.add(a.parentId);
			}
		}
	}
	
	
	if ((oppIds.size() + leadIds.size()) > 0) {
		// get opps and leads
		Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([SELECT Conga_Owner1_PG_Sign_URL__c, Conga_Owner2_PG_Sign_URL__c,
																	 Conga_Owner1_Sign_URL__c, Conga_Owner2_Sign_URL__c
															  FROM Opportunity
															  WHERE Id IN :oppIds]);
		
		Boolean isLead;
		for(Attachment a : trigger.isdelete ? trigger.old : trigger.new) {
			if (((String)a.parentId).startsWith('00Q') || ((String)a.parentId).startsWith('006')) {
				isLead = ((String)a.parentId).startsWith('00Q');
				system.debug('isLead is: ' + isLead + ', a.name is: ' + a.name);
			} // end parentid if
		} // end for
		update oppMap.values();
	}
	update leadMap.values();	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Abhay AroraAbhay Arora

Hi there is some kind of recussion goin on like

 

1.)Trigger is calling another trigger which is trying to update same record

2.)batch is calling another trigger which is trying to update same record

 

So just check all your active actions and see if any 2 of them are clashing with each other

 

In your case you are updating leads and opportunities which are having some triggers whichare trying to update same record

All Answers

Abhay AroraAbhay Arora

Hi there is some kind of recussion goin on like

 

1.)Trigger is calling another trigger which is trying to update same record

2.)batch is calling another trigger which is trying to update same record

 

So just check all your active actions and see if any 2 of them are clashing with each other

 

In your case you are updating leads and opportunities which are having some triggers whichare trying to update same record

This was selected as the best answer
devraj tomardevraj tomar
Tests that are started from the Salesforce user interface (including the Developer Console) run in parallel. In particular, data contention issues and UNABLE_TO_LOCK_ROW errors might occur in the following cases.
1. When tests update the same records at the same time—Updating the same records typically occurs when tests don’t create their own data and turn off data isolation to access the org’s data.
2. When a deadlock occurs in tests that are running in parallel and that try to create records with duplicate index field values—Test data is rolled back when a test method finishes execution. A deadlock occurs when two running tests are waiting for each other to roll back data, which happens if two tests insert records with the same unique index field values in different orders.

You can prevent these errors by turning off parallel test execution in the Salesforce user interface:
In Setup
enter Apex Test Execution in the Quick Find box
elect Apex Test Execution, then click Options....
In the Apex Test Execution Options dialog, select Disable Parallel Apex Testing and then click OK.