You need to sign in to do that
Don't have an account?
IT 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');
}
@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');
}
I tested it in my DE environment and it looks good.
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
I tested it in my DE environment and it looks good.
Please do let me know if it helps you.
Regards,
Mahesh