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
TeddyAbleTeddyAble 

HELP! Needed with Test Class please

Hello guys;

 

I will like to write a Test Class for the code below,

 

Please any feedback will be useful

 

trigger RollUp on Managed_Fund__c (after insert, after update, before delete) {

    // Set to hold the IDs of the Contacts of the Subscriptions
    Set<ID> AdviserIds= new Set<ID>();
    // Set to hold the IDs of the Subscriptions
    Set<ID> SubIDs = new Set<ID>();
    // Set to hold the IDs of the Contacts where the values need to be reset
    Set<ID> AdviserIdssForReset = new Set<ID>();
    Set<ID> AdviserIdss=New Set<ID>();
     // The list of Contacts where the update has to take place
List<Adviser__c> AdvisersToUpdate = new List<Adviser__c>(); 
    if(trigger.isInsert)
    {
        for(Managed_Fund__c rsSub : trigger.new )
        {
            // If the contact of a subscription has changed, then the old contact also needs to be updated 
            if( rsSub.Adviser__c != trigger.newmap.get(rsSub.ID).Adviser__c)
                if( trigger.oldmap.get(rsSub.ID).Adviser__c!= null )
                    AdviserIdss.add(trigger.newmap.get(rsSub.ID).Adviser__c);
            if(rsSub.Adviser__c!=null)
            {
                AdviserIds.add(rsSub.Adviser__c);
            }
        }
        AdviserIdssForReset.addAll(AdviserIds);

          

        // Querying the database to get the Number of Subscriptions under an contact and the sum of the deal values of the Subscriptions under an contact
        AggregateResult[] fuMSum = [select sum(Total_Unit_Balance__c) aggr, Adviser__c AdviserIds from Managed_Fund__c where Adviser__c in: AdviserIds group by Adviser__c ];
        
        
        for( AggregateResult AdviserDetail  : fuMSum )
        {
            Adviser__c AdviserToUpdate5 = new Adviser__c(ID = (ID)(AdviserDetail .get('AdviserIds')));
            AdviserToUpdate5.FUM__c ='$ '+String.valueOf(AdviserDetail.get('aggr'));
            AdvisersToUpdate.add(AdviserToUpdate5);
            // The contact being handled should not be reset. Hence removing it from the ResetSet
            AdviserIdssForReset.remove((ID)(AdviserDetail .get('AdviserIds')));
            System.debug('###'+String.valueOf(AdviserDetail.get('aggr')));
        }
        
        for( ID i : AdviserIdssForReset )
        {
            // Resetting the summed up values to 0 if the contact no longer has a .V.
            Adviser__c AdviserToUpdate4 = new Adviser__c( ID = i );
            AdviserToUpdate4.FUM__c = '0';
            AdvisersToUpdate.add(AdviserToUpdate4);
        }
        
        if(AdvisersToUpdate.size() > 0)
        {
           update AdvisersToUpdate;
         }   
        AdvisersToUpdate.clear();
    }

    if(trigger.isupdate)
    {
        for(Managed_Fund__c rsSub : trigger.new )
        {
            // If the contact of a subscription has changed, then the old contact also needs to be updated 
            if( rsSub.Adviser__c != trigger.oldmap.get(rsSub.ID).Adviser__c)
                if( trigger.oldmap.get(rsSub.ID).Adviser__c!= null )
                    AdviserIdss.add(trigger.oldmap.get(rsSub.ID).Adviser__c);
            if(rsSub.Adviser__c!=null)
            {
                AdviserIds.add(rsSub.Adviser__c);
            }
        }
        AdviserIdssForReset.addAll(AdviserIds);

          

        // Querying the database to get the Number of Subscriptions under an contact and the sum of the deal values of the Subscriptions under an contact
        AggregateResult[] fuMSum = [select sum(Total_Unit_Balance__c) aggr, Adviser__c AdviserIds from Managed_Fund__c where Adviser__c in: AdviserIds group by Adviser__c ];
        
        for( AggregateResult AdviserDetail  : fuMSum )
        {
            Adviser__c AdviserToUpdate5 = new Adviser__c(ID = (ID)(AdviserDetail .get('AdviserIds')));
            AdviserToUpdate5.FUM__c = '$ '+String.Valueof(AdviserDetail.get('aggr'));
            AdvisersToUpdate.add(AdviserToUpdate5);
            // The contact being handled should not be reset. Hence removing it from the ResetSet
            AdviserIdssForReset.remove((ID)(AdviserDetail .get('AdviserIds')));
        }
        
        for( ID i : AdviserIdssForReset )
        {
            // Resetting the summed up values to 0 if the contact no longer has a .V.
            Adviser__c AdviserToUpdate4 = new Adviser__c( ID = i );
            AdviserToUpdate4.FUM__c = '0';
            AdvisersToUpdate.add(AdviserToUpdate4);
        }
        
        if(AdvisersToUpdate.size() > 0)
        {
           update AdvisersToUpdate;
         }   
        AdvisersToUpdate.clear();
    }

    if(trigger.isdelete)
    {
        for(Managed_Fund__c rsSub : trigger.old )
        {
            if( rsSub.Adviser__c != null)
                AdviserIdss.add(rsSub.Adviser__c);
            SubIDs.add(rsSub.ID);
        }
        AdviserIdssForReset.addAll(AdviserIdss);

        // The list of Contacts where the update has to take place
        List<Adviser__c> AdviserToUpdate = new List<Adviser__C>();    
        
        // Querying the database to get the Number of Subscriptions under an contact excluding the Subscriptions being deleted
        AggregateResult[] fuMSum = null;
        if(AdviserIdss.size() > 0)
        {
            fuMSum = [select count(ID) Fum__c, Adviser__c AdviserIds from Managed_Fund__c where Adviser__c in: AdviserIds and ID not in: SubIDs group by Adviser__c ];

            for( AggregateResult ContactDetail : fuMSum)
            {
                Adviser__c AdviserToUpdate2 = new Adviser__c(ID = (ID)(ContactDetail.get('AdviserIds')));
                AdviserToUpdate2.Fum__c = '$ '+String.Valueof(ContactDetail.get('Total_Unit_Balance__c'));
                AdviserToUpdate.add(AdviserToUpdate2);
                // The contact being handled should not be reset. Hence removing it from the ResetSet
                AdviserIdssForReset.remove((ID)(ContactDetail.get('AdviserIds')));
            }

            for( ID i : AdviserIdssForReset )
            {
                // Resetting the summed up values to 0 if the contact no longer has a subscription.
                Adviser__c AdviserToUpdate3 = new Adviser__c( ID = i );
                AdviserToUpdate3.FUM__c = '0';
               AdviserToUpdate.add(AdviserToUpdate3);
            }
        }
        
        if(AdviserToUpdate.size() > 0)
            update AdviserToUpdate;

        AdviserToUpdate.clear();
    }
}

 

Thank you in Advance