You need to sign in to do that
Don't have an account?

Trigger to count number of contacts associated with an account
Hi All,
I want to display number of contacts associated with an account using triggers.
for this I had created a lookup field like noofcontacts__c in account object. and Wrote code as
trigger numberofcontacts on contact(after insert, after update, after delete) {
Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
Set<Id> AcctIds = new Set<Id>();
List<schema.Account> AcctList = new List<schema.Account>();
List<schema.Contact> ConList = new List<schema.Contact>();
if(trigger.isInsert || trigger.isUPdate) {
for(Contact Con : trigger.New) {
if(String.isNotBlank(Con.AccountId)){
AcctIds.add(Con.AccountId);
}
}
}
if(trigger.isDelete) {
for(Contact Con : trigger.Old) {
AcctIds.add(Con.AccountId);
}
}
if(AcctIds.size() > 0){
ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
for(Contact Con : ConList) {
if(!AcctContactList.containsKey(Con.AccountId)){
AcctContactList.put(Con.AccountId, new List<Contact>());
}
AcctContactList.get(Con.AccountId).add(Con);
}
AcctList = [SELECT noofContacts__c FROM Account WHERE Id IN : AcctIds];
for(Account Acc : AcctList) {
List<schema.Contact> ContList = new List<schema.Contact>();
ContList = AcctContactList.get(Acc.Id);
Acc.Number_of_Contacts__c = ContList.size();
}
update AcctList;
}
}
I am getting an error as "Variable doesnot exist:id".
Kindly support and suggest.
Thanks
I want to display number of contacts associated with an account using triggers.
for this I had created a lookup field like noofcontacts__c in account object. and Wrote code as
trigger numberofcontacts on contact(after insert, after update, after delete) {
Map<Id, List<Contact>> AcctContactList = new Map<Id, List<Contact>>();
Set<Id> AcctIds = new Set<Id>();
List<schema.Account> AcctList = new List<schema.Account>();
List<schema.Contact> ConList = new List<schema.Contact>();
if(trigger.isInsert || trigger.isUPdate) {
for(Contact Con : trigger.New) {
if(String.isNotBlank(Con.AccountId)){
AcctIds.add(Con.AccountId);
}
}
}
if(trigger.isDelete) {
for(Contact Con : trigger.Old) {
AcctIds.add(Con.AccountId);
}
}
if(AcctIds.size() > 0){
ConList = [SELECT Id, AccountId FROM Contact WHERE AccountId IN : AcctIds];
for(Contact Con : ConList) {
if(!AcctContactList.containsKey(Con.AccountId)){
AcctContactList.put(Con.AccountId, new List<Contact>());
}
AcctContactList.get(Con.AccountId).add(Con);
}
AcctList = [SELECT noofContacts__c FROM Account WHERE Id IN : AcctIds];
for(Account Acc : AcctList) {
List<schema.Contact> ContList = new List<schema.Contact>();
ContList = AcctContactList.get(Acc.Id);
Acc.Number_of_Contacts__c = ContList.size();
}
update AcctList;
}
}
I am getting an error as "Variable doesnot exist:id".
Kindly support and suggest.
Thanks
http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
Thank you for response.
With the help of this link only,we wrote that code.
Iam getting same error now.
Kindly suggest and support.
Maps are super useful, but in this instance (always increment/decrement according to changes in child) you can't beat a simple aggregate SOQL query.
List<Contact> Contactlist=[select id,name,phone,accountid from contact where accountID In :trigger.new ];
map<id,integer> countMap=new map<id,integer>();
integer i=1;
for(contact c:contactlist)
{
if(countmap.containskey(c.accountid))
{
i=countmap.get(c.accountid)+1;
countmap.put(c.accountid,i);
}
else
{
countMap.put(c.accountid,i);
}
}
for(account a:trigger.new)
{
try{
if(countmap.containskey(a.id))
a.Contact_Count__c=countMap.get(a.id);
}
catch(exception e)
{
}
}
}
Thank you for response.
I had used your logic.But,I am getting same error as" Variable doesnot exist :id"
Awaiting for your response.
the below code works well for me.
As we have count total no of account associated with account .But i want to update the field when there is reparenting (Suppose i have created c1 on account a1 so count is 1 but i do some change i changed the account from a1 to a2 on same contact c1 and then after saved it i am getting the same count on count field so anyone can can suggest how can i achieve this?
Please find below code that will cover all the scenarios-
trigger ContactTrigger on Contact(after insert,after update, after delete){
if(trigger.isAfter){
Map<Id, Account> accounts = new Map<Id, Account>();
for(Contact con : Trigger.isdelete? Trigger.old: Trigger.new){
accounts.put(con.AccountId, new Account(Id=con.AccountId,NoOfContacts__c=0));
if(trigger.isUpdate){
for(Contact co: trigger.Old){
if(co.AccountId!=con.AccountId){
accounts.put(co.AccountId, new Account(Id=co.AccountId,NoOfContacts__c=0));
}
}
}
}
for(AggregateResult result : [Select count(id) cou, AccountId Account From Contact Where AccountId IN : accounts.keySet() Group By AccountId]){
accounts.put((Id)result.get('Account'), new Account(Id = (Id)result.get('Account'), NoOfContacts__c = Integer)result.get('cou')));
}
update accounts.values();
}
}
Hope the above code helps.
Thanks.
NumberofLocations__c is the Count of the number of contacts associated with the account.
Hope this code helps you!!
Thanks.
trigger countContacts on Contact (after insert, after update, after delete)
{
list<contact> conList = new list<contact>();
list<account> accList = new list<account>();
set<ID> accIDs = new set<ID>();
if(trigger.isInsert || trigger.isUpdate)
{
for(contact con : trigger.new)
{
if(String.isNotBlank(con.accountId))
{
accIds.add(con.accountId);
}
}
}
if(trigger.isDelete)
{
for(contact con : trigger.old)
{
accIDs.add(con.AccountId);
}
}
if(accIDs.size()>0)
{
conList = [select name, id, accountId from contact where accountId IN:accIDs];
accList = [select name, id , number_of_contacts__c from account where ID IN:accIDs];
}
for(account acc : accList)
{
acc.Number_of_contacts__c = conList.size();
}
update accList;
}
First you have to create one custom field, which you have used in your trigger for example field name as 'Number of Contacts' in the Account object.
After creating the custom field, just open any account record.
And in related contact list just create new contacts for that perticular account record.
once contacts are created or updated the account record also will be updated due to trigger and the number of contacts asscoiated with the account in the contact related list . the total number of count of contacts in the list will show as the value of that account field 'Number of contacts'
for example:
Yoy have created 3 contacts asccoiated with an account record
so the value of the custom field in account object record aslo Number of contact = 3 .
ContactTrigger :
trigger Count_No_Contacts on Contact (before insert, after insert, after update, after delete) {
ContactHandler handler = new ContactHandler();
if(Trigger.isAfter && Trigger.isInsert){
handler.countContacts(Trigger.new); }
if(Trigger.isAfter && Trigger.isUpdate) {
handler.countContacts(Trigger.new); }
if(Trigger.isAfter && Trigger.isDelete) {
handler.countContacts(Trigger.old); }
}
============================================
public class ContactHandler
{
List<Account> accountList = new List<Account>();
List<Account> accountListUpdated = new List<Account>();
public void countContacts(List<Contact> newList) {
for (Contact con : newList) {
system.debug('ContactsAccountID'+con.AccountId);
accountList = [SELECT Id,Name,TrailApp_DonTes__Number_of_Contacts__c, (SELECT Id, Name FROM Contacts)
FROM Account WHERE Id =: con.AccountId];
}
for (Account acc : accountList) {
acc.TrailApp_DonTes__Number_of_Contacts__c = acc.Contacts.size() ;
accountListUpdated.add(acc);
}
update accountListUpdated;
}
}
set<id> accid=new set<id>();
list<Account> accUpdate=new list<Account>();
for(contact con:trigger.new){
accid.add(con.accountid);
}
for(Account acc : [select id,no_of_contact__c,(select id from contacts) from account where id in :accid]){
acc.no_of_contact__c=acc.contacts.size();
accUpdate.add(acc);
}
update accUpdate;
}
@kushagra can u paste ur code, where u r getting the error
@Tharun kumar 22
your code works for single records.. can u please execute by entering 2 contacts with 2 Accounts on one go like below
List<Contact> cons = new List<Contact>();
contact con = new contact();
con.lastName = 'Test1';
con.AccountId ='0012w000002rWuRAAU';
cons.add(con);
contact cond= new contact();
cond.lastName = 'Test2';
cond.AccountId= 'AccountId1';
cons.add(cond);
contact cone= new contact();
cone.lastName = 'Test4';
cone.AccountId= 'AccountId2';
cons.add(cone);
insert cons;
Count Account with Different Industry(on account picklist fields is there)(e,g. For Industry Electronics we have 3 Accounts) using Map.
plz help me i am new in apex collection.
CODE-
trigger CountonAccountofcontact on Contact (after insert,after delete)
{
Set<Id> mysetid = new Set <Id>();
if(Trigger.isinsert)
{
System.debug('Insert new contact for trigger.new '+ Trigger.new);
for(Contact contac :trigger.new)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select Id,Number_Of_Contact_Count__c from Account where Id in : mysetid];
List<Contact> Con = [Select Id from Contact where Accountid in : mysetid];
for(Account A: Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('Number of count is ' + Acc);
}
if(Trigger.isdelete)
{
System.debug('The Delete Contact Name For Trigger.old'+ Trigger.Old);
for(Contact contac : Trigger.Old)
{
mysetid.add(contac.Accountid);
}
List<Account> Acc = [Select id,Number_Of_Contact_Count__c from Account where id in: mysetid];
List<Contact> Con = [Select id from Contact where Accountid in : mysetid];
for(Account A :Acc)
{
A.Number_Of_Contact_Count__c = Con.size();
}
update Acc;
System.debug('The Update number is '+ Acc);
}
}
NOTE- This code is running but I want for After Update event also and Plz Help me in that
This is the code written in very simple manner and can be easily understood
trigger Accountmapping on Contact (after insert, after update, after delete, after undelete) {
set<id>accid=new set<id>();
if(trigger.isinsert || trigger.isupdate || trigger.isundelete || contact.accountid!=null)
{
for(contact con:trigger.new)
{
accid.add(con.accountid);
}
list<account> acclist= new list<account>([select id,name,Number_of_Contacts__c,(select id, name from contacts) from account where id IN:accid ]);
for(account acc:acclist)
{
acc.Number_of_Contacts__c= acc.contacts.size();
}
update acclist;
}
}
if you find the answer as helpful, mark it as a solution and best answer so that other users with the same issue might be benefitted. Thank you
trigger TriggerContact on Contact (after insert, after update, after delete, after undelete) {
List<Contact> contactList = Trigger.isDelete ? Trigger.Old : Trigger.New;
Set<Id> accountIdSet = new Set<Id>();
for (Contact con : contactList) {
if (Trigger.isUpdate) {
if (con.AccountId != Trigger.oldMap.get(con.Id).AccountId) {
if (con.AccountId != null && Trigger.oldMap.get(con.Id).AccountId == null) {
accountIdSet.add(con.AccountId);
} else if (con.AccountId == null && Trigger.oldMap.get(con.Id).AccountId != null){
Id accountId = Trigger.oldMap.get(con.Id).AccountId;
accountIdSet.add(accountId);
} else {
accountIdSet.add(con.AccountId);
Id accountId = Trigger.oldMap.get(con.Id).AccountId;
accountIdSet.add(accountId);
}
}
} else if (con.AccountId != null) {
accountIdSet.add(con.AccountId);
}
}
if (accountIdSet.size() > 0) {
List<Account> accountList = [SELECT Id, Contact_Count__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIdSet];
if (accountList.size() > 0) {
for (Account acc : accountList) {
acc.Contact_Count__c = acc.Contacts.size();
}
update accountList;
}
}
}
if u find helpfull please give Thums up
trigger CountContactOnAcc on Contact (before insert,before update) {
if(trigger.Isupdate || trigger.IsInsert){
Set<id> AccIds = new Set<Id>();
List<Account> UpdtAccList = new List<Account>();
for(COntact c : trigger.new){
AccIds.add(c.accountid);
}
List<Account> accList = [select id,name,total_employee__c,(select id,name from contacts) from Account where id in : AccIds];
for(Account a :accLIst){
a.total_employee__c = a.contacts.size();
UpdtAccList.add(a);
}
update UpdtAccList;
}
}
Hi,
The below code is working for all listed operations:
- Inserting Contact with an Account
- Updating/Switching Accounts on Contacts ( this will update the count on both the Accounts )
- Removing Account from Contact
- Deleting Contact
- Undeleting Contact
Contact TriggerContact Trigger Handler
Thanks!
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
if(Trigger.isAfter)
{
Map<Id,Integer> mapAcctIdAndContSize=new Map<Id,Integer>();
List<Account> lstAct = new List<Account>();
if(Trigger.isInsert || Trigger.isUndelete || Trigger.isUpdate){
for( Contact ctNew : Trigger.New)
{
if(ctNew.AccountId != null )
{
List<Contact> lstContactNew = [Select Id,AccountId from Contact Where AccountId =: ctNew.AccountId];
if(lstContactNew != null)
{
mapAcctIdAndContSize.put(ctNew.AccountId, lstContactNew.size());
}
}
}
}
if( Trigger.isDelete || Trigger.isUpdate)
for( Contact ctOld : Trigger.Old)
{
if(ctOld.AccountId != null )
{
List<Contact> lstContactOld = [Select Id,AccountId from Contact Where AccountId =: ctOld.AccountId];
if(lstContactOld != null)
{
mapAcctIdAndContSize.put(ctOld.AccountId, lstContactOld.size());
}
}
}
for( Id actId : mapAcctIdAndContSize.keySet())
{
Account act = new Account();
act.Id = actId;
act.Count_Contact__c = mapAcctIdAndContSize.get(actId);
lstAct.add(act);
}
if(lstAct.size()>0)
{
update lstAct;
}
}
}
Trigger
trigger contactOntrigger on Contact (After insert , after update , After delete , After undelete) {
if(Trigger.isAfter){
if(Trigger.isInsert){
contactHandler.countContact(trigger.new);
}
else if(trigger.isUpdate){
contactHandler.countContact(trigger.old);
contactHandler.countContact(trigger.new);
}
else if(trigger.isdelete){
contactHandler.countContact(trigger.old);
}
else if(trigger.isUndelete){
contactHandler.countContact(trigger.new );
}
}
}
Handler Class
public class contactHandler {
public static void countContact(List<Contact> conlist )
{
set<Id> sid = new Set<id>();
for(contact c : conlist){
if(c.accountid!= null){
sid.add(c.AccountId);
}
}
Map< id , Integer> mMap = new Map<id , Integer>();
for(Account ac : [select name , (select lastName from contacts ) from account where id In:sid])
{
mMap.put(ac.Id , ac.contacts.size());
}
Set<id> s = new Set<id>(mMap.keyset());
list<Account> ac = new List<Account>();
for(Id ids : s){
ac.add(new Account(id = ids , KaranJain__countOfContact__c = mMap.get(ids)));
}
update ac;
}
this code work on any condition
trigger contactrigger on Contact (after insert,after delete,after update,after undelete)
{
list<account> acclist=[select id,name,(select AccountId,name from contacts ) from account];
for(integer i=0;i<acclist.size();i++)
{
acclist[i].No_of_related_contact__c=acclist[i].contacts.size();
}
update acclist;
}
your question is : display number of contacts associated with an account using triggers.
Whenever we are inserted contact in the related account, the no of contacts to be incremented,if you deleted then decremented. Both are related objects so we are using (after trigger) here.
after trigger we have four events: after insert,after update ,after delete, after undelete
after insert: After insertion of contact we have to update in account object with field name: total no of contacts
after update: After contact account namewe have to update (may be u have to change the accoun name) in account object with field name: total no of contacts
after delete: After seleteof contact we have to update in account object with field name: total no of contacts
after undelete: After undelete of contact we have to dat the contact in account object with field name: total no of contacts
first we have to create a field in account object:Total no of contact which is number field because count is number
below trigger write down:
trigger ContactTrigger on Contact (after insert,after update,after delete,after undelete) {
//Total_Contact_Counts__c
ContactTriggerHelper helper = new ContactTriggerHelper();
set<id> accIds = new set<id>();
if(trigger.isafter && (trigger.isinsert || trigger.isupdate || trigger.isundelete)){
for(contact con : Trigger.new){
if(con.accountid != null){
accIds.add(con.accountid);
}
}
}
if(trigger.isafter && (trigger.isupdate || trigger.isdelete)){
for(contact con : Trigger.old){
if(con.accountid != null){
accIds.add(con.accountid);
}
}
}
if(accIds != null){
helper.updatetotalcount(accIds);
}
}
public class ContactTriggerHelper {
public void updatetotalcount(set<id> accIds)
{
list<Account> lstaccs =[select id,name,Total_Contact_Counts__c,(select id, name from contacts) from Account where id in :accIds];
for(account acc : lstaccs)
{
acc.Total_Contact_Counts__c = acc.contacts.size();
}
update lstaccs;
}
}
it's work fine;
Prework: Make 'Count of Contacts' field with type as Number on Account Object.
trigger CountOfContactsRelatedToAccount on Contact (after insert, after delete, after Undelete) {
Set<Id> accId = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete){
for(Contact con : Trigger.new){
accId.add(con.AccountId);
}
}
if(Trigger.isDelete){
for(Contact con : Trigger.old){
accId.add(con.AccountId);
}
}
List<Account> accList = [Select Id,Name,Count_of_Contacts__c,(Select id from contacts) from Account where Id IN : accId];
for(Account acc :accList){
acc.Count_of_Contacts__c = acc.contacts.size();
}
update accList;
}
Your Problem is similar like this.
If you found this helpful, Kindly mark it as best answer so others can see this who want help.
Thanks,
Tushar
Handler:
==========
public class ContactHandler
{
List<Account> accountList = new List<Account>();
List<Account> accountListUpdated = new List<Account>();
public void countContacts(List<Contact> newList) {
for (Contact con : newList) {
system.debug('ContactsAccountID'+con.AccountId);
accountList = [SELECT Id,Name,Count_Of_Contacts_Associated__c, (SELECT Id, Name FROM Contacts)
FROM Account WHERE Id =: con.AccountId];
}
for (Account acc : accountList) {
acc.Count_Of_Contacts_Associated__c = acc.Contacts.size() ;
accountListUpdated.add(acc);
}
update accountListUpdated;
}
}
trigger:
=============
trigger ContactTrigger on Contact (before insert, after insert, after update, after delete) {
ContactHandler handler = new ContactHandler();
if(Trigger.isAfter && Trigger.isInsert){
handler.countContacts(Trigger.new); }
if(Trigger.isAfter && Trigger.isUpdate) {
handler.countContacts(Trigger.old);
handler.countContacts(Trigger.new);
}
if(Trigger.isAfter && Trigger.isDelete) {
handler.countContacts(Trigger.old); }
}
trigger TriggerContact on Contact (after insert, after update, after delete, after undelete) {
List<Contact> contactList = Trigger.isDelete ? Trigger.Old : Trigger.New;
Set<Id> accountIdSet = new Set<Id>();
for (Contact con : contactList) {
if (Trigger.isUpdate) {
if (con.AccountId != Trigger.oldMap.get(con.Id).AccountId) {
if (con.AccountId != null) accountIdSet.add(con.AccountId);
if (Trigger.oldMap.get(con.Id).AccountId != null) accountIdSet.add(Trigger.oldMap.get(con.Id).AccountId);
}
} else if (con.AccountId != null) {
accountIdSet.add(con.AccountId);
}
}
if (accountIdSet.size() > 0) {
List<Account> accountList = [SELECT Id, Contact_Count__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIdSet];
if (accountList.size() > 0) {
for (Account acc : accountList) {
acc.Contact_Count__c = acc.Contacts.size();
}
update accountList;
}
}
}
********** Trigger to count the number of contacts associated with an account. **********
trigger ConTrigger on Contact (after insert, after update, after undelete , after delete ) {
List<Contact> conlist = new List<Contact>();
if( Trigger.isInsert || Trigger.isUndelete )
{
conlist.addAll(Trigger.new);
}
if( Trigger.IsDelete)
{
conlist.addAll(Trigger.old);
}
if( Trigger.IsUpdate)
{
conlist.addAll(Trigger.new);
conlist.addAll(Trigger.old);
}
Set<id> accId = new Set<id>();
for(Contact con : conlist)
{
accId.add(con.accountId);
}
List<Account> accList = [Select Name,Number_Of_Contact__c, (select Name from Contacts) from Account WHERE Id IN :accId];
for(Account acc : accList)
{
acc.Number_Of_Contact__c = acc.contacts.size();
}
update accList;
}
If you found this helpful, Kindly mark it as the best answer so others can see this who want help.
Thanks,
Shivam Singh
Here is the complete code we can reduce the trigger by using class as well but i have give comple one shot code its working fine for me.
*****************write a trigger total number of contact on account ******************
//before to start create one custom field on account object - Number_of_Contacts__c
*/
trigger NoContactTrigger on Contact (after update, after insert, after delete, after Undelete) {
if(trigger.isInsert && trigger.isAfter){
Set<id> AccIds = new Set<Id>();
for(Contact con : trigger.new){
if(con.accountID != null) {
AccIds.add(con.accountid);
}
}
List<Account> accList = [SELECT Id,(SELECT id FROM contacts) FROM Account WHERE ID IN : AccIds];
for(Account acc :accList){
acc.Number_of_Contacts__c = acc.contacts.size();
}
update accList;
}
if(trigger.isUpdate && trigger.isAfter){
Set<id> AccIds = new Set<Id>();
for(Contact con : trigger.new){
if(con.accountID != null) {
AccIds.add(con.accountid);
}
}
List<Account> accList = [SELECT Id,(SELECT id FROM contacts) FROM Account WHERE ID IN : AccIds];
for(Account acc :accList){
acc.Number_of_Contacts__c = acc.contacts.size();
}
update accList;
}
if(trigger.isDelete && trigger.isAfter){
Set<id> AccIds = new Set<Id>();
for(Contact con : trigger.old){
if(con.accountID != null) {
AccIds.add(con.accountid);
}
}
List<Account> accList = [SELECT Id,(SELECT id FROM contacts) FROM Account WHERE ID IN : AccIds];
for(Account acc :accList){
acc.Number_of_Contacts__c = acc.contacts.size();
}
update accList;
}
if(trigger.isUndelete && trigger.isAfter){
Set<id> AccIds = new Set<Id>();
for(Contact con : trigger.old){
if(con.accountID != null) {
AccIds.add(con.accountid);
}
}
List<Account> accList = [SELECT Id,(SELECT id FROM contacts) FROM Account WHERE ID IN : AccIds];
for(Account acc :accList){
acc.Number_of_Contacts__c = acc.contacts.size();
}
update accList;
}
}
public static void ConCountInAccount1(List<Contact> ConList, Map<Id,Contact> OldConMap)
{
Set<id> AccIds = New Set<id>();
for(Contact IUDU:ConList)//IUDU-Insert/UnDelete/Delete/Update
{
if(String.isNotBlank(IUDU.AccountId) && OldConMap==Null)
{ //OldMap=Null Means Here Insert or Undelete or Delete Operation Occuring.
AccIds.add(IUDU.accountId);
//Here Adding Inserted/Undeleted/Deleted(That Parameter Passing OldList Too In the Trigger Code) Con.AccountIds
//Work Around: Contact Inserting/Restoring/Removing
}
else if(String.isNotBlank(IUDU.AccountId) && IUDU.AccountId!=OldConMap.get(IUDU.id).accountId)
{
AccIds.add(IUDU.AccountId);
//Here adding Updated Con.accountIds(which is Not Blank)
//Work Around: Opened Old Record and Changing AccountName with New Account or another Account in the DataBase
// Or Reparenting Account Lookup Field
//So In Above Cases We need to add Both New And Old AccountIds
AccIds.add(OldConMap.get(IUDU.id).accountId);
}
else if(String.isBlank(IUDU.AccountId) && String.isNotBlank(OldConMap.get(IUDU.id).accountId))
{
AccIds.add(OldConMap.get(IUDU.id).accountId);
//Here Also Adding Updataed Con.AccountIds (New Value is Null and Old Value is Not Empty)
//Work Around: Opened Old Record and Changing AccountName with Empty/Null value /Removing AccoutName
// Then Adding Old AccountId of this Updating Contact(Decreased Con Count in that AccountID)
}
}
List<Account> AccList =[Select Id,No_of_Contacts__c,(Select id from Contacts) from Account WHERE Id IN :AccIds];
List<Account> UpdatedAccList = New List<Account>();
for(Account EA: AccList)
{
EA.No_of_Contacts__c=EA.Contacts.Size();
UpdatedAccList.add(EA);
}
Update UpdatedAccList;
}
Trigger::
if( Trigger.isAfter && (trigger.isInsert || trigger.isUndelete||trigger.isDelete))
{
ContactTriggerHandler.ConCountInAccount1(trigger.IsDelete ? trigger.old:trigger.new,null);
}
if( Trigger.isAfter && trigger.IsUpdate)
{
ContactTriggerHandler.ConCountInAccount1(trigger.new,trigger.oldMap);
}
{
if(trigger.isafter&&(trigger.isInsert||trigger.IsUnDelete))
{
ContactTriggerHandler.conCountInAccount(trigger.new,null);
}
if(Trigger.IsAfter&&(Trigger.IsUpdate||Trigger.IsDelete))
{
ContactTriggerHandler.conCountInAccount(Trigger.IsUpdate?Trigger.New:Trigger.Old,Trigger.OldMap);
}
}
public class ContactTriggerHandler
{//*Trigger to Show Up all Contacts associted with account
public static void conCountInAccount(List<Contact> conList,Map<Id,Contact> conOldMap)
{
Set<Id> allAccIds = New Set<Id>();
for(Contact EC:conList)
{
if(String.isNotBlank(EC.AccountId) && conOldMap==Null)//Insert || Undelete
{
allAccIds.add(EC.AccountId);//Here EC.AccountId from TRIGGER.NEW(Check Trigger)
System.debug('After Insert or Undelete ::New AccountIds Added Into allAccIds');
}
else
{
if(EC.AccountId!=conOldMap.get(EC.Id).AccountId)//Update
{
if(String.isNotBlank(EC.AccountId))//We Need to check here wheather the newly added accountId is null or Not
{
allAccIds.add(EC.AccountId);//Here EC.AccountId from TRIGGER.NEW(Check Trigger)
System.debug(String.isBlank(conOldMap.get(EC.Id).AccountId)?'After Update::Old AccountIds Null and New AccountIds Added Into allAccIds':'After Update::New AccountIds Added Into allAccIds');
}
if(String.isNotBlank(conOldMap.get(EC.Id).AccountId))//We Need to check here wheather old accountId is null or Not
{
allAccIds.add(conOldMap.get(EC.Id).AccountId);
System.debug(String.isBlank(EC.AccountId)?'After Update::New AccountIds Null and Old AccountIds Added Into allAccIds':'After Update Old AccountIds also Added Into allAccIds');
}
}
else//Delete
{
if(String.isNotBlank(EC.AccountId))//Here EC.AccountId from TRIGGER.OLD(Check Trigger)
{
allAccIds.add(EC.AccountId);
System.debug('After Delete::Deletable AccountIds Added Into allAccIds');
}
}
}
}
List<Account> UpdateAccList =[SELECT Id,Contacts__c,(SELECT Id from Contacts)FROM Account WHERE Id=:allAccIds];
for(Account EA:UpdateAccList)
{
EA.Contacts__c=EA.Contacts.size();
}
update UpdateAccList;
}
}