function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sai krishna 267sai krishna 267 

I have a requirement Account having opportinities and contacts my question is in Account object it has to print opportunities count and contact count by using the custom field

I have a requirement Account having opportinities and contacts my question is in Account object it has to print opportunities count and contact count by using the custom field and respective names of opportunites and contacts will also printed in  Account.whenever delete the contact or opportunity it will be automatically updated count & name Automatically give me any body propercode for these....
Best Answer chosen by sai krishna 267
RKSalesforceRKSalesforce
Hi Sai,

You need to write 2 triggers. One on Contact and One on Opportunity. Before using this code Please create two fields on Account Contact_Count__c, Opportunity_Count__c. 
Write similar code on Opportunity to get Opportunity count. This code will work in getting Contact Count.
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> accountIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact con: Trigger.New){
            if(con.AccountId  != null){
                accountIds.add(con.AccountId);
            }  
        }
    }
    If(Trigger.IsDelete){
        For(Contact con: Trigger.Old){
            accountIds.add(con.AccountId);
        }
    }
     
    List<Account> AccountListToUPdate = New List<Account>();
     
    For(Account act: [Select Id, Contact_Count__c, (Select ID FROM Contacts) FROM Account WHERE ID = :accountIds]){
		act.Contact_Count__c = act.Contacts.size();
		AccountListToUPdate.add(act);
    }
     
     
    try{
        Update AccountListToUPdate;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}

Please let me know if helped by marking Best Answer.

Regards,
Ramakant​

All Answers

RKSalesforceRKSalesforce
Hi Sai,

You need to write 2 triggers. One on Contact and One on Opportunity. Before using this code Please create two fields on Account Contact_Count__c, Opportunity_Count__c. 
Write similar code on Opportunity to get Opportunity count. This code will work in getting Contact Count.
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> accountIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact con: Trigger.New){
            if(con.AccountId  != null){
                accountIds.add(con.AccountId);
            }  
        }
    }
    If(Trigger.IsDelete){
        For(Contact con: Trigger.Old){
            accountIds.add(con.AccountId);
        }
    }
     
    List<Account> AccountListToUPdate = New List<Account>();
     
    For(Account act: [Select Id, Contact_Count__c, (Select ID FROM Contacts) FROM Account WHERE ID = :accountIds]){
		act.Contact_Count__c = act.Contacts.size();
		AccountListToUPdate.add(act);
    }
     
     
    try{
        Update AccountListToUPdate;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}

Please let me know if helped by marking Best Answer.

Regards,
Ramakant​
This was selected as the best answer
Balayesu ChilakalapudiBalayesu Chilakalapudi
Write a trigger on opportunity  to update account fields whenever a new opportunity created or deleted for an account.
 
trigger on opportunity{
List<Account> acclist=new List<Account>();
Set<Id> acidset=new Set<Id>();
//creating new opportunity
  if(trigger.insert && trigger.isafter){
     for(Opportunity op:trigger.new){
         acidset.add(op.accountId);
     }
   }
//deleting an opportunity
  if(trigger.insert && trigger.isbefore){
     for(Opportunity op:trigger.old){
         acidset.add(op.accountId);
     }
   }
 //update account with opportunity count
for(account acc:[select id,(select Id from opportunities) from account where id in:acidset]){
  acc.opportunity_count__c=acc.opportunities.size();
 acclist.add(acc);
} 
update acclist;
}



Similarly you can write contact trigger to update contact count on account.
sai krishna 267sai krishna 267
hi frds thank for u r reply but iam not only asking count i want to print the names of respect Opprtunities and contacts in same manner reply how to print the names like Opp_names__c,Con_names__c names will be printed saparated by comma(,) symbol this is requirement?
Balayesu ChilakalapudiBalayesu Chilakalapudi
In my previous code, change lines from 17 - 20 like this,
//update account with opportunity count and opportunity name
for(account acc:[select id,(select Id from opportunities) from account where id in:acidset]){
  acc.opportunity_count__c=acc.opportunities.size();
  String oppnames='';
   for(Opportunity op:acc.opportunities){
      oppnames+=op.name+',';
    }
  acc.opportunity_names__c=oppnames;
 acclist.add(acc);
}

 
RKSalesforceRKSalesforce
Please use below code:
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> accountIds = New List<ID>();
	List<ID> ContactIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact con: Trigger.New){
            if(con.AccountId  != null){
                accountIds.add(con.AccountId);
				ContactIds.add(con.Id);
            }  
        }
    }
    If(Trigger.IsDelete){
        For(Contact con: Trigger.Old){
            accountIds.add(con.AccountId);
			ContactIds.add(con.Id);
        }
    }
     
    List<Account> AccountListToUPdate = New List<Account>();
     
    For(Account act: [Select Id, Contact_Count__c, (Select ID, FirstName, LastName FROM Contacts) FROM Account WHERE ID = :accountIds]){
		for(Contact con:act.Contacts){
			act.Con_names__c  = con.FirstName +' '+ con.LastName +',';
		}
		AccountListToUPdate.add(act);
    }
     
     
    try{
        Update AccountListToUPdate;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}

Hope this will work.

Regards,
Ramakant​
sai krishna 267sai krishna 267
hi @ramakant  its working but names of the curent contact name only printed privous contact names not printed.like Account having 3 contacts only last contact name only printed.1st and sccound contact name is not printed.....check it once again

 
RKSalesforceRKSalesforce
Please use below code:
Trigger ContactRecordCount on Contact(After Insert, After Update, After Delete, After UnDelete){
    
    List<ID> accountIds = New List<ID>();
	List<ID> ContactIds = New List<ID>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Contact con: Trigger.New){
            if(con.AccountId  != null){
                accountIds.add(con.AccountId);
				ContactIds.add(con.Id);
            }  
        }
    }
    If(Trigger.IsDelete){
        For(Contact con: Trigger.Old){
            accountIds.add(con.AccountId);
			ContactIds.add(con.Id);
        }
    }
     
    List<Account> AccountListToUPdate = New List<Account>();
    String nameString = '';
    For(Account act: [Select Id, Contact_Count__c, (Select ID, FirstName, LastName FROM Contacts) FROM Account WHERE ID = :accountIds]){
		for(Contact con:act.Contacts){
			//act.Con_names__c  = con.FirstName +' '+ con.LastName +',';
			nameString = nameString + con.FirstName +' '+ con.LastName +',';
		}
		act.Con_names__c = nameString;
		AccountListToUPdate.add(act);
    }
     
     
    try{
        Update AccountListToUPdate;
    }
     Catch(Exception E){
        System.Debug('Error Message: ' + e.getMessage());
    }
}