Write a trigger that updates the Account ‘Number of Opportunities’ field when an Opportunity is inserted, updated, or deleted.

This Apex trigger ensures that the custom field Number_of_Opportunities__c on the Account object always reflects the current number of related Opportunity records. It runs after insert, update, and delete operations on Opportunity.

Trigger-

trigger opportunityTrigger on Opportunity (after insert, after update, after delete) {
    if (Trigger.isAfter) {
        if (Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete) {
            opportunityTriggerHandler.oppCountUpdate(Trigger.new, Trigger.oldMap);
        }
    }
}

Apex class-

public class opportunityTriggerHandler {
    public static void oppCountUpdate(List<Opportunity> oppList, Map<Id,Opportunity> oldMap) {
        Set<Id> accountIds = new Set<Id>();
        
        // From new records
        if (oppList != null) {
            for (Opportunity opp : oppList) {
                if (opp.AccountId != null) {
                    accountIds.add(opp.AccountId);
                }
            }
        }

        // From oldMap (for deletes/updates)
        if (oldMap != null) {
            for (Opportunity oldopp : oldMap.values()) {
                if (oldopp.AccountId != null) {
                    accountIds.add(oldopp.AccountId);
                }
            }
        }

        if (accountIds.isEmpty()) return;

        List<Account> accountsToUpdate = [
            SELECT Id, (SELECT Id FROM Opportunities) 
            FROM Account 
            WHERE Id IN :accountIds
        ];

        for (Account acc : accountsToUpdate) {
            acc.Number_of_Opportunities__c = acc.Opportunities.size();
        }

        update accountsToUpdate;
    }
}