+ Start a Discussion
Michael CerulloMichael Cerullo 

Relationship-based SOQL is returning a null result

I'm inserting an Account, followed by an Opportunity.  Then, I run a SOQL to make sure the name field on Account is not null.  However, it keeps showing as null.  I know I can do a SOQL directly on Account, but this is just a simplified question for the real issue I'm trying to solve.  Does anyone know why this is showing as null?  Thanks in advance for your help!

Account a = new Account();
        a.Name = 'Universal Containers';
        a.BillingStreet = 'Market';
        a.ShippingStreet = '1 Market';
        a.BillingCity = 'San Francisco';
        a.ShippingCity = 'San francisco';
        a.BillingPostalCode = '94105';
        a.ShippingPostalCode = '94105';
        a.BillingState = 'California';
        a.ShippingState = 'California';
        a.BillingCountry = 'United States';
        a.ShippingCountry = 'United States';
insert a;

Opportunity o = new Opportunity();
       o.Name = 'Universal Containers';
       o.Account = a;
        o.CloseDate = Date.today();
        o.StageName = 'Submitted';
        insert o;
        
Opportunity o = [SELECT Id, Account.Name FROM Opportunity WHERE Id = :o.Id LIMIT 1];
System.Assert(o.Account.Name != null);
//This assert always fails even tough I know the Account and Opportunity were successfully inserted and the the Account.Name field is populated.
Best Answer chosen by Michael Cerullo
Shashikant SharmaShashikant Sharma
Hi Michael,

Use this for account assignment instead of assigning instance directly.
o.AccountId = a.Id;
It should work for you.

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi Michael,

Use this for account assignment instead of assigning instance directly.
o.AccountId = a.Id;
It should work for you.

Thanks
Shashikant
This was selected as the best answer
Michael CerulloMichael Cerullo
Thanks!!  This worked.