+ Start a Discussion
shwetha prabhakarshwetha prabhakar 

Can anyone help me out in writing test class for below trigger code?

trigger QuotaRoll on Opportunity (after delete,after update,after insert,after undelete) {
    
    set<ID>QtaIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(opportunity opp : trigger.new){
            QtaIds.add(opp.Quota__c);
        }
    }
    if(trigger.isdelete){
        for(opportunity opp : trigger.old){
            QtaIds.add(opp.Quota__c);           
        }        
    }
       
    if(trigger.isupdate){
        for(opportunity opp:trigger.new){
            QtaIds.add(opp.Quota__c);
            if(trigger.oldmap.get(opp.id).Quota__c != opp.Quota__c && trigger.oldmap.get(opp.id).Quota__c != null ){
                QtaIds.add(trigger.oldmap.get(opp.id).Quota__c);
            }            
        }
    }    
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [SELECT SUM(Amount) sum,Quota__c FROM Opportunity where Quota__c in :QtaIds and ClosedDate_Checkbox__c ='True' and StageName = 'Closed Won' group by Quota__c ]){
        amtmap.put((ID)ag.get('Quota__c'), double.valueof(ag.get('SUM')));
    }
    list<Quota__c>Qtalist = new list<Quota__c>();
   
    for(id iid : QtaIds){
        Quota__c quot = new Quota__c(id=iid);
        if(amtmap.containskey(iid)){
            quot.Inside_Sales_Roll_up__c = amtmap.get(iid);
        }else{
            quot.Inside_Sales_Roll_up__c = 0;
        } 
        Qtalist.add(quot);       
    }
   
    if(Qtalist.size()>0){
        update Qtalist;
    }
   
}