function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arvind010Arvind010 

Displaying a field in child object

In contact object there is a checkbox field 'primary program contact'. I want to display the contact name for which this checkbox field is checked
in the Account detail page. I'm using the following query to return the name of the contact. But i'm not able to find out the component tag to use in the page editor to display the contact name.
Code:
public Account getcin(){
cin=[select(SELECT Name  FROM Contacts where Primary_Contact_for_this_Company__c=true) FROM Account where OwnerId = :Userinfo.getUserId()];
return cin;
}

 Any suggestion on this page will be really helpful.

jwetzlerjwetzler
Code:
{!cin.name}
<apex:outputField value="{!cin.name}"/>
<apex:outputText value="{!cin.name}"/>

 Those should all work
Arvind010Arvind010
Code:
<apex:outputField value="{!cin.name}"/>

When i use this component tag, its showing the following error:
"Sobject row was retrieved without querying the requested field:name"

jwetzlerjwetzler
Sorry, I didn't look at your query close enough.  So within your select statement you are also querying for the list of the account's contacts (and you're only selecting the name for those contacts).

Since the actual field that your selecting on Account is contacts, you're going to get a list back.  So you might want to limit the list to 1 (I assume that there will only be one primary contact for your account) but do something like this:

Code:
public class extension {

    String id;
    Account cin;

    public extension(ApexPages.StandardController controller) {
        id = controller.getId();
    }
    
    public Account getCin(){
      if (cin == null) {
        cin=[select(SELECT Name  FROM Contacts where primary__c=true) FROM Account where id = :id];
      }
      return cin;
    }

    public Contact getPrimary() {
      return getCin().contacts.get(0);
    }
}

 
Then you can access {!primary.name}

Does that make sense?

You can access cin.contacts since that what you selected directly from the account, but since that's a list, the only way to get at the names is to use an iterating component.  Or to pull the contact out in your controller so you can access it directly.