You need to sign in to do that
Don't have an account?
Rida J
CloseWon count
I have an account object on which I should create a field named 'close won count', whenever the opportunities associated to the account stage name change from open or prospecting to Close Won then the count on account field 'close won count' should increment by 1. And if the stage name is close won and from this it changes to any other stage name the count on 'close won count' should get decremented.
Path to follow: use trigger handler for logic. trigger on opportunity. For condition check try to use old values as it is going to be on update trigger
Path to follow: use trigger handler for logic. trigger on opportunity. For condition check try to use old values as it is going to be on update trigger
try with below code.
Apex trigger:
Apex class:
If this helps, Please mark it as best answer.
Thanks!!
All Answers
Try with below code.
If this helps, Please mark it as best answer.
Thanks!!
handler?
try with below code.
Apex trigger:
Apex class:
If this helps, Please mark it as best answer.
Thanks!!
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;
}
}