• devraj tomar
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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();	
}

 

  • May 19, 2012
  • Like
  • 0