You need to sign in to do that
Don't have an account?
hylim1215
Please help check my trigger, im getting exclusive access to this record error
Hi,
I have a trigger on Contact that will trigger an update on Account as below:
my account trigger
when i do a batch import of 50k contacts, i kept getting this error 'unable to obtain exclusive access to this record' but when i import this error file again and it go through but still not all imported, still left a portion of contacts with that error, then i keep repeat import the error file.
is it due to the the trigger above?
Thanks.
I have a trigger on Contact that will trigger an update on Account as below:
trigger calAccountLifeCycleStatus on Contact (after insert, after update) { List<id> acc_ids = new List<id>(); List<Account> acc_toUpdate = new List<Account>(); if(trigger.isInsert){ for(Contact ctc : Trigger.new){ acc_ids.add(ctc.AccountId); } } if(acc_ids.size() > 0){ for(Account acc: [Select id, CalculateALC__c from Account where id in: acc_ids]){ acc.CalculateALC__c = true; acc_toUpdate.add(acc); } } if(acc_toUpdate.size() > 0){ update acc_toUpdate; } }then in my account i have this triggger -> class
my account trigger
trigger AccountLifeCycleUpdate on Account (before update) { AccountLifeCycleCal.calculate(trigger.New ,Trigger.NewMap, Trigger.OldMap); }my apex class
public without sharing class AccountLifeCycleCal{ public static void calculate( List<Account> newAcc, Map<ID, Account> newAccMap, Map<ID, Account> oldAccMap){ Map<Id, String> AccountStatusMap = new Map<Id, String>(); for(Account acc_trigger: newAcc){ if(acc_trigger.CalculateALC__c == true){ acc_ids.add(acc_trigger.id); } } for(Account acc: [select id, name, Status__c,(select id, contact_type__c from Contacts) from Account where Id IN :acc_ids]) { String AccountStatus = ''; for(Contact c : acc.contacts){ if(c.contact_type__c = 'A'){ AccountStatus = 'customer'; break; } } AccountStatusMap.put(acc.id, AccountStatus); } for(Account upd_acc: newAcc){ if(acc_ids.size() > 0){ upd_acc.Status__c = AccountStatusMap.get(upd_acc.id); } } } }
when i do a batch import of 50k contacts, i kept getting this error 'unable to obtain exclusive access to this record' but when i import this error file again and it go through but still not all imported, still left a portion of contacts with that error, then i keep repeat import the error file.
is it due to the the trigger above?
Thanks.
In one transaction you are basically trying to update the Account twice (one in contact trigger to make calculateALC__c = true and then in account trigger to update account status).
if possible try to do both these actions in contact trigger only.
Some common causes are -
1. Sharing Rules are being calculated.
2. A picklist value has been replaced and replacement is in progress.
3. A custom index creation/removal is in progress.
4. Most unlikely one - someone else is already editing the same record that you are trying to access at the same time.