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
IT OperationsIT Operations 

Related object returns null when requeried in test class

Here is the sample code I'm trying to run.  When I requery the contact record for account.name, it returns null.  Does anyone know why this is?

@isTest public class TestSampleInsert {
public static testMethod void testMyController() {        
Account someAccount = new Account(Name = 'Test Account', Website = 'http://www.somesite.com');
insert someAccount;

Contact someContact = new contact(FirstName = 'Tim', lastName = 'Joes', Email = 'test@test.com', Account = someAccount);
insert someContact;

// Requery contact for field data
Contact contactRequery = [SELECT ID, FirstName, LastName, Email, Account.Name from Contact where ID = :someContact.id limit 1];

// This fails!  Account.name is null
System.AssertEquals(contactRequery.account.name, 'Test Account');
}
Best Answer chosen by IT Operations
Mahesh DMahesh D
Please take the below code:

I tested it in my DE environment and it looks good.

 
@isTest
public class TestSampleInsert {
    public static testMethod void testMyController() {
        Account someAccount = new Account(Name = 'Test Account', Website = 'http://www.somesite.com');
        insert someAccount;
        
        Contact someContact = new contact(FirstName = 'Tim', lastName = 'Joes', Email = 'test@test.com', AccountId = someAccount.Id);
        insert someContact;
        
        // Requery contact for field data
        Contact contactRequery = [SELECT ID, FirstName, LastName, Email, Account.Name from Contact where ID = :someContact.id limit 1];
        
        // This fails!  Account.name is null
        System.AssertEquals(contactRequery.account.name, 'Test Account');
    }
}

Please do let me know if it helps you.

Regards,
Mahesh​

All Answers

Mahesh DMahesh D
Please take the below code:

I tested it in my DE environment and it looks good.

 
@isTest
public class TestSampleInsert {
    public static testMethod void testMyController() {
        Account someAccount = new Account(Name = 'Test Account', Website = 'http://www.somesite.com');
        insert someAccount;
        
        Contact someContact = new contact(FirstName = 'Tim', lastName = 'Joes', Email = 'test@test.com', AccountId = someAccount.Id);
        insert someContact;
        
        // Requery contact for field data
        Contact contactRequery = [SELECT ID, FirstName, LastName, Email, Account.Name from Contact where ID = :someContact.id limit 1];
        
        // This fails!  Account.name is null
        System.AssertEquals(contactRequery.account.name, 'Test Account');
    }
}

Please do let me know if it helps you.

Regards,
Mahesh​
This was selected as the best answer
IT OperationsIT Operations
Thanks a lot.  I see I needed to set AccountId on contact and not account.  Thanks so much!