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
Timothy SmithTimothy Smith 

APEX testing cant populate my Trigger.new

Trigger will close Opportunity if no valid phone numbers exist.

Opportunity has Primary_Contact__c and Secondary_Contact__c

On Contact 3 phone fields to check:
Phone__c
Work_phone__c
Mobile_phone__c

On Contact 3 Phone Status Fields:  
Phone_Status__c
Work_Status__c
Mobile_Status__c.

After Insert/Update of a Contact, the Trigger will check all Opportunities associated with the Contact.  If there are no valid (meaning having a number and corresponding status is not equal to "Do Not Call"), then close the Opportunity.

I have created the Trigger and Test Class but I don't understand why the lines in the test class are not executing.

Thank you.

Trigger:
trigger Contact_DNC_Opportunity on Contact (after insert, after update){

    

      List<Opportunity> OpportunitiesToClose = new List<Opportunity>();

    List<Opportunity> WorkingOpportunities = [SELECT StageName, Primary_Contact__c, Secondary_Contact__c,

                                              Primary_Contact__r.Phone_Status__c, Primary_Contact__r.Mobile_Phone_Status__c, Primary_Contact__r.Work_Phone_Status__c,

                                              Primary_Contact__r.Phone, Primary_Contact__r.Normalized_Mobile_Phone__c, Primary_Contact__r.Normalized_Work_Phone__c,                                          

                                              Secondary_Contact__r.Phone_Status__c, Secondary_Contact__r.Mobile_Phone_Status__c, Secondary_Contact__r.Work_Phone_Status__c,

                                              Secondary_Contact__r.Phone, Secondary_Contact__r.Normalized_Mobile_Phone__c, Secondary_Contact__r.Normalized_Work_Phone__c

                                              FROM Opportunity WHERE Primary_Contact__c In :Trigger.New OR Secondary_Contact__c In :Trigger.New

                                             ];

     
System.debug(WorkingOpportunities);
    
    for(Opportunity opp: WorkingOpportunities){
        
//Check Primary Contact phone numbers          

            if ((opp.Primary_Contact__r.Phone == Null) || (opp.Primary_Contact__r.Phone_Status__c == 'Do Not Call')&&
                (opp.Primary_Contact__r.Normalized_Mobile_Phone__c == Null) || (opp.Primary_Contact__r.Mobile_Phone_Status__c == 'Do Not Call')&&
                (opp.Primary_Contact__r.Normalized_Work_Phone__c == Null) || (opp.Primary_Contact__r.Work_Phone_Status__c == 'Do Not Call')&&
                (opp.Secondary_Contact__r.Phone == Null) || (opp.Secondary_Contact__r.Phone_Status__c == 'Do Not Call')&&
                (opp.Secondary_Contact__r.Normalized_Mobile_Phone__c == Null) || (opp.Secondary_Contact__r.Mobile_Phone_Status__c == 'Do Not Call')&&
                (opp.Secondary_Contact__r.Normalized_Work_Phone__c == Null) || (opp.Secondary_Contact__r.Work_Phone_Status__c == 'Do Not Call')
                ) {

                    

//Add opp to List if fits criteria

       opportunitiesToClose.add(opp);

           }

}

   

    for (Opportunity opp1 : opportunitiesToClose){

        opp1.StageName = 'Closed Lost';

    }

 

    if(opportunitiesToClose.size() > 0){

        update opportunitiesToClose ;

    }

 

}

Test Class:
@isTest
private class ContactDNCOpportunityTest {
       
    private static testMethod void testCloseOpps(){
        //Create Account
        
        Account newAcc = FlowTestUtils.createHouseholdAccount();
    		insert newAcc;  
        
        
       //Create Contacts
           List<Contact> conList = new List<Contact> {
		new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'),  //Has Phone Number - Do not call, Closed Opp
        new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers  Closed Opp
        new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp
		new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = '1234567891', Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp
        new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp      
        new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked
            };	
                
            insert conList;
       
        
        Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1'].id);
        Test.startTest();
            insert opptest1;
        Test.stopTest();   
        
        System.assertEquals('Closed', opptest1.StageName);
                
            
        Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2'].id);
        Test.startTest();
            insert opptest2;
        Test.stopTest();   
        
        System.assertEquals('Closed', opptest2.StageName);
          
        
         Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3'].id);
         Test.startTest();
            insert opptest3;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest3.StageName);
           
        
         Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4'].id);
         Test.startTest();
            insert opptest4;
         Test.stopTest();   
        
        System.assertNotEquals('Closed', opptest4.StageName);
           
        
         Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5'].id);  
         Test.startTest();
            insert opptest5;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest5.StageName);
            
        
         Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6'].id);
         Test.startTest();
            insert opptest6;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest6.StageName);
            
         Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today());                                            
         Test.startTest();
            insert opptest7;
         Test.stopTest();   
        
        System.assertEquals('Closed', opptest7.StageName);
        
    }
             
}

User-added image​​​​​​​
Best Answer chosen by Timothy Smith
Maharajan CMaharajan C
Hi Timothy,

Try the below one :
 
@isTest
private class ContactDNCOpportunityTest {
       
    private static testMethod void testCloseOpps(){
        //Create Account
        
        Account newAcc = FlowTestUtils.createHouseholdAccount();
    		insert newAcc;  
        
        
       //Create Contacts
           List<Contact> conList = new List<Contact> {
		new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'),  //Has Phone Number - Do not call, Closed Opp
        new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers  Closed Opp
        new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp
		new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = '1234567891', Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp
        new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp      
        new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked
            };	
                
            insert conList;
       
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1'].id);
        oppList.add(opptest1);
            
        Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2'].id);
        oppList.add(opptest2);  
                  
        
         Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3'].id);
         oppList.add(opptest3); 
                   
        
         Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4'].id);
         oppList.add(opptest4); 
                   
        
         Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5'].id);  
         oppList.add(opptest5);  
                    
        
         Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6'].id);
         oppList.add(opptest6);
                    
         Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today());                                            
         oppList.add(opptest7); 

		 insert oppList;
		 
		 Test.StartTest();
			update conList;
		 Test.stopTest();  
		 
		 // Add your assert statements below here.
                
    }
             
}

Thanks,
Maharajan.C

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Timothy,

Does the related opportunity with contact which you are inserting in test class meeting all those if conditions . Please cross check it once.

Thanks!
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please try this below code.

@isTest private class ContactDNCOpportunityTest {
 private static testMethod void testCloseOpps(){
Contact con=new Contact();
con.FirstName='Test';
con.LastName='Data';
con.Phone ='1234554321';
con.Phone_Status__c ='Do Not Call';
con.Mobile_Phone_Status__c ='Do Not Call';
con.Work_Phone_Status__c ='Do Not Call';
  
insert con;

Opportunity op=new Opportunity();
op.Name='Data';
Op.closeDate=Date.today();
op.StageName='Qualification';
op.Primary_Contact__c=con.id;
insert op;

op.StageName='Closed Lost';
update op;
}
}

Please mark it as the Best Answer if it helps you.

Thank You

Maharajan CMaharajan C
Hi Timothy,

Try the below one :
 
@isTest
private class ContactDNCOpportunityTest {
       
    private static testMethod void testCloseOpps(){
        //Create Account
        
        Account newAcc = FlowTestUtils.createHouseholdAccount();
    		insert newAcc;  
        
        
       //Create Contacts
           List<Contact> conList = new List<Contact> {
		new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'),  //Has Phone Number - Do not call, Closed Opp
        new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers  Closed Opp
        new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp
		new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = '1234567891', Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp
        new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp      
        new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked
            };	
                
            insert conList;
       
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1'].id);
        oppList.add(opptest1);
            
        Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2'].id);
        oppList.add(opptest2);  
                  
        
         Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3'].id);
         oppList.add(opptest3); 
                   
        
         Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4'].id);
         oppList.add(opptest4); 
                   
        
         Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5'].id);  
         oppList.add(opptest5);  
                    
        
         Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6'].id);
         oppList.add(opptest6);
                    
         Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today());                                            
         oppList.add(opptest7); 

		 insert oppList;
		 
		 Test.StartTest();
			update conList;
		 Test.stopTest();  
		 
		 // Add your assert statements below here.
                
    }
             
}

Thanks,
Maharajan.C
This was selected as the best answer