+ Start a Discussion
Stéphane C.Stéphane C. 

Display on a Visualforce page object parent records with child object records

Hi,

I want to display on the same Visualforce page, the "Contact" records and the "Course" records.

I want to display on the same line the name of the student and the courses who is following. If the student is following several courses (n courses) so I want to display n lines (other ideas are welcome). I want to be able to inline edit the records.

I have create a controller :
public class inlineDetail {
public List<contact> acclst=new List<contact>();

public List<contact> getContacts() {
 acclst=[select name,accountid,title,assistantphone,phone,(select name from courses__r) from contact];
      return acclst;
}

public void saveChanges() {
     update acclst;
}
}

And I have create a VisualForce page :
 
<apex:page controller="inlineDetail">
<apex:form >
<apex:pageBlock mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandbutton value="save" action="{!saveChanges}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!contacts}" var="c">
<apex:column Headervalue="Nom">
<apex:outputfield value="{!c.name}"/>
</apex:column>
<apex:column headervalue="Téléphone">
<apex:inputfield value="{!c.phone}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

<apex:relatedList list="Course__r">
<apex:facet name="header">Titles can be overriden with facets</apex:facet>
</apex:relatedList>

</apex:page>

​I cannot see how to make it works together.

Thank you.
Best Answer chosen by Stéphane C.
Stéphane C.Stéphane C.
Here is one solution :
 
public class inlineDetail {

	public List<Contact> contactsList = new List<Contact>();   
    
    public List<Contact> getContacts() {
        contactsList = [SELECT Name,accountId,Phone,AssistantPhone,Email, (SELECT Name, Classroom__c, Comments__c from Courses__r) FROM Contact ORDER BY Name];
        return contactsList;
    }
    
    public void saveChanges() {
        List<Course__c> courses = new List<Course__c>();
        for (Contact c : contactsList) {
			courses.addAll(c.Courses__r);
		}
		update courses;
        update contactsList;
    }
    
}

Any comments will be appreciated.

All Answers

Nayana KNayana K
<apex:page controller="inlineDetail">
<apex:form >
<apex:pageBlock mode="inlineEdit">
<apex:pageBlockButtons >
<apex:commandbutton value="save" action="{!saveChanges}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!contacts}" var="c">
<apex:column Headervalue="Nom">
<apex:outputfield value="{!c.name}"/>
</apex:column>
<apex:column headervalue="Téléphone">
<apex:inputfield value="{!c.phone}"/>
</apex:column>
<apex:column headervalue="Courses">
                  <apex:facet name="Facet Name">
                   </apex:facet>
                      <apex:pageblocktable value="{!c.courses__r}" var="course">
                         <apex:column headervalue="Name">
                            <apex:inp value="{!course.Name}"/>
                         </apex:column>
                         
                      </apex:pageblocktable>                           
               </apex:column>           
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>

May be you can try something like this. 
Stéphane C.Stéphane C.
HI Nayana and thannk you,

I have a wlak around your code and I have some issues. And I don't know if it's me ?!

Perhaps we could work with a standard object like the opportunities.

So I want to list all the contact and I want to display if an opportunity is linked to a contact. An opportunity can have several contacts.

So I decide to rewrite my code to use the same format as your code :
 
<!-- Shows a table of contacts associated with an opportunity.
The contact column headers are controlled by the facets.-->

<apex:page controller="inlineDetail">
    <apex:pageBlock title="Contacts" mode="inlineEdit">
        <apex:dataTable value="{!contacts}" var="c" cellPadding="4" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                        {!c.Name}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Phone</apex:facet>
                        {!c.Phone}
            </apex:column>

            <apex:column >
                <apex:facet name="header">Opportunity</apex:facet>
                        <apex:pageblocktable value="{!c.opportunites__r}" var="o">
                                {!o.Name}
                        </apex:pageblocktable>
            </apex:column>

			<apex:column headervalue="Opportunity">
                  <apex:facet name="Facet Name">
                   </apex:facet>
                      <apex:pageblocktable value="{!c.opportunities__r}" var="o">
                         <apex:column headervalue="Name">
                            {!o.Name}
                         </apex:column>
                         
                      </apex:pageblocktable>                           
			</apex:column>  
            
        </apex:dataTable>
    </apex:pageBlock>
</apex:page>
 
public class inlineDetail {
public List<contact> acclst=new List<contact>();

public List<contact> getContacts() {
 acclst=[select name,accountid,title,assistantphone,phone,(select name from Opportunities__r) from contact];
      return acclst;
}

public void saveChanges() {
     update acclst;
}
}

Screenshot

Any idea what it is going wrong?

Thank you.
Stéphane C.Stéphane C.
Ok. I'm going further.
public class inlineDetail {
public List<contact> acclst=new List<contact>();

public List<contact> getContacts() {
 acclst=[select name,accountId,phone,AssistantPhone,email, (select name, classroom__c, comments__c from Courses__r) from Contact];
      return acclst;
}

public void saveChanges() {
     update acclst;
}
}
<!-- Shows a table of contacts associated with courses.
The contact column headers are controlled by the facets.-->

<apex:page controller="inlineDetail">
    <apex:form>
        
    <apex:pageBlock title="Contacts" mode="inlineEdit">
        
    <apex:pageBlockButtons>
		<apex:commandButton value="Save" action="{!saveChanges}"/>
	</apex:pageBlockButtons>
        
        <apex:dataTable value="{!contacts}" var="s" cellPadding="4" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                    {!s.Name}
            </apex:column>
			<apex:column >
            <apex:facet name="header">Account</apex:facet>
                    {!s.accountId}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Phone</apex:facet>
                	<apex:inputField value="{!s.Phone}"/>
            </apex:column>
            <apex:column>
                <apex:facet name="header">Phone Assistant</apex:facet>
            	{!s.AssistantPhone}
			</apex:column>
            <apex:column>
                <apex:facet name="header">eMail</apex:facet>
            	{!s.email}
			</apex:column>

            <apex:column >
                <apex:facet name="header">Course</apex:facet>
                        <apex:pageblocktable value="{!s.Courses__r}" var="c">
                            <apex:column >
								<apex:inputField value="{!c.Name}"/>
                            </apex:column>
                            <apex:column >
								{!c.Classroom__c}
                            </apex:column>
                            <apex:column >
								<apex:inputField value="{!c.comments__c}"/>
                            </apex:column>
                </apex:pageblocktable>
            </apex:column>
            
        </apex:dataTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>
Screenshot

So, now I want to save the input data of the last column (like "Ok, it seems to work!"). It doesn't work because this is a child object records.

Is it possible? Any idea to do it?

Thank you.
Stéphane C.Stéphane C.
Here is one solution :
 
public class inlineDetail {

	public List<Contact> contactsList = new List<Contact>();   
    
    public List<Contact> getContacts() {
        contactsList = [SELECT Name,accountId,Phone,AssistantPhone,Email, (SELECT Name, Classroom__c, Comments__c from Courses__r) FROM Contact ORDER BY Name];
        return contactsList;
    }
    
    public void saveChanges() {
        List<Course__c> courses = new List<Course__c>();
        for (Contact c : contactsList) {
			courses.addAll(c.Courses__r);
		}
		update courses;
        update contactsList;
    }
    
}

Any comments will be appreciated.
This was selected as the best answer