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
Kunal Purohit 4Kunal Purohit 4 

How to write handler for given trigger

Hello Folks,
How to write Handler for below trigger.?

trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete, After Undelete) {
 List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    if(Trigger.isInsert){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
            	}
			}
		}
    } 
    system.debug('setAccIds ==> '+setAccIds);
    if(Trigger.isUpdate){
         if(trigger.isAfter){
        for(Opportunity con : Trigger.new){ 
            if(con.AccountId!=Trigger.oldMap.get(con.Id).AccountId && con.Amount>10000.00){
               	setAccIds.add(con.AccountId);
                setAccIds.add(Trigger.oldMap.get(con.Id).AccountId);
            	}
            
			}        
        }
    }
    if(Trigger.isDelete){
        if(trigger.isAfter){
        for(Opportunity con : Trigger.old) { 
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    } 
    
    if(Trigger.isUndelete){
        if(trigger.isAfter){
        for(Opportunity con : Trigger.new) { 
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
            	}
        	}
        }
    } 
    for(Account acc :[Select id,Total_Opportunity__c ,(Select Amount from Opportunities where Amount>10000.00) from Account where Id in : setAccIds]){
			
        acc.Total_Opportunity__c = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    
}
Best Answer chosen by Kunal Purohit 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kunal,

Can you try below trigger and handler.

Handler:
public class SumAmountOppHandler {
    
     List<Account> accList=new List<Account>();
     Set<Id> setAccIds = new Set<Id>();
    public  void afterinsert(List<Opportunity> newoppylist){
  for(Opportunity con : newoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
               updateamount(setAccIds) ;
                }
                        } 
    }
      public  void afterupdate(List<Opportunity> newoppylist, Map<Id,Opportunity> mapoppy){
    
        for(Opportunity con : newoppylist){ 
            if(con.AccountId!=mapoppy.get(con.Id).AccountId && con.Amount>10000.00){
                setAccIds.add(con.AccountId);
                setAccIds.add(mapoppy.get(con.Id).AccountId);
                updateamount(setAccIds) ;
                }
            
                        }        
        
    }
      public  void afterdelete(List<Opportunity> oldoppylist){
  for(Opportunity con : oldoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
                updateamount(setAccIds) ;
                }
                        } 
    }
    
    public void updateamount(set<id> accids){
          for(Account acc :[Select id,Total_Opty_Amount__c  ,(Select Amount from Opportunities where Amount>10000.00) from Account where Id in : accids]){
                        
        acc.Total_Opty_Amount__c  = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    }

}

Trigger:
 
trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete, After Undelete) {
 List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    SumAmountOppHandler sah= new SumAmountOppHandler();
    if((Trigger.isInsert || Trigger.isundelete) && Trigger.isAfter){

      sah.afterinsert(Trigger.new);
                
    } 
    if(Trigger.isUpdate && Trigger.isAfter){
       sah.afterupdate(Trigger.new,Trigger.oldmap);
    }
    if(Trigger.isDelete && Trigger.isAfter){
        sah.afterdelete(Trigger.old);
    } 
    
  
  
    
}

Replace Total_Opty_Amount__c with Total_Opportunity__c

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kunal,

Can you confirm if Total_Opportunity__c is curency field on Account object?

Thanks,


 
Kunal Purohit 4Kunal Purohit 4
Yeah Sai Praveen,
Total_Opportunity__c is Number field in Account Object.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kunal,

Can you try below trigger and handler.

Handler:
public class SumAmountOppHandler {
    
     List<Account> accList=new List<Account>();
     Set<Id> setAccIds = new Set<Id>();
    public  void afterinsert(List<Opportunity> newoppylist){
  for(Opportunity con : newoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
               updateamount(setAccIds) ;
                }
                        } 
    }
      public  void afterupdate(List<Opportunity> newoppylist, Map<Id,Opportunity> mapoppy){
    
        for(Opportunity con : newoppylist){ 
            if(con.AccountId!=mapoppy.get(con.Id).AccountId && con.Amount>10000.00){
                setAccIds.add(con.AccountId);
                setAccIds.add(mapoppy.get(con.Id).AccountId);
                updateamount(setAccIds) ;
                }
            
                        }        
        
    }
      public  void afterdelete(List<Opportunity> oldoppylist){
  for(Opportunity con : oldoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
                updateamount(setAccIds) ;
                }
                        } 
    }
    
    public void updateamount(set<id> accids){
          for(Account acc :[Select id,Total_Opty_Amount__c  ,(Select Amount from Opportunities where Amount>10000.00) from Account where Id in : accids]){
                        
        acc.Total_Opty_Amount__c  = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    }

}

Trigger:
 
trigger SumAmountOpp on Opportunity (After Insert,After Update,After Delete, After Undelete) {
 List<Account> accList=new List<Account>();

    Set<Id> setAccIds = new Set<Id>();
    SumAmountOppHandler sah= new SumAmountOppHandler();
    if((Trigger.isInsert || Trigger.isundelete) && Trigger.isAfter){

      sah.afterinsert(Trigger.new);
                
    } 
    if(Trigger.isUpdate && Trigger.isAfter){
       sah.afterupdate(Trigger.new,Trigger.oldmap);
    }
    if(Trigger.isDelete && Trigger.isAfter){
        sah.afterdelete(Trigger.old);
    } 
    
  
  
    
}

Replace Total_Opty_Amount__c with Total_Opportunity__c

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Kunal Purohit 4Kunal Purohit 4

Thanks Sai Praveen. It worked for me. Now, I am trying to write a test class for same. Can you please help me how to accompolish the same? Here, is my test class.

@isTest
public class AmountatOpportunityTest {

    @istest
    static void OppCount()
    {
        Account acc= new Account();
        acc.name= 'sample account';
        acc.ISSN_Number__c = 111;
        insert acc;
        
        Account acc1 = new Account();
        acc1.Name = 'test account';
        acc1.ISSN_Number__c = 2222;
        insert acc1;
        
           Opportunity opp = new Opportunity();
        opp.AccountId = acc.Id;
        opp.Amount=15000.00;
        opp.Name = 'Test sum';
        opp.StageName = 'Test picklist';
        opp.CloseDate= date.newInstance(2023, 11, 10);
        insert opp;
        
        
        Opportunity Opp1 = new Opportunity();
        opp1.AccountId = acc1.Id;
        opp1.Amount=15000.00;
        opp1.Name = 'Test sum2';
        opp1.StageName = 'Test picklist2';
        opp1.CloseDate= date.newInstance(2023, 11, 10);
        insert opp1;
        
        List<Opportunity> OppLst = new List<Opportunity>();
        OppLst.add(opp);
        OppLst.add(opp1);
        
        
        Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>();
        OppMap.Put(opp.Id, opp);
        OppMap.Put(opp1.Id, opp1);
        
        AmountAtOpportunityClass ac = new AmountAtOpportunityClass();
        ac.afterdelete(OppLst);
        ac.afterinsert(OppLst);
        ac.afterupdate(OppLst, OppMap);
         
        
    }    
}

 

It is shoiwng error:

System.ListException: Duplicate id in list: 0012w000019RuxzABC

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Thanks for confirming on it. As this seems to test class for the above which will be seperate question  can you post this as new question so I can answer it so it will avoid confusion for others as well.

Thanks,
Kunal Purohit 4Kunal Purohit 4
Ok..