You need to sign in to do that
Don't have an account?
Rida J
Handler for this class
trigger getRelatedOpportunitiesCount on Opportunity (after insert,after update, after delete, after undelete){
Set<Id> accID = new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(Opportunity opp : Trigger.New){
accID.add(opp.AccountId);
}
updateAcc(accID);
}
else if(Trigger.isDelete){
for(Opportunity opp : Trigger.old){
accID.add(opp.AccountId);
}
updateAcc(accID);
}
private void updateAcc(Set<Id> accIds){
List<Account> accList = [select id, Close_Won_Count__c from Account where Id in :accIds];
List<Opportunity> oppsList = [select id from Opportunity where AccountId in :accIds and StageName='Closed Won'];
for(Account a : accList){
a.Close_Won_Count__c= oppsList.size();
}
update accList;
}
}
This is my code, could anyone tell me how to write the trigger handler for this?
Set<Id> accID = new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
for(Opportunity opp : Trigger.New){
accID.add(opp.AccountId);
}
updateAcc(accID);
}
else if(Trigger.isDelete){
for(Opportunity opp : Trigger.old){
accID.add(opp.AccountId);
}
updateAcc(accID);
}
private void updateAcc(Set<Id> accIds){
List<Account> accList = [select id, Close_Won_Count__c from Account where Id in :accIds];
List<Opportunity> oppsList = [select id from Opportunity where AccountId in :accIds and StageName='Closed Won'];
for(Account a : accList){
a.Close_Won_Count__c= oppsList.size();
}
update accList;
}
}
This is my code, could anyone tell me how to write the trigger handler for this?
Same quetion, answered here
https://dfc-org-production.my.salesforce.com/chatteranswers/ChatterAnswersQuestionSingleItem.apexp?id=9062I000000R4lH
THanks!!
and an updated trigger to call the appropriate method in the handler class for each trigger event:
With this updated trigger, the Close_Won_Count__c field on the related Account records will be updated whenever an Opportunity record is inserted, updated, deleted, or undeleted.
Hope it helps.
Enjoy!