+ Start a Discussion
nagendra kumar 21nagendra kumar 21 

System.DmlException: Update failed. First exception on row 0 with id 0011200001DyK2BAAV; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 200 records

Hi Everyone, 

I have trigger and class for a custom object, and in prod when data updated or loaded to the custom object I'm getting this error. can anyone please give a solution for this, I'm new to coding.

Error --
External E-Mail – Verify Email Address, Links, and Attachments

Apex script unhandled trigger exception by user/organization: 005A0000004JEGW/00DA0000000C46b

DivisionTrigger: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 0011200001DyK2BAAV; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 200 records: 0011200001DyK2BAAV,0011200001DyKFAAA3,0011200001DyLr8AAF,0011200001DyNIQAA3,0011200001DyNJHAA3,0011200001DyNdZAAV,0011200001DyNycAAF,0011200001DyPHUAA3,0011200001DyQeIAAV,0011200001DyS64AAF, ... (190 more): []

Class.DivisionTriggerHandler.calculateCount: line 124, column 1
Trigger.DivisionTrigger: line 8, column 1


Trigger - 


trigger DivisionTrigger on Division__c (after insert,after update,after delete) {
//if( Division__c.Account__c != Null || Division__c.Account__c == Null){
  if ( trigger.isAfter ) {
        if ( trigger.isInsert ) {
            DivisionTriggerHandler.calculateCount(trigger.new);
        } 
        else if ( trigger.isUpdate ) {
            DivisionTriggerHandler.calculateCount(trigger.new);
        }
        else if ( trigger.isDelete ) {
            DivisionTriggerHandler.calculateCount(trigger.old);
        }
    }
//}
}



Class - 

public without sharing class DivisionTriggerHandler {
    
    public static void calculateCount(List<Division__c> divisionList) {
        
        System.debug(divisionList);
        Set<Id> accIds = new Set<Id>();
        
        for(Division__c dv: divisionList)
        {
            accIds.add(dv.Account__c);
        }
        System.debug(accIds);
        
        if(accIds.size()>0)
        {
            Map<Id,Account> accMap = new Map<id, Account>([Select id, of_DIvisions__c ,of_Divisions_ever_having_AP__c ,of_Participated_Divisions__c ,of_Blacklisted_Divisions__c ,of_Awarded_Divisions__c  from Account where Id in :accIds]);
            
            List<Division__c> divList = [Select id,Account__c, RecordType.Name,First_Awarded_Date__c,Status__c,
                                         First_Invoice_Load_Date__c,First_Participation_Date__c from Division__c where RecordType.Name in ('C2FO Division','International Division') and Account__r.RecordType.Name ='Strategic Supplier' and Account__c in: accIds];
            
            Map<Id,List<Division__c>> AccDivMap = new Map<Id, List<Division__c>>();
            for(Division__c dv: divList)
            {
                if(AccDivMap.containsKey(dv.Account__c))
                {
                    List<Division__c> tempList =  AccDivMap.get(dv.Account__c);
                    tempList.add(dv);
                    AccDivMap.put(dv.Account__c, tempList);
                }
                else{
                    List<Division__c> tempList =  new List<Division__c>();
                    tempList.add(dv);
                    AccDivMap.put(dv.Account__c, tempList);
                }
            }
            for(Id accId : accMap.keySet())
            {
                
                Account acc2 =  accMap.get(accId);
                System.debug(acc2);
                acc2.of_Awarded_Divisions__c=0;
                acc2.of_Blacklisted_Divisions__c=0;
                acc2.of_DIvisions__c=0;
                acc2.of_Divisions_ever_having_AP__c=0;
                acc2.of_Participated_Divisions__c =0;
                accMap.put(accId,acc2);
                List<Division__c> tempDivisionlist = AccDivMap.get(accId);
                System.debug(tempDivisionlist);
                if(tempDivisionlist!=null)
                {
                    for(Division__c div : tempDivisionlist)  
                    {
                        System.debug(div.Status__c);
                        if(div.First_Awarded_Date__c!=null)
                        {
                            Account acc =  accMap.get(div.Account__c);
                            if(acc.of_Awarded_Divisions__c != null)
                            {
                                acc.of_Awarded_Divisions__c  = acc.of_Awarded_Divisions__c + 1;
                            }
                            else
                            {
                                acc.of_Awarded_Divisions__c = 1;
                            }
                            accMap.put(acc.Id,acc);
                        }
                        if(div.Status__c=='Blacklisted')
                        {
                            System.debug('Hello I am here');
                            Account acc =  accMap.get(div.Account__c);
                            if(acc.of_Blacklisted_Divisions__c  != null)
                            {
                                acc.of_Blacklisted_Divisions__c   = acc.of_Blacklisted_Divisions__c  + 1;
                            }
                            else
                            {
                                acc.of_Blacklisted_Divisions__c  = 1;
                            }
                            accMap.put(acc.Id,acc);
                        }
                        if(div.First_Awarded_Date__c ==null&&div.First_Participation_Date__c!=null)
                        {
                            Account acc =  accMap.get(div.Account__c);
                            if(acc.of_Participated_Divisions__c  != null)
                            {
                                acc.of_Participated_Divisions__c  = acc.of_Participated_Divisions__c + 1;
                            }
                            else
                            {
                                acc.of_Participated_Divisions__c = 1;
                            }
                            accMap.put(acc.Id,acc);
                        }
                        if(div.First_Invoice_Load_Date__c!=null)
                        {
                            Account acc =  accMap.get(div.Account__c);
                            if(acc.of_Divisions_ever_having_AP__c   != null)
                            {
                                acc.of_Divisions_ever_having_AP__c   = acc.of_Divisions_ever_having_AP__c  + 1;
                            }
                            else
                            {
                                acc.of_Divisions_ever_having_AP__c  = 1;
                            }
                            
                            accMap.put(acc.Id,acc);
                        } 
                        
                        Account acc =  accMap.get(div.Account__c);
                        if(acc.of_DIvisions__c   != null)
                        {
                            acc.of_DIvisions__c   = acc.of_DIvisions__c  + 1;
                        }
                        else
                        {
                            acc.of_DIvisions__c  = 1;
                        }
                        
                        accMap.put(acc.Id,acc);
                        
                        
                    }
                }
                
            }
            update accMap.values();
        }
        
    }
    
    
}
Best Answer chosen by nagendra kumar 21
Suraj MakandarSuraj Makandar
Hi  nagendra kumar 21,

Usually this error occurs  because the records in your scheduled job cause each other to be updated.

For example, if the job updates all Accounts, and you have automation on Accounts (triggers, WFRules, PBuilders) that, for example, update Contacts related to the Accounts, then, you have automation on Contacts that cause the related Account to be updated.

Link: https://success.salesforce.com/answers?id=9063A000000Dn6EQAS

Thanks,
Suraj
 

All Answers

Suraj MakandarSuraj Makandar
Hi  nagendra kumar 21,

Usually this error occurs  because the records in your scheduled job cause each other to be updated.

For example, if the job updates all Accounts, and you have automation on Accounts (triggers, WFRules, PBuilders) that, for example, update Contacts related to the Accounts, then, you have automation on Contacts that cause the related Account to be updated.

Link: https://success.salesforce.com/answers?id=9063A000000Dn6EQAS

Thanks,
Suraj
 
This was selected as the best answer
nagendra kumar 21nagendra kumar 21
@Suraj Thank you, yes we have a PB that is running based on the triggered update. Now we are removing trigger and going with diff business rule
thanks