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 

Access variables of inserted data

At the bottom in my Assertion statements, I am trying to reach the StageName of the Opportunity.  However I know that the variable ie (opptest1.StageName) is a reference before the insert and Stagename update.  How do I access that specific Opportunity after the Insert?

 

@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 = 'Do Not Call'), // Has Work Number, Active, 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.
         System.assertEquals('Closed Lost', [SELECT Id FROM Opportunity WHERE Primary_Contact Name = 'test1'].id);
        
         System.assertEquals('Closed Lost', opptest2.StageName);
         System.assertEquals('Closed Lost', opptest3.StageName);
         System.assertEquals('Closed Lost', opptest4.StageName); 
         System.assertEquals('Closed Lost', opptest5.StageName);
         System.assertEquals('Closed Lost', opptest6.StageName);
         System.assertEquals('Closed Lost', opptest7.StageName);
		*/
    }
             
}
ANUTEJANUTEJ (Salesforce Developers) 
Hi Timothy,

You will have to fetch the records that you have inserted in the test class and then check using the loop and assert statements to see if the data is proper.

so in the above test class you will have to fetch all the records with the filter on account id and then run through the list using a loop and assert statements.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.
Timothy SmithTimothy Smith
Anutej,
Thank you for responding.  Forgive me as I am very new to this, would you be able to provide an example?