+ Start a Discussion
Douglas TrasterDouglas Traster 

Help with Test Class on Trigger

I have the following trigger:

trigger activeOpp on Opportunity (after insert, after update) {
    
    List<contact> conUpdList = new List <contact>();
    List<Id> oppConIDList = new List<Id>();
    List<opportunity> oppConList = new List<opportunity>();
    List<Contact> conList = new List<Contact>();
    Map<ID, Contact> conMap = new Map<ID, Contact>();

    
    for (opportunity opp: Trigger.new){
      system.debug(opp.StageName+ ' ' +opp.Contact__c);
        if(opp.Contact__c!= null){
            oppConList.add(opp);
            oppConIDList.add(opp.Contact__c);
            system.debug(opp.Contact__c);
        }
    }   
    
    if(!oppConIDList.isEmpty()){   
        conList  = [select ID, Active_Opportunity__c from contact where ID = :oppConIDList];
       
    }
        
    if(!conList.isEmpty()){
        for(Contact con : conList){
            conMap.put(con.ID,con);     
        }
    }
        if(!oppConList.isEmpty()){    
        for(Opportunity optyObj : oppConList){
       Contact c = conMap.get(optyObj.Contact__c);
            if( c!= null )
               
            {
            if (optyObj.StageName == 'Closed Won' || optyObj.StageName == 'Closed Lost'){
                c.Active_Opportunity__c = False;
                system.debug(c.Active_Opportunity__c);
            }
            
            else if(optyObj.StageName == 'Prospecting'
            || optyObj.StageName == 'Qualified Interest'
            || optyObj.StageName == 'Needs Analysis'
            || optyObj.StageName == 'Value Proposition'
            || optyObj.StageName == 'Proposal/Price Quote'
            || optyObj.StageName == 'Negotiation/Review'
            || optyObj.StageName == 'Verbal Commit'
            || optyObj.StageName == 'Closed Pending'){
                c.Active_Opportunity__c = True;
            }
            conUpdList.add(c);        
        }
    }
    }
    if(!conUpdList.isEmpty()){
        update conUpdList;
    }
}
I am trying to use the following test, but not sure why it is only giving me 75%?  Any help on why I can't get this working.

@isTest

private class TestOppTrgWContUpd {

    static testmethod void TestOpportunityTrgr() {
   
           Test.startTest();

       // Create two contacts

           Contact ct1 = new Contact();
           Contact ct2 = new Contact();
           ct1.FirstName = 'John';
           ct1.LastName = 'Doe';
           ct2.FirstName = 'Jane';
           ct2.LastName = 'Smith';
           //ct1.Account = 'Public School';
          // ct2.Account = 'Public School';
           ct1.Category__c = 'Public School';
           ct2.Category__c = 'Public School';
           ct1.Role__c = 'Instructional Technology';
           ct2.Role__c = 'Other';
           ct1.Email = 'jdoe@school.edu';
           ct2.Email = 'jsmith@hed.edu';
           Insert ct1;    
           Insert ct2;

           system.debug('Completed Contact Creation'); 

           

     

        // Create Opportunity

          Opportunity op1 = new Opportunity();
          Opportunity op2 = new Opportunity();
          
          op1.name = 'Opportunity Contact Insertion Test';
          //op1.account = 'Public School';
          op1.CloseDate =  Date.today()+2;
          op1.StageName = 'Prospecting';
          op1.forecastcategoryname = 'Pipeline';   
          op1.Contact__c = ct1.Id ;

          //insert op1;
          
          op1.name = 'Opportunity Contact Insertion Test';
          //op1.account = 'Public School';
          op1.CloseDate =  Date.today()+2;
          op1.StageName = 'Prospecting';
          op1.forecastcategoryname = 'Pipeline';   
          op1.Contact__c = ct1.Id ;
          
          //insert op2;
          
          Database.SaveResult sr1 = Database.insert(op1, true);
          Database.SaveResult sr2 = Database.insert(op2, true);

          System.assert(sr1.isSuccess());
          System.assert(sr2.isSuccess());

          system.debug('Inserted new opportunity');

          

       // Update Opportunity with new contact

 

          op1.Contact__c = ct2.Id;

          //update op1;

          Database.SaveResult sr3 = Database.update(op1, true);

          System.assert(sr3.isSuccess());

          system.debug('Opportunity updated');

          

          Test.stopTest();

          System.assert(sr3.isSuccess()); 

    }    

}
Best Answer chosen by Douglas Traster
Ajay Nagar 7Ajay Nagar 7
@isTest

private class TestOppTrgWContUpd {
    static testmethod void TestOpportunityTrgr() {
           Test.startTest();
       // Create two contacts
           Contact ct1 = new Contact();
           Contact ct2 = new Contact();
           ct1.FirstName = 'John';
           ct1.LastName = 'Doe';
           ct2.FirstName = 'Jane';
           ct2.LastName = 'Smith';
           //ct1.Account = 'Public School';
          // ct2.Account = 'Public School';
           ct1.Category__c = 'Public School';
           ct2.Category__c = 'Public School';
           ct1.Role__c = 'Instructional Technology';
           ct2.Role__c = 'Other';
           ct1.Email = 'jdoe@school.edu';
           ct2.Email = 'jsmith@hed.edu';
           Insert ct1;    
           Insert ct2;
           system.debug('Completed Contact Creation'); 
        // Create Opportunity
          Opportunity op1 = new Opportunity();
          Opportunity op2 = new Opportunity();
		  Opportunity op3 = new Opportunity();
          Opportunity op4 = new Opportunity();
          
          op1.name = 'Opportunity Contact Insertion Test';
          //op1.account = 'Public School';
          op1.CloseDate =  Date.today()+2;
          op1.StageName = 'Prospecting';
          op1.forecastcategoryname = 'Pipeline';   
          op1.Contact__c = ct1.Id ;
          //insert op1;
          op2.name = 'Opportunity Contact Insertion Test';
          //op2.account = 'Public School';
          op2.CloseDate =  Date.today()+2;
          op2.StageName = 'Closed Won';
          op2.forecastcategoryname = 'Pipeline';   
          op2.Contact__c = ct2.Id ;
		  
		  op3.name = 'Opportunity Contact Insertion Test';
          //op3.account = 'Public School';
          op3.CloseDate =  Date.today()+2;
          op3.StageName = 'Needs Analysis';
          op3.forecastcategoryname = 'Pipeline';   
          op3.Contact__c = ct2.Id ;
		  
		  op4.name = 'Opportunity Contact Insertion Test';
          //op4.account = 'Public School';
          op4.CloseDate =  Date.today()+2;
          op4.StageName = 'Qualified Interest';
          op4.forecastcategoryname = 'Pipeline';   
          op4.Contact__c = ct2.Id ;
		  
          //insert op2;
          Database.SaveResult sr1 = Database.insert(op1, true);
          Database.SaveResult sr2 = Database.insert(op2, true);
		  Database.SaveResult sr3 = Database.insert(op3, true);
          Database.SaveResult sr4 = Database.insert(op4, true);
          System.assert(sr1.isSuccess());
          System.assert(sr2.isSuccess());
		  System.assert(sr3.isSuccess());
          System.assert(sr4.isSuccess());
          system.debug('Inserted new opportunity');
       // Update Opportunity with new contact
          op1.Contact__c = ct2.Id;
          //update op1;
          Database.SaveResult sr3 = Database.update(op1, true);
          System.assert(sr3.isSuccess());
          system.debug('Opportunity updated');
          Test.stopTest();
          System.assert(sr3.isSuccess()); 
    }    

}
Use this code.