+ Start a Discussion
JessBJessB 

Returning List of Contacts at Account on a Related Custom Object

Hi,

 

My Custom Object is called Training (API is Training__c). This Custom Object's Parent in the Master-Detail relationship is Accounts.

 

I currently have a VF page that sits on the Custom Object that shows only the iFrame for the related Contact list, but it allows users to edit the Contacts/create new ones from inside the frame, and therefore, it loses it's inital purpose: simply to list the Contact associated with the Account. I want to replace it with a simple table that returns a list of Contacts (First Name and Last Name, Title, Email).

 

 

I discovered I could create my own little table with a basic list, and put it on the Account page to test the list generation (works perfectly). My issue is that this same code does not work when I set the Standard Controller to "Training__c". I'm brand-new to VF so I know I need to adjust my code so that the same list that generates on the Account appears on my Custom object. 

 

I appreciate your help and feedback; I am trying to learn!

 

 

My VF page that works on the Account page layout:

<apex:page standardController="Account" tabStyle="Contact" >
    <apex:pageBlock title="Contact List">
    <apex:dataTable value="{!account.Contacts}"
        var="contact"
        cellPadding="4" border="0.5">
        <apex:column >
            <apex:facet name="header">Full Name</apex:facet>
           <apex:outputText value="{!contact.FirstName} {!contact.LastName}" escape="false" />
            </apex:column>
        <apex:column >
            <apex:facet name="header">Title</apex:facet>
            {!contact.Title}
            </apex:column>
        <apex:column >
            <apex:facet name="header">Email</apex:facet>
            {!contact.Email}
            </apex:column>
        </apex:dataTable>
    </apex:pageBlock>
</apex:page>

 

 

 

 

My class:

public class mySecondController{

    public Account getAccount() {
        return [select id, name,
                (select id, firstname, lastname
                 from Contacts limit 20)
                from Account where id=
                :System.currentPageReference()
                .getParameters().get('id')];
    }
}

 

Subramani_SFDCSubramani_SFDC

VF PAGE

 

<apex:page Controller="mySecondController">
<apex:pageBlock title="Contact List">
<apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4" border="0.5">
<apex:column >
<apex:facet name="header">Full Name</apex:facet>
<apex:outputText value="{!contact.FirstName} {!contact.LastName}" escape="false" />
</apex:column>
<apex:column >
<apex:facet name="header">Title</apex:facet>
{!contact.Title}
</apex:column>
<apex:column >
<apex:facet name="header">Email</apex:facet>
{!contact.Email}
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:page>

 

Controller

 

public class mySecondController{

public Account getAccount() {
return [select id, name,
(select id, firstname,Email, lastname,Title from Contacts limit 20) from Account where id=:System.currentPageReference().getParameters().get('id')];
}
}

 

 

Pass the id at the end like this https://c.ap1.visual.force.com/apex/star?id=0019000000jiMSy(Acc id)

 

 

u no need to use the standard controller.....replace the Training__c in the place of Account and replace the Account in the place of contact...Hope it will works...

JessBJessB

Thank you, I got it to work.

 

So I am testing this in Sandbox. How do I get it to go to Production? I have never created a test class before, and I get the error when using Developer Console in production that I cannot add a class to an active ord.

Subramani_SFDCSubramani_SFDC

Hi JessBes,

 

 

Test Clas  for ur controller..it covers 100%

 

@isTest
private class mySecondControllerTest{

static testMethod void mySecondController() {
Account a = new Account();
a.Name = 'test';
insert a;

Contact c = new Contact();
c.FirstName = 'Joe';
c.LastName = 'Schmoe';
c.AccountId = a.Id;
Insert c;
PageReference pgRef = Page.star; //Create Page Reference - 'star' is the name of Page
Test.setCurrentPage(pgRef); //Set the page for Test Method
ApexPages.currentPage().getParameters().put('id', a.id);//Pass Id to page
mySecondController m1=new mySecondController ();
m1.getAccount();
}
}