You need to sign in to do that
Don't have an account?
Eric B
Trigger to count total contacts related to an account
Hi all,
i'am new to Apex and trying to write a trigger that counts the number of contacts with phone number related to an account and populate it on a custom field on account (Contacts_With_Phone_Numbers__c). this is the code, the problem when add a new contact to the related list, the number of contacts field is not updated! any help please!?
trigger accountsRelatedContacts on Contact (After insert,after update){
// create a set of contact Ids to querry against
List<Contact> contactsWithPhone = new List<Contact>();
Set<Id> accIds = new Set<Id>();
for (Contact con : contactsWithPhone){
accIds.add(con.AccountId);
}
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c,
(Select Id from Contacts)
From Account
Where Id in :accIds];
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
}
Update accountsRelatedContacts;
}
Thanks
i'am new to Apex and trying to write a trigger that counts the number of contacts with phone number related to an account and populate it on a custom field on account (Contacts_With_Phone_Numbers__c). this is the code, the problem when add a new contact to the related list, the number of contacts field is not updated! any help please!?
trigger accountsRelatedContacts on Contact (After insert,after update){
// create a set of contact Ids to querry against
List<Contact> contactsWithPhone = new List<Contact>();
Set<Id> accIds = new Set<Id>();
for (Contact con : contactsWithPhone){
accIds.add(con.AccountId);
}
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c,
(Select Id from Contacts)
From Account
Where Id in :accIds];
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
}
Update accountsRelatedContacts;
}
Thanks
Your trigger should be like below:
trigger accountsRelatedContacts on Contact (after insert,after update,after delete){
// create a set of contact Ids to querry against
Set<Id> accIds = new Set<Id>();
List<Account> accListtoUpdate = new List<Account>();
if(Trigger.isInsert){
for(Contact con : trigger.new){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isUpdate){
for(Contact con : trigger.new){
if(con.Phone != null && con.Phone != Trigger.oldMap.get(con.Id).Phone){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isDelete){
for(Contact con : trigger.old){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c,
(Select Id from Contacts WHERE Phone!=null)
From Account
Where Id in :accIds];
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
accListtoUpdate.add(c);
}
if(accListtoUpdate.size() > 0)
Update accountsRelatedContacts;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
All Answers
Are you suggesting that i should query all contact Ids such that:
trigger accountsRelatedContacts on Contact (After insert,after update){
// create a set of contact Ids to querry against
List<Contact> contactsWithPhone = [ Select Id from Contact];
Set<Id> accIds = new Set<Id>();
for (Contact con : contactsWithPhone){
accIds.add(con.AccountId);
}
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c,
(Select Id from Contacts)
From Account
Where Id in :accIds];
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
}
Update accountsRelatedContacts;
}
Your trigger should be like below:
trigger accountsRelatedContacts on Contact (after insert,after update,after delete){
// create a set of contact Ids to querry against
Set<Id> accIds = new Set<Id>();
List<Account> accListtoUpdate = new List<Account>();
if(Trigger.isInsert){
for(Contact con : trigger.new){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isUpdate){
for(Contact con : trigger.new){
if(con.Phone != null && con.Phone != Trigger.oldMap.get(con.Id).Phone){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isDelete){
for(Contact con : trigger.old){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c,
(Select Id from Contacts WHERE Phone!=null)
From Account
Where Id in :accIds];
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
accListtoUpdate.add(c);
}
if(accListtoUpdate.size() > 0)
Update accountsRelatedContacts;
}
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Maharajan.C
When i try to add a related contact to an existing account with other related contacts, i got this error
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,Name,Contacts_With_Phone_Numbers__c,
(Select Id from Contacts WHERE Phone!=null)
From Account
Where Id in :accIds];
Remove the Debug Line:39
Related contacts:
and account detail page:
Thanks
It is working fine now...
Use the below Updated one it will cover all the scenario's and Optimized one :
trigger accountsRelatedContacts on Contact (after insert,after update,after delete,after undelete){
// create a set of contact Ids to querry against
Set<Id> accIds = new Set<Id>();
List<Account> accListtoUpdate = new List<Account>();
if(Trigger.isInsert){
for(Contact con : trigger.new){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isUpdate){
for(Contact con : trigger.new){
if(con.Phone != Trigger.oldMap.get(con.Id).Phone || con.AccountId != Trigger.oldMap.get(con.Id).AccountId){
accIds.add(con.AccountId);
if(con.AccountId != Trigger.oldMap.get(con.Id).AccountId)
accIds.add(Trigger.oldMap.get(con.Id).AccountId);
}
}
}
else if(Trigger.isDelete){
for(Contact con : trigger.old){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isUndelete){
for(Contact con : trigger.new){
if(con.Phone != null){
accIds.add(con.AccountId);
}
}
}
if(!accIds.isEmpty()){
// querry for all the accounts with related contacts in the set of contacts
List<Account> accountsRelatedContacts = [Select Id,
Name,
Contacts_With_Phone_Numbers__c ,
(Select Id from Contacts WHERE Phone!=null)
From Account
Where Id in :accIds];
if(!accountsRelatedContacts.IsEmpty()){
// Loop through accounts to find matching contacts with phone numbers
for (Account c : accountsRelatedContacts){
List<Contact> s = c.Contacts;
//System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
c.Contacts_With_Phone_Numbers__c = s.size();
accListtoUpdate.add(c);
}
if(accListtoUpdate.size() > 0)
Update accountsRelatedContacts;
}
}
}
Thanks,
Maharajan.C