You need to sign in to do that
Don't have an account?
Trigger for Contact count on Account giving Exception: Too many code statements: 200001
Hi,
I have trigger that rolls up total number of contacts on Account, This trigger was working fine in sandbox, But when moved to production it is throwing errors at the backend saying 'System.LimitException: Too many code statements: 200001'. My company production enviroment is integrated with 3rd party system. So everytime there is an update from external system this trigger is firing errors.
Below is my code:
trigger ContactRollUpOnAccounts on Contact (after delete, after insert, after update) { set<ID> AccountIds = new set<ID>(); set<ID> conIDs = new Set<ID>(); if(trigger.isInsert || trigger.isUpdate){ for(Contact c : trigger.new){ AccountIds.add(c.AccountId); } } if(trigger.isDelete){ for(Contact c : trigger.old){ AccountIds.add(c.AccountId); } } Map<ID, Contact> AccountMap = new Map<ID, Contact>([select id,AccountId from Contact where AccountId IN :AccountIds]); List<Account> AccountsToUpdate = new List<Account>(); for(Account a : [Select Id, Contacts__c from Account where Id IN :AccountIds ]){ for (Contact con : AccountMap.values()) { if (con.AccountId == a.Id) conIDs.add(con.Id); } if (a.Contacts__c!= conIDs.size()) a.Contacts__c= conIDs.size(); AccountsToUpdate.add(a); } update AccountsToUpdate; }
I understand the problem is because I am writing for loop within a for loop, but I am not aware of the workaround to avoid this.
Please help me with your valuiable thoughts
You need to change field name:
No_of_Contacts_in_SFDC__c = Contacts__c
::::code below:::::::::
trigger ContactsOnAccount on Contact (after insert, after delete,after undelete,after update) {
Set<Id> aId = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete){
for(Contact opp : Trigger.New){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.No_of_Contacts_in_SFDC__c=con.size();
}update acc;
}
if(Trigger.isDelete){
for(Contact opp : Trigger.old){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.No_of_Contacts_in_SFDC__c=con.size();
}update acc;
}
if(Trigger.isUpdate){
Set<Id> OldAId = new Set<Id>();
for(Contact opp : Trigger.new){
if(opp.AccountId != Trigger.oldMap.get(opp.id).AccountId || opp.Primary_Contact__c != Trigger.oldMap.get(opp.id).Primary_Contact__c)
aId.add(opp.AccountId);
OldAId.add(Trigger.oldMap.get(opp.id).AccountId);
}
if(!aId.isEmpty()){
//for new Accounts
List<Account> acc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:aId];
//For New Account Contacts
List<Contact> con = [select id from contact where AccountId in :aId];
/*
This is For Old Contacts Count
*/
//for Old Accounts
List<Account> Oldacc = [select id,No_of_Contacts_in_SFDC__c from Account where Id in:OldAId];
//For Old Account Contacts
List<Contact> OldCon = [select id from contact where AccountId in :OldAId];
//For New Accounts
for(Account a : acc){
a.No_of_Contacts_in_SFDC__c=con.size();
}update acc;
//For Old Accounts
for(Account a : Oldacc){
a.No_of_Contacts_in_SFDC__c=OldCon.size();
}update Oldacc;
}
}
}