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
Rida JRida 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?
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Rida,

Same quetion, answered here
https://dfc-org-production.my.salesforce.com/chatteranswers/ChatterAnswersQuestionSingleItem.apexp?id=9062I000000R4lH

THanks!!
Dosbol TDosbol T
Hi there, please try this handler:
 
public class OpportunityTriggerHandler {
    public static void handleAfterInsert(List<Opportunity> newOpps) {
        Set<Id> accIds = new Set<Id>();
        for (Opportunity opp : newOpps) {
            accIds.add(opp.AccountId);
        }
        updateAcc(accIds);
    }
    public static void handleAfterUpdate(List<Opportunity> newOpps, Map<Id, Opportunity> oldOppsMap) {
        Set<Id> accIds = new Set<Id>();
        for (Opportunity opp : newOpps) {
            Opportunity oldOpp = oldOppsMap.get(opp.Id);
            if (opp.AccountId != oldOpp.AccountId) {
                accIds.add(opp.AccountId);
                accIds.add(oldOpp.AccountId);
            } else {
                accIds.add(opp.AccountId);
            }
        }
        updateAcc(accIds);
    }
    public static void handleAfterDelete(Map<Id, Opportunity> oldOppsMap) {
        Set<Id> accIds = new Set<Id>();
        for (Opportunity opp : oldOppsMap.values()) {
            accIds.add(opp.AccountId);
        }
        updateAcc(accIds);
    }
    public static void handleAfterUndelete(List<Opportunity> newOpps) {
        Set<Id> accIds = new Set<Id>();
        for (Opportunity opp : newOpps) {
            accIds.add(opp.AccountId);
        }
        updateAcc(accIds);
    }
    private static 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'];
        Map<Id, Account> accMap = new Map<Id, Account>();
        for (Account a : accList) {
            accMap.put(a.Id, a);
        }
        for (Opportunity opp : oppsList) {
            if (accMap.containsKey(opp.AccountId)) {
                Account a = accMap.get(opp.AccountId);
                a.Close_Won_Count__c++;
            }
        }
        update accMap.values();
    }
}
and an updated trigger to call the appropriate method in the handler class for each trigger event:
 
trigger getRelatedOpportunitiesCount on Opportunity (after insert, after update, after delete, after undelete) {
    if (Trigger.isInsert) {
        OpportunityTriggerHandler.handleAfterInsert(Trigger.new);
    } else if (Trigger.isUpdate) {
        OpportunityTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);
    } else if (Trigger.isDelete) {
        OpportunityTriggerHandler.handleAfterDelete(Trigger.oldMap);
    } else if (Trigger.isUndelete) {
        OpportunityTriggerHandler.handleAfterUndelete(Trigger.new);
    }
}
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!