You need to sign in to do that
Don't have an account?
Kiran Kumar 225
aused an unexpected exception, contact your administrator
Hi, I have the following code it is throwing
trigger ContactCount on Contact (after insert, after delete, after undelete,after update) {
set<string> AccountIds = new set<string>();
if(trigger.isinsert || trigger.Isundelete){
for(Contact Con: trigger.new){
AccountIds.add(con.AccountId);
system.debug('AccountID-->'+con.AccountId);
}
}
if(trigger.isDelete || trigger.isupdate){
system.debug('Delete');
for(Contact oldCon : trigger.old){
AccountIds.add(oldCon.AccountId);
system.debug('Delete---2');
}
}
List<Account> accountList = [select id,count__c,(select id from contacts) from Account Where ID IN:AccountIds];
list<account> accountupdate = new List<Account>();
for(Account acc : accountList){
system.debug('Contacts--->'+acc.contacts.size());
acc.count__c = acc.contacts.size();
//accountupdate.add(acc);
}
update accountList;
}
It is throwing unexpected exception error. Please let me konw where i did the mistake
trigger ContactCount on Contact (after insert, after delete, after undelete,after update) {
set<string> AccountIds = new set<string>();
if(trigger.isinsert || trigger.Isundelete){
for(Contact Con: trigger.new){
AccountIds.add(con.AccountId);
system.debug('AccountID-->'+con.AccountId);
}
}
if(trigger.isDelete || trigger.isupdate){
system.debug('Delete');
for(Contact oldCon : trigger.old){
AccountIds.add(oldCon.AccountId);
system.debug('Delete---2');
}
}
List<Account> accountList = [select id,count__c,(select id from contacts) from Account Where ID IN:AccountIds];
list<account> accountupdate = new List<Account>();
for(Account acc : accountList){
system.debug('Contacts--->'+acc.contacts.size());
acc.count__c = acc.contacts.size();
//accountupdate.add(acc);
}
update accountList;
}
It is throwing unexpected exception error. Please let me konw where i did the mistake
Please find the below trigger:
I already tested it in my DE and everything is working fine.
Please do let me know if it helps.
Regards,
Mahesh