+ Start a Discussion

Please help check my trigger, im getting exclusive access to this record error


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>();
        for(Contact ctc : Trigger.new){

    if(acc_ids.size() > 0){
        for(Account acc: [Select id, CalculateALC__c from Account where id in: acc_ids]){
            acc.CalculateALC__c = true;
    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){
		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';
			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?



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.
Ankur Saini 9Ankur Saini 9
This is something temporary and would be fixed automatically in 10-15 minutes maximum.
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.
Gunnam RamGunnam Ram
Hi, check if there are any Workflows updating the same field on Account.