You need to sign in to do that
Don't have an account?
sonali verma
Count Contacts
Hi friends
I need to count the number of contacts for each account. so I created a custom field (Number_of_Contacts_c) on the account object.
For new accounts I have written code and its working fine. Also when I delete a contact in detail page of account its workign fine.
There will be old accounts with no contacts records in detail page prior to this code. I am stuck here as how to update this accounts and show the number of contacts records
apex class code:
public class CountContacts_InAccount
{
public void CountContacts()
{
list<Account> noofcontacts = [select Id,Number_of_contacts__c from Account];
list<Contact> conforcontacts = [select ID,AccountID from Contact];
for (Account acct : noofcontacts )
{
list<ID> conIds = new list<ID>();
for (Contact con : conforcontacts )
{
if (con.AccountId == acct.Id)
{
conIds.add(con.Id);
acct.Number_of_contacts__c= conIds.size();
}
}
}
Database.Update(noofcontacts);
}
}
trigger code:
trigger trg_CountContacts on Contact (after Insert,after Update,after Delete,after Undelete)
{
CountContacts obj = new CountContacts();
if ((Trigger.IsInsert) || (Trigger.IsUndelete))
{
obj.CountContacts(Trigger.New);
}
if (Trigger.IsDelete)
{
obj.CountContacts(Trigger.Old);
}
}
Can any one psl elt me know:?
Regards
sonali verma
I need to count the number of contacts for each account. so I created a custom field (Number_of_Contacts_c) on the account object.
For new accounts I have written code and its working fine. Also when I delete a contact in detail page of account its workign fine.
There will be old accounts with no contacts records in detail page prior to this code. I am stuck here as how to update this accounts and show the number of contacts records
apex class code:
public class CountContacts_InAccount
{
public void CountContacts()
{
list<Account> noofcontacts = [select Id,Number_of_contacts__c from Account];
list<Contact> conforcontacts = [select ID,AccountID from Contact];
for (Account acct : noofcontacts )
{
list<ID> conIds = new list<ID>();
for (Contact con : conforcontacts )
{
if (con.AccountId == acct.Id)
{
conIds.add(con.Id);
acct.Number_of_contacts__c= conIds.size();
}
}
}
Database.Update(noofcontacts);
}
}
trigger code:
trigger trg_CountContacts on Contact (after Insert,after Update,after Delete,after Undelete)
{
CountContacts obj = new CountContacts();
if ((Trigger.IsInsert) || (Trigger.IsUndelete))
{
obj.CountContacts(Trigger.New);
}
if (Trigger.IsDelete)
{
obj.CountContacts(Trigger.Old);
}
}
Can any one psl elt me know:?
Regards
sonali verma
Try with below trigger your functionality will be achieved.
If this solves your Problem, Mark it as the Best Answer.
Thanks,
SFDC Beginner.
All Answers
Try with below code it will help !
Let me know if it helps!
Thanks
Manoj
Go to developer console and execute the code below and should update the number of contacts field on the account
In the new window, you can enter the code:
List<Account> list = [select Id, Number_of_Contacts__c, (select Id from Contacts) from Account];
for(Account a:list)
a.Number_of_Contacts_c = a.Contacts..size();
update list;
After this, all the old account will updated. Then the trigger will fire for all the new stuffs.
Try with below trigger your functionality will be achieved.
If this solves your Problem, Mark it as the Best Answer.
Thanks,
SFDC Beginner.
I modified your code. It works.
Pls let me know in real time , should we have a single method which performs all operations or we can go for two different methods in apex class:?
apex class code:
public class CountContacts
{
public void CountContacts(list<Contact> conID)
{
//INSERT. New contacts only
Set<ID> aID = new Set<ID>();
for(Contact con:conID)
{
aID.add(con.AccountID);
}
list<Account> acc = [select Id,Number_of_contacts__c from Account where ID IN :aID];
list<Contact> con = [select ID from Contact where AccountID IN :aID];
for(Account acn:acc)
{
acn.Number_of_contacts__c=con.Size();
}
Database.Update(acc);
// Substracting Contacts When deleted.
for(Contact con1:conID)
{
aID.add(con1.AccountID);
}
list<Account> a1 = [select Id,Number_of_contacts__c from Account where ID IN :aID];
list<Contact> con_1 = [select ID from Contact where AccountID IN :aID];
for(Account a:a1)
{
a.Number_of_contacts__c=con_1.Size();
}
Database.Update(a1);
} //method ends here
public void CountContacts(list<Contact> conID,Map<ID,Contact> conmapIds)
{
//first parameter is Trigger.New & second parameter is Trigger.OldMap
Set<ID> aId = new SET<ID>(); //for New AccountIDs
Set<ID> OldAId = new Set<ID>(); //for Old AccountIDs
//Map<ID,Contact> cmapid = [select AccountID from Contact where ID IN :conmapIds];
for(Contact opp : conID)
{
if (opp.AccountId != conmapIds.get(opp.id).AccountId)
{
aId.add(opp.AccountId);
OldAId.add(conmapIds.get(opp.id).AccountId);
}
}
if (!aId.isEmpty())
{
//for new Accounts
List<Account> acc = [select id,Number_of_contacts__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,Number_of_contacts__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.Number_of_contacts__c=con.size();
}
Database.Update(acc);
//For Old Accounts
for(Account a : Oldacc)
{
a.Number_of_contacts__c=OldCon.size();
}
Database.Update(Oldacc);
}
}//end of method
} // end of class
Trigger Code:
trigger trg_CountContacts on Contact (after Insert,after Update,after Delete,after Undelete)
{
CountContacts obj = new CountContacts();
if ((Trigger.IsInsert) || (Trigger.IsUndelete))
{
obj.CountContacts(Trigger.New);
}
if (Trigger.IsDelete)
{
obj.CountContacts(Trigger.Old);
}
if (Trigger.IsUpdate)
{
obj.CountContacts(Trigger.New,Trigger.OldMap);
}
}
Test Class:
@istest(SeeAllData=false)
private class CountContacts_Test
{
private static testmethod void validatetest()
{
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u = new User(Alias = 'sample', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='sampleLname', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='testnamesss@testorg.com');
system.runAs(u)
{
Account acn = new Account();
acn.Name='test1';
Database.Insert(acn);
Contact con = new Contact();
con.AccountID=acn.ID;
con.LastName='test3';
Database.Insert(con);
Contact oldcon=[select AccountID from Contact where ID=:con.ID];
Database.Delete(oldcon);
Database.Update(acn);
}
}
}
The code coveage is around 85%.
Thanks
Sonali verma