Why I can't access to opportunity account name on apex/vf ?


I'm creating a opportunity VF page, extending the standarcontroller:

 <apex:page standardController="Opportunity" extensions="test1Extension">

On my extension class I'm setting the opp object using:

 opp = (Opportunity) controller.getRecord();

When I try to reference account on the vf by {!opp.Account.Name} I get this error:

SObject row was retrieved via SOQL without querying the requested field: Opportunity.Account

In addition, I've tried to query the account using a subquery:

opp =[select Id, (select Id from Account where Id= opportunity.AccountId) from Opportunity where id=:opp.Id];

Or Account acc = [select Id from Account where id=:opp.AccountId];

SObject row was retrieved via SOQL without querying the requested field: Opportunity.AccountId


1) Why I can't reference Account from Opportunity ? 2) What is the proper why to make that sub-query?

thx in advance

  • If my answer isn't clear, please comment on it and I will happily clarify further :)
    – Doug B
    Commented May 16, 2013 at 16:53
  • @DougB ten aswer was perfect, it was just what i was looking for. Thx Commented May 16, 2013 at 17:21

3 Answers 3


Since you have the standard controller there you can refer to fields through that and they will be automatically queried. In this case you would refer to


on the VF and that would work.
The prefix Opportunity. is referring to the standard controller (rather than your extension variable Opp.
Any fields that you include on the VF page from the Standard Controller (Opportunity.) will be automatically queried. You don't need to do anything in your extension to make that data available.

If you had wanted to use your own variable Opp on the page (to have Opp.Account.Name), you were nearly there with the approach you had you just needed to phrase the query correctly, the syntax you would want is

Opp = [select id, account.name from opportunity where id=:opp.Id]

when we get records from controller.getRecord(); it will return all the fields of the same object and only id fields its parent's objects , to access all the remaining fields of parent's object you have to query them.

Also in the SOQL query you write all the name of fields which you want to access. For e.g to access the name of Account your query will be as follows :

opp =[select Id, Account.Name from Opportunity where id=:opp.Id];

You can write inner query when you want to access its child's object records.

  • The Standard Controller does not return all fields of the object, it only returns those referenced on the page. You can also use the .addFields method but I find some problems with this in test classes.
    – Doug B
    Commented May 16, 2013 at 14:03

What if I want to access child object using parent object's query? for Example, based on Opportunity's name I want to search but using Account object something like :

Account acc = [select a.Id from Account a where a.opportunity.name='XYZ'];

You must log in to answer this question.

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