1

I have the following code in my unit test to insert data:

  Account account = new Account();
  account.Name = 'Account Name';
  insert account;

 Opportunity opportunity = new Opportunity(
   Name = 'Test Opportunity',
   CloseDate = Date.today() + 1,
   StageName = 'Closed Won',
   AccountId = account.ID
 );
 insert opportunity;

I then execute the following nested SOQL query:

SELECT Id,account.Name,account.Type,account.ParentId,account.BillingStreet,account.OperatingHoursId,(SELECT IsPrimary,Role,ContactId,contact.name,contact.email,contact.fax,contact.MobilePhone FROM OpportunityContactRoles), (SELECT Product2.Name,Product2.Description,Id,OpportunityId,SortOrder,PricebookEntryId,Product2Id,ProductCode,Name,Quantity,TotalPrice,UnitPrice,ListPrice,ServiceDate,Description,CreatedDate,CreatedById,LastModifiedDate,LastModifiedById,SystemModstamp,IsDeleted FROM OpportunityLineItems) FROM Opportunity

However it returns only opportunity data and not account data:

(Opportunity:{Id=0062100000DI3JZAA1, IsDeleted=false, AccountId=0012100000twnhAAAQ, IsPrivate=false, Name=Test Opportunity, StageName=Closed Won, Probability=100, CloseDate=2021-01-04 00:00:00, IsClosed=true, IsWon=true, ForecastCategory=Closed, ForecastCategoryName=Closed, HasOpportunityLineItem=false, OwnerId=00521000006SxqyAAC, CreatedDate=2021-01-04 21:13:41, CreatedById=00521000006SxqyAAC, LastModifiedDate=2021-01-04 21:13:41, LastModifiedById=00521000006SxqyAAC, SystemModstamp=2021-01-04 21:13:41, FiscalQuarter=1, FiscalYear=2021, Fiscal=2021 1, LastViewedDate=2021-01-04 21:13:41, LastReferencedDate=2021-01-04 21:13:41, HasOpenActivity=false, HasOverdueTask=false})
13:13:40.516 (6981223444)

It does have the account ID, but I was expecting to see the account name as well since it was created in the test data. When I run this against production data it works fine.

1 Answer 1

2

This is just a System.debug behavior, logs sometimes skip displaying the information if it is too long.

I just tested your SOQL in a test class and it did work fine. If you want to see the info do a System.debug on the Opportunity like below and should display fine.

Opportunity opp = [Select Account.Name FROM Opportunity];
system.debug('opportunity Account Name ' + opp.Account.Name);

The alternative is to set log levels for Apex to FINE. This prints finer details like values assigned to variables so you can easily check the variable values.

2
  • in my case I'm using database.query and assigning the results to List<SObject> objects. I tried doing objects.get(0).get('name')); and that gets assigned the opportunity name as expected. But if I do objects.get(0).get('Account.name')); then it returns an error System.SObjectException: Invalid field Account.name for Opportunity
    – Felix
    Commented Jan 5, 2021 at 5:42
  • 1
    Looks like I had to convert the SObject into an Opportunity object first
    – Felix
    Commented Jan 5, 2021 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .