While preparing myself to the Developer 1 certification I met the following question:

A Visualforce page uses the Contact standard controller. How can a developer display the Name from the parent Account record on the page?

A) Use SOQL syntax to find the related Accounts Name field.

B) Use additional Apex logic within the controller to query for the Name field.

C) Use the (contact.Account.Name) merge field syntax

D) Use an additonal standard controller for Accounts

I answered C), while the correct answer is A). Could someone explain to me why, please?

Why did I answer C)? Because I saw a few examples where such an access (field chaining) was possible. For example, here:

<apex:pageBlockTable value="{!account.contacts}" var="contact"> 
 <apex:column value="{!contact.name}"/>
 <apex:column value="{!contact.email}"/>

Here we have the following chain: account.contacts[i].name (where i is an iteration variable, index in the array).


Basically, for a related object, we can only get Ids for the record like Contact.AccountId.

To fetch rest of the Account's attribute you need to separately execute SOQL query.

Like this:

Contact con = [SELECT Account.Name, Account.BillingStreet FROM Contact WHERE Id=:contactId];

If you try to execute through merge field (contact.Account.Name) then it will not return value, that is displayed as blank without throwing any errors.

So, correct answer is A

  • It is a pity, but you are wrong. I will provide my answer below, please, if possible, have a look at it and point out if I am missing something there. – iloveseven Aug 21 '18 at 19:17

The correct answer is C). According to the documentation:

You can traverse up to five levels of child-to-parent relationships. For example, if using the Contact standard controller, you can use {!contact.Account.Owner.FirstName} (a three-level child-to-parent relationship) to return the name of the owner of the account record that is associated with the contact.

That means we can use {!contact.Account.Name}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.