+ Start a Discussion
neckrneckr 

Is there a way to display a Child Field Set using the Standard Controller of the Parent Object?

Hi,

 

I am looking to display a fieldset from my Contacts Object within the visualforce page that uses the Standard Account Controller.  I am not sure what notation to use  to call the Fieldset in the Contact (Child) object?????

 

FieldSet Name linked  to Account Object: Con_Account_Company_Info

FieldSet Name linked  to Contact Object: Con_Account_Contact_Info

 

Here is an example of a fieldset in the Account Object that works fine, which I want to mimic for the Contacts Object.

 

<apex:page standardController="Account" showHeader="false" tabStyle="account" >

 

<apex:pageBlockSection columns="2" title="Account Information" >
<apex:repeat value="{!$ObjectType.Account.FieldSets.Con_Account_Company_Info}" var="f">
<apex:outputField value="{!Account[f]}"/>
</apex:repeat>

 

Following the Dot Notation for calling child objects I was thinking the notation may look something like what I have below:

 

<apex:pageBlockSection columns="2" title="Contact Information" >
<apex:repeat value="{!$ObjectType.Account.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!Account.Contact[f]}"/>
</apex:repeat>

 

Please let me know if you can  help.   I will need to repeat this notation a few times within some custom objects as well, so If there is anything I should know about doing this with custom child objects of custom parent objects, please advise also.

 

Thanks,


Ricky

 

Best Answer chosen by Admin (Salesforce Developers) 
neckrneckr

Thank you.  I reversed the single quotes and it work

 

rendered="{!c.Contact_Type__c ='Primary Contact'}"

 

I noticed the formatting was off so I experimented with the rendered attribute directly in my outputField and it worked. :)  See my final code below. 

 

                 <apex:pageBlockSection columns="2" title="Contact Information" >
                        <apex:repeat value="{!Account.Contacts}" var="c">
                              <apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
                                     <apex:outputField rendered="{!c.Contact_Type__c ='Primary Contact'}" value="{!c[f]}"/>
                              </apex:repeat>
                         </apex:repeat>
                    </apex:pageBlockSection>

All Answers

aballardaballard

To access a contact fieldset you would just use !$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}

So your code would be something like

 

<apex:pageBlockSection columns="2" title="Contact Information" >

<apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!Account.Contact[f]}"/>
</apex:repeat>

 

 

neckrneckr

Thank you for the quick reply.

 

I tried the notation you suggested and I receievd the following error on the visual force page:

 

ErrorError: Invalid field Contact for SObject Account

 

Here is the code:

 

<apex:pageBlockSection columns="2" title="Account Information" >
<apex:repeat value="{!$ObjectType.Account.FieldSets.Con_Account_Company_Info}" var="f">
<apex:outputField value="{!Account[f]}">
</apex:outputfield>
</apex:repeat>
</apex:pageBlockSection>

<apex:pageBlockSection columns="2" title="Account Information" >
<apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!Account.Contact[f]}"/>
</apex:repeat>
</apex:pageBlockSection>

aballardaballard

oh, yes... account to contact is one to many so that doesn't make sense.  Did you need another loop, or a pageblocktable to iterate over all contacts?  Or do you have a custom field selecting a specific contact you want to display? 

 

Maybe something like

<apex:repeat value="{!Account.contacts}" var="c">

<apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!c[f]}"/>
</apex:repeat>

<br/>

</apex:repeat>

neckrneckr

I follow you.  I only need one contact record dispalyed where my cutsom field in the Contacts Object, Contact_Type__c = "Principal"

neckrneckr

Thank you! The code you provided above works.  Where can I enter the logic to only display records where Contact_Type__c = "Primary Contact."  I prefer to use the fieldsets as opposed to display the fields indepently and from my knowledge I need to use a loop to dipslay the field set. I tried this, however the notation for my if statement is not reading as code and is displaying on my page.

 

 

<apex:pageBlockSection columns="2" title="Contact Information" >
<apex:repeat value="{!Account.Contacts}" var="c">
if (Contact.Field.Contact_Type__c ="Primary Contact") {
<apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!c[f]}"/>
</apex:repeat>
}
</apex:repeat>
</apex:pageBlockSection>

 

Also is it possible to have inline edit for the Contact fields that are displayed in my code above. I have the code below in my page block however it does not allow me to enable the contact object fields but works fine for the account object fields.  Please adivise.  Thanks!!!

 

<apex:inlineEditSupport event="ondblClick" showOnEdit="saveButton,cancelButton" hideOnEdit="editButton" />

aballardaballard

You can't embed if statements or other code into the page like that.   You should be able to use the rendered attribute on a component to display only the record you care about.   Something like should work:

 

<apex:pageBlockSection columns="2" title="Contact Information" >
<apex:repeat value="{!Account.Contacts}" var="c">
<apex:outputPanel rendered={!c.ContactType__c ="Primary Contact"}>

<apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
<apex:outputField value="{!c[f]}"/>
</apex:repeat>
</apex:outputPanel>
</apex:repeat>

 

Sorry, not sure about the inlineEdit issue.

neckrneckr

Ok... I Get the following error:  The element type "apex:outputPanel" must be terminated by the matching end-tag "</apex:outputPanel>".   Looks like the outputPanel component can not be within the repeat component.  Anything I am missing?

 

<apex:pageBlockSection columns="2" title="Contact Information" >
    <apex:repeat value="{!Account.Contacts}" var="c">
        <apex:outputPanel rendered="{!c.Contact_Type__c ="Primary Contact"}">
               <apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
                    <apex:outputField value="{!c[f]}"/>
               </apex:repeat>
         </apex:outputPanel>  
     </apex:repeat>
</apex:pageBlockSection>

aballardaballard

OutputPanel inside repeat is ok.   The error message is really bad.   But I think the problem is with the rendered attribute, where you have a quoted string inside a quoted string.   Use single quotes for the outer pair and it should be ok...

 

rendered='{!c.Contact_Type__c ="Primary Contact"}'

 

neckrneckr

Thank you.  I reversed the single quotes and it work

 

rendered="{!c.Contact_Type__c ='Primary Contact'}"

 

I noticed the formatting was off so I experimented with the rendered attribute directly in my outputField and it worked. :)  See my final code below. 

 

                 <apex:pageBlockSection columns="2" title="Contact Information" >
                        <apex:repeat value="{!Account.Contacts}" var="c">
                              <apex:repeat value="{!$ObjectType.Contact.FieldSets.Con_Account_Contact_Info}" var="f">
                                     <apex:outputField rendered="{!c.Contact_Type__c ='Primary Contact'}" value="{!c[f]}"/>
                              </apex:repeat>
                         </apex:repeat>
                    </apex:pageBlockSection>

This was selected as the best answer