You need to sign in to do that
Don't have an account?
Vikram Singh 157
HI ! Associated contacts not shown with the below triggers..
trigger Ass_cts on Contact (after insert, after undelete) {
set<id> acid = new set<id>();
if (trigger.isinsert||trigger.isundelete){
for (contact opp : trigger.new){
acid.add(opp.accountid);
}
list<account> acc = [select id, Number_of_contacts__c from account where id in : acid];
list<contact> con = [select id from contact where accountid in :acid];
for(account a : acc){
a.Number_of_contacts__c = con.size();
}
Update acc;
}
}
set<id> acid = new set<id>();
if (trigger.isinsert||trigger.isundelete){
for (contact opp : trigger.new){
acid.add(opp.accountid);
}
list<account> acc = [select id, Number_of_contacts__c from account where id in : acid];
list<contact> con = [select id from contact where accountid in :acid];
for(account a : acc){
a.Number_of_contacts__c = con.size();
}
Update acc;
}
}
set<id> acid = new set<id>();
List<Account> updateAccount = new List<Account>();
if (trigger.isinsert||trigger.isundelete){
for (contact opp : trigger.new){
acid.add(opp.accountid);
}
Integer contactCOunts = [Select Count() from contact where accountid : acid]
for(Account acc: acid){
acc.Number_of_contacts__c = contactCOunts;
updateAccount.add(acc);
}
Update updateAccount;
}
}
Hope this helps you out.
Mark it as best.
Thanks,
Shamsi.
All Answers
set<id> acid = new set<id>();
List<Account> updateAccount = new List<Account>();
if (trigger.isinsert||trigger.isundelete){
for (contact opp : trigger.new){
acid.add(opp.accountid);
}
Integer contactCOunts = [Select Count() from contact where accountid : acid]
for(Account acc: acid){
acc.Number_of_contacts__c = contactCOunts;
updateAccount.add(acc);
}
Update updateAccount;
}
}
Hope this helps you out.
Mark it as best.
Thanks,
Shamsi.
Let us know if this will help you
you will not get perfect count from "Shamsi 110" code if you will update multiple record by data loader because if you will update 2 record together then you will get 2 account Id in "acid". Below query will return count of both account
not for individual account contact count.