• Rida J
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
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?
 
  • January 06, 2023
  • Like
  • 0
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
  • January 05, 2023
  • Like
  • 0
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
  • January 05, 2023
  • Like
  • 0