+ Start a Discussion
kshannonkshannon 

Problem with trigger's test class..

Hey all, I have a trigger that is looping through all opportunities that have been updated and syncing the information to any contracts that are existing. I have a for loop going through each opportunity effected and then my test class is getting caught up on

 

newmonthcontracts = OppIDs[0].Term_Yrs__c.intValue() * 12;

 

I have the similar code in the test class that does this feature and have a systemassertequals to check it, but its still not covering the code.

 

Do I need to do something in perticular for the loop so that it validates? It seems to only be the code in the loop.

ErikssonEriksson

Hi,

 

It will be helpful if you could attach your completed script.

 

Cheers

kshannonkshannon

hey sorry for the delay, this is the code and still having troubles with this loop!

 

 

public class SyncDonationClass {
    
    public static void SyncDonation(Id oppid){
    
    system.debug('oppid - ' + oppid);
    
    // create the variable for the list of opportunities updated
    List<Opportunity> OppIDs = new List<Opportunity>([SELECT Amount, Term_Yrs__c, Begin_Date__c, 
         Description, Commitment_Date__c, FYs_Applied_To__c, AccountId, Program_Designation__c
         FROM Opportunity WHERE Id =: oppid]);
         
         
        
    integer newmonthcontracts;
    integer newmonthcontracts2;
   
   
   /* // loop through the updated opportunities
        for (Opportunity o : oppid) {

        // add the id of all updated opportunities (donations)
        OppIDs.add(o);
        
        }
        // query the list of all contracts related and store them in a list "contractrecords"
        */
        List<Contract> contractrecords = new List<Contract>([Select id from Contract Where Donation__c =: oppid]); 
        
        // these lines are used to debug values
        //System.debug('contracts' + contractrecords); 
        //System.debug('oppids here ' + OppIDs);
        
        
        //loop through all contracts we found associated with the opportunity (donation)
        for(Contract c : contractrecords){
           
           // convert the term years to months for the contract
           newmonthcontracts = OppIDs[0].Term_Yrs__c.intValue() * 12;
           
            // update the contract fields from the opportunity
            c.Contract_Value__c = OppIDs[0].Amount;
            c.StartDate = OppIDs[0].Begin_Date__c;
            c.Description = OppIDs[0].Description;
            c.EndDate = OppIDs[0].Commitment_Date__c;
            c.FYs__c = OppIDs[0].FYs_Applied_To__c;
            c.Account = OppIDs[0].Account;
            c.Team__c = OppIDs[0].Program_Designation__c;
            c.ContractTerm =  newmonthcontracts; 
            update c;
        }
    }
    static testMethod void testSync(){
        
        integer newmonthcontracts;
    integer newmonthcontracts2;
        
        List<Contract> contrs = new List<Contract>{};
        List<Contract> contrs2 = new List<Contract>{};
        
        account a = new Account(Name = 'Test Account', Relationship_Stage__c = 'Lapsed Funder');
        insert a;
        opportunity o = new Opportunity(Name = 'Test Donation',
                Term_Yrs__c = 2, 
                Accountid=a.id,StageName = 'Open - Prospect',
                CloseDate =Date.newinstance(2010,04,05),Amount=100);
        insert o;
        
        Opportunity oo = [select id,StageName,Term_Yrs__c, isWon from Opportunity WHERE ID =: o.id];
                   oo.ForecastCategoryName = 'Closed';
                   oo.StageName = 'Closed - Fully Funded';
                  // oo.isWon = true;
        update oo; 
        
        integer i;
        
        for(i = 0; i < 10; i++){
        
        Contract c = new Contract(Name = 'Test Contract', AccountId = a.id,
        Contract_Value__c = 111,
                ContractTerm = 24,
                Donation__c = o.id,
                Description = 'Test Description',
                FYs__c = 'FY11',
                Team__c = 'Information Systems',
                StartDate = Date.newinstance(2010,04,05));
     insert c;
     contrs.add(c);
         }
     
     System.assertequals(contrs[0].Name,'Test Contract');
     
     Contract cc = [select id, Name, Donation__c, Contract_Value__c, StartDate, Description, EndDate,FYs__c, AccountId,Team__c, ContractTerm from Contract WHERE Donation__c =: o.id];
     
     newmonthcontracts2 = oo.Term_Yrs__c.intValue() * 12;
     
  
     
      System.assertequals(newmonthcontracts2,24);
     for(Integer ii = 0; ii < 10; ii++){
  cc.Contract_Value__c = 111;
  cc.StartDate = Date.newinstance(2010,04,05);
  cc.Description = 'Test Description';
  cc.EndDate = Date.newinstance(2010,04,05);
  cc.FYs__c = 'FY11';
  cc.AccountId = a.Id;
  cc.Team__c = 'Information Systems';
  cc.ContractTerm = newmonthcontracts2;
 update cc;
 contrs2.add(cc);
}

System.assertEquals(o.Id,contrs2[0].Donation__c);
 System.assertEquals(contrs[0].Contract_Value__c,contrs2[0].Contract_Value__c);
  System.assertEquals(contrs[0].ContractTerm,contrs2[0].ContractTerm);
   System.assertEquals(contrs[0].StartDate,contrs2[0].StartDate);
      System.assertEquals(contrs[0].Description,contrs2[0].Description);
         System.assertEquals(contrs[0].FYs__c,contrs2[0].FYs__c);
            System.assertEquals(contrs[0].AccountId,contrs2[0].AccountId);
               System.assertEquals(contrs[0].Team__c,contrs2[0].Team__c);
   System.assertequals(contrs2[0].ContractTerm,24);
        
    }
}