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
Himanshu GhateHimanshu Ghate 

Can someone tell me what happing in this particular code and its not working.

trigger acctopp on Opportunity (after insert,after update,after delete) {
 Set<id> accid = new Set<id>();
  
        if(Trigger.isInsert ||  Trigger.isUpdate )
        {
            for(Opportunity opp:Trigger.new)
            {
               accid.add(opp.AccountId);//inserting accountid inside accid(inserting inside the set values)
            } 
        }
         if(Trigger.isdelete)
            {
                for(Opportunity opp:Trigger.old)
                {
                    accid.add(opp.AccountId);
                }
            }
        List<account> acclist = new List<account>();
        acclist = [Select id , name ,Account.Total_Opp__c,(Select id , name ,amount from Opportunities) from Account where Id IN:accid];//Query of account
        decimal total =0;
        for(account acc : acclist)
        {
            for(opportunity opp : acc.opportunities)
            {  
              
                total = total + opp.Amount;
                
            }
         
            acc.Total_Opp__c=total;
        }
    // update acclist;

}

The Scenario was-->When user go to Opportunity object and make some changes in Amount field or insert new record at that time in Account object there will be Total opp amount field will be there,And that changes which we made inside opportunity object in Amount field should reflect inside account object Total opp amount field automatically.
 
vikas singh 158vikas singh 158
Hi Himanshu please use this code 
for more dynamic please create a helper class and peaste the code there and just call that class in switch case where you want, hope it will work 
trigger triggeronopportunity on Opportunity (after insert , after update,after delete) {
    try{
        list<id> accountId = new list<id>();
        for(opportunity opt:trigger.new){
            if(opt !=null){
                accountId.add(opt.Accountid);
            }
        }
        
        switch on trigger.operationtype{
            when  AFTER_INSERT{
                  list<Account> newUpdate = new list<Account>();
                   newUpdate = [select id, name ,Total_opportunity__c,(Select id ,name,amount from Opportunities) from account where id In:accountId];
                for(account act:newUpdate ){
                    Decimal amont =0;
                    for(opportunity opt:act.Opportunities){
                        amont = amont+opt.Amount;
                    }
                    act.Total_opportunity__c = amont;
                }
                update newUpdate;
            }
            when AFTER_UPDATE{
                
            }
            when AFTER_DELETE{
                
            }
        }  
        
        
    }catch(Exception ex){
         system.debug('Exception::::'+ex.getLineNumber()+'at'+ ex.getMessage());
    }
    

}

thank you if it is help full please mask sa best answer 
Prateek Prasoon 25Prateek Prasoon 25
trigger UpdateTotalOppAmount on Opportunity (after insert, after update) {
    Set<Id> accountIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();
    Map<Id, Decimal> accountTotalOppAmounts = new Map<Id, Decimal>();
    
    for(Opportunity opp : Trigger.new){
        accountIds.add(opp.AccountId);
    }
    
    for(Account acc : [SELECT Id, Total_Opp_Amount__c, 
                            (SELECT Id, Amount FROM Opportunities) 
                       FROM Account WHERE Id IN :accountIds]){
        Decimal totalOppAmount = 0;
        
        for(Opportunity opp : acc.Opportunities){
            if(opp.Amount != null){
                totalOppAmount += opp.Amount;
            }
        }
        
        accountTotalOppAmounts.put(acc.Id, totalOppAmount);
    }
    
    for(Id accId : accountIds){
        if(accountTotalOppAmounts.containsKey(accId)){
            Account acc = new Account(Id = accId, Total_Opp_Amount__c = accountTotalOppAmounts.get(accId));
            accountsToUpdate.add(acc);
        }
    }
    
    if(accountsToUpdate.size() > 0){
        update accountsToUpdate;
    }
}

If you find my answer helpful, please mark it as the best answer. Thanks!
Himanshu GhateHimanshu Ghate
Thanks a lot.