You need to sign in to do that
Don't have an account?
manjunath vivek
I am trying to count the number of contacts associated with the account.Code is saved , but not works.Please help me.
I am trying to count the number of contacts associated with the account.Code is saved , but not works.Please help me.
Trigger counting on contact(after update,after insert){
List<account> accids=New list<Account>();
For(contact con:trigger.new){
Accids.add(con.account);
}
For(contact con:trigger.new)
{
Account Acc;
Acc.count__c=accids.size();
}
}
Trigger counting on contact(after update,after insert){
List<account> accids=New list<Account>();
For(contact con:trigger.new){
Accids.add(con.account);
}
For(contact con:trigger.new)
{
Account Acc;
Acc.count__c=accids.size();
}
}
trigger countcontacts on Contact (after insert, after delete) {
Set<Id> aId = new Set<Id>();
if(Trigger.isInsert){
System.debug('Insert contact for trigger.new - '+Trigger.New);
for(Contact opp : Trigger.New){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
System.debug('Number is '+acc);
}
if(Trigger.isDelete){
System.debug('delete contact for trigger.old - '+Trigger.Old);
for(Contact opp : Trigger.old){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
System.debug('Number is '+acc);
}
All Answers
you can try this code it will give you the number of contact as well as id of all the contact If your problem solved then please mark as best answer or ask your doubt.
Thanks
Rishav
Why don't you use a roll-up summary field for this purpose?
Else, in the 2nd part of the trigger, you should update the Account record in the database, not just the memory structure (I think so).
Hope this helps,
Fred
We cannot create rollup summary for contacts on accounts. We don't get option for that.
With trigger, we can simplye achieve this with a field on account and a contact's trigger will update the field. Trigger should count values on insert , updated as well as delete.
Hope this helps!
Refere the below links.
http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
http://www.dhruvsoft.com/blog/displaying-total-number-of-contacts-for-each-account-in-salesforce/
However, you have to explicitly update the Account at the end.
Have a look at this link https://success.salesforce.com/answers?id=90630000000h3mNAAQ
Your code helps me a lot, but one more thing, I want to assign the size to custom field Count__C, can you please let me know, how to do that.
trigger countcontacts on Contact (after insert, after delete) {
Set<Id> aId = new Set<Id>();
if(Trigger.isInsert){
System.debug('Insert contact for trigger.new - '+Trigger.New);
for(Contact opp : Trigger.New){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
System.debug('Number is '+acc);
}
if(Trigger.isDelete){
System.debug('delete contact for trigger.old - '+Trigger.Old);
for(Contact opp : Trigger.old){
aId.add(opp.AccountId);
}
List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
List<Contact> con = [select id from contact where AccountId in :aId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
System.debug('Number is '+acc);
}