List<id> TriggerList = new List<id>();
if(Trigger.isInsert)
{
List<id> TriggerList = new List<id>();
for(Opportunity Opp : Trigger.new)
{
TriggerList.add(Opp.AccountId);
}
List<Account> DML = New List<Account>();
List<Account> Ls = [select id,(select id from Opportunities) from Account where id in:TriggerList];
for(Account AccNew : ls)
{
Integer Num = AccNew.Opportunities.size();
AccNew.AccountNumber=String.valueOf(Num);
DML.add(AccNew);
}
update ls;
}
if(Trigger.isDelete)
{
for(Opportunity Opp : Trigger.old)
{
TriggerList.add(Opp.AccountId);
}
List<Account> DML = New List<Account>();
List<Account> Ls = [select id,(select id from Opportunities) from Account where id in:TriggerList];
for(Account act : Ls)
{
act.AccountNumber=String.valueOf(act.Opportunities.size());
DML.add(act);
}
update Ls;
}
}
Ideally the trigger should not be Account. To store the count of Contact on parent account, you should write a trigger on Contact and handle the insert, update and delete event (and undelete if that's in scope). For opportunity, you can create a rollup on Account to store the opportunity count. This should be staight forward configuration, no code required for this.
For opportunity you can create a roll-up filed on account which will automatically count the opporunity on account. And for contact you can try the following trigger :
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
You dnt need trigger for opportunity. You can use the roll-up summery field for this.
And for contact you can try below trigger with halper.
Trigger.
Handler
Please check below post for Trigger Framework
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html
Let us know if this will help you
All Answers
Try below sample code
Also refer below link.
http://www.infallibletechie.com/2013/09/trigger-to-count-number-of-contacts.html
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Sandhya
Ideally the trigger should not be Account. To store the count of Contact on parent account, you should write a trigger on Contact and handle the insert, update and delete event (and undelete if that's in scope). For opportunity, you can create a rollup on Account to store the opportunity count. This should be staight forward configuration, no code required for this.
Thanks,
Ravi
For opportunity you can create a roll-up filed on account which will automatically count the opporunity on account. And for contact you can try the following trigger :
trigger ContactTrigger on Contact (after insert,after update, after delete, after undelete) {
Set<Id> accountIds = new Set<Id>();
if(Trigger.isDelete){
for(Contact conRecord : Trigger.Old)
accountIds.add(conRecord.AccountId);
}else{
for(Contact conRecord : Trigger.New){
if(Trigger.isInsert || Trigger.isUndelete)
accountIds.add(conRecord.AccountId);
if(Trigger.isUpdate && conRecord.AccountId != Trigger.oldMap.get(conRecord.Id).AccountId){
accountIds.add(conRecord.AccountId);
accountIds.add(Trigger.oldMap.get(conRecord.Id).AccountId);
}
}
}
if(accountIds.size() > 0){
List<Account> accountList = [SELECT Roll_up_Contact__c, (SELECT Id FROM Contacts) FROM Account WHERE Id in: accountIds];
if(accountList.size() > 0){
for(Account accRecord : accountList)
accRecord.Roll_up_Contact__c = accRecord.Contacts.size();
UPDATE accountList;
}
}
}
WHERE Roll_up_Contact__c is a number field on account.
You dnt need trigger for opportunity. You can use the roll-up summery field for this.
And for contact you can try below trigger with halper.
Trigger.
Handler
Please check below post for Trigger Framework
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html
Let us know if this will help you