You need to sign in to do that
Don't have an account?
pintoo rajput
Trigger to count number of Contacts associated with an Account ???
trigger should work for after insert , after update, after delete, after undelete
Thanks in advance
Thanks in advance
Thanks
Amit Chaudhary
Here you go,
Please mark this as the best answer if this helps
@Rakesh51 your code is not working when we are transferring the contact from one account to another account..
@Vasani Parth can you please help me to understand why we we are using Primary_Contact__c?
Map<Id, List<Contact>> mapAcctIdContactList = new Map<Id, List<Contact>>();
Map<Id, List<Contact>> mapAcctIdDelContactList = new Map<Id, List<Contact>>();
Set<Id> AcctIds = new Set<Id>();
List<Account> listAcc = new List<Account>();
if(trigger.isInsert) {
for(Contact Con : trigger.New) {
if(String.isNotBlank(Con.AccountId)) {
if(!mapAcctIdContactList.containsKey(Con.AccountId)) {
mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
}
mapAcctIdContactList.get(Con.AccountId).add(Con);
AcctIds.add(Con.AccountId);
}
}
}
if(trigger.isUpdate) {
for(Contact Con : trigger.New) {
if(String.isNotBlank(Con.AccountId) && Con.AccountId != trigger.oldMap.get(Con.Id).AccountId) {
if(!mapAcctIdContactList.containsKey(Con.AccountId)){
mapAcctIdContactList.put(Con.AccountId, new List<Contact>());
}
mapAcctIdContactList.get(Con.AccountId).add(Con);
AcctIds.add(Con.AccountId);
} else if(String.isBlank(Con.AccountId) && String.isNotBlank(trigger.oldMap.get(Con.Id).AccountId)) {
if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
}
mapAcctIdDelContactList.get(Con.AccountId).add(Con);
AcctIds.add(trigger.oldMap.get(Con.Id).AccountId);
}
}
}
if(trigger.isDelete) {
for(Contact Con : trigger.Old) {
if(String.isNotBlank(Con.AccountId)){
if(!mapAcctIdDelContactList.containsKey(Con.AccountId)){
mapAcctIdDelContactList.put(Con.AccountId, new List<Contact>());
}
mapAcctIdDelContactList.get(Con.AccountId).add(Con);
AcctIds.add(Con.AccountId);
}
}
}
if(AcctIds.size() > 0) {
listAcc = [SELECT Id, Contact_Count__c FROM Account WHERE Id IN : AcctIds];
for(Account acct : listAcc) {
Integer noOfConts = 0;
if(mapAcctIdContactList.containsKey(acct.Id)) {
noOfConts += mapAcctIdContactList.get(acct.Id).size();
}
if(mapAcctIdDelContactList.containsKey(acct.Id)) {
noOfConts -= mapAcctIdDelContactList.get(acct.Id).size();
}
acct.Contact_Count__c = acct.Contact_Count__c == null ? noOfConts : (acct.Contact_Count__c + noOfConts);
}
update listAcc;
}
}
would you happen to have an Apex test class for your trigger?
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
"Try this code."
trigger CountAccountRelatedcontacts on Contact (after insert, after delete) {
Set<Id> conId = new Set<Id>();
if(Trigger.isInsert){
for(Contact con : Trigger.New){
conId.add(con.AccountId);
}
List<Account> accList = [select id,count_of_contacts__c from Account where Id in:conId];
List<Contact> conList = [select id.lastname from contact where AccountId in :conId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
}
if(Trigger.isDelete){
for(Contact con : Trigger.old){
conId.add(con.AccountId);
}
List<Account> accList = [select id,count_of_contacts__c from Account where Id in:conId];
List<Contact> conList = [select id.lastname from contact where AccountId in :conId];
for(Account a : acc){
a.count_of_contacts__c=con.size();
}
update acc;
}
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi