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
Raquel RiemerRaquel Riemer 

Create a visualforce table with fields from related lists

I am somewhat new to this to this area of salesforce so please be patient.

I want to create a table using visual force that can be accessed from a button residing Travel__c

I want the following fields in the table that are either directly or indirectly related to that specific Travel__c

Field 1 is from Tour__Interests__r (which is a child of master/detail with Travel__c)  Contact__c which is a (lookup to the contact) First Name and then Last Name

Field 2 would take the same path as above and then pick up a related list to the contact called Recognitions__c and the fields I want from there are Amount, Date, Name
And if there are no Recognitions either that Tour Interest Record can be skipped or the recognition fields can be null

I have tried multiple ways to get the related list fields but cannot get there. I believe I need to build a custom controller as the error seems to be it does not recognize any of the Travel Interests fields.

Help


 
Raquel RiemerRaquel Riemer
Note: I am still using classic but will be converting soon
Britto Fernandez 2Britto Fernandez 2
Dear Raquel,

Roughly it would be translated as 
- In the method called from button, use 1 or multiple SOQL on Tour__Interests__r and populate/manipulate result in a list.
- Above list display in VF.

If you already have a code, it can be resolved faster by sharing screen via skype/email(madhukar.reddy@heptarc.com) or zoom. 
Deepali KulshresthaDeepali Kulshrestha
Hi Raquel,

Please go through with below code, it may be helpful to you.

Step 1: Create a Visualforce Page:ContactRelatedList.vfp 


    <apex:page standardController="Account" extensions="ContactRelatedListController" sidebar="true" showHeader="false" >     

      <apex:form >                 

        <apex:pageblock id="ContactList"> 

          <div style="margin-left: 30%;"><apex:commandLink value="New Contact" action="{!newContact}" target="_parent" styleClass="btn" style="text-decoration:none;padding:4px;"/></div> 

            <br/> 

            <apex:pageBlockTable value="{!contacts}" var="cont" rendered="{!NOT(ISNULL(contacts))}">                 

                <apex:column HeaderValue="Action" width="60"> 

                    <apex:commandLink value="Edit" style="color:#015ba7;" action="{!editContact}" target="_parent" ><apex:param value="{!cont.id}" name="contactId"/> 

                    </apex:commandLink> 

                    &nbsp;|&nbsp; 

                    <apex:commandLink value="Del" onclick="return confirm('Are you sure?')" style="color:#015ba7;" action="{!deleteContact}" target="_parent"><apex:param value="{!cont.id}" name="contactId"/> 

                    </apex:commandLink> 

                </apex:column> 

                <apex:column headerValue="Contact Name"><apex:outputLink value="/{!cont.id}" target="_blank">{!cont.Name}</apex:outputLink> </apex:column>               

                <apex:column value="{!cont.region__c}"/> 

                <apex:column value="{!cont.MobilePhone}"></apex:column> 

                <apex:column value="{!cont.Email}"/>              

            </apex:pageBlockTable>            

            <apex:outputLabel value="No records to display" rendered="{!(ISNULL(contacts))}" styleClass="noRowsHeader"></apex:outputLabel> 

        </apex:pageblock> 

     </apex:form>  

   </apex:page>

 Step 2: Create a controller extension :-  ContactRelatedListController.apxc
 
public class ContactRelatedListController {     

    public List<contact> contacts{get;set;} 

    public Account accounts {get;set;} 

    public Account acc {get;set;} 

     

    //Constructor 

    public ContactRelatedListController(ApexPages.StandardController controller) { 

        acc = (account)controller.getRecord();      

        accounts = [SELECT id FROM account WHERE id=: acc.id LIMIT 1]; 

        contacts = [SELECT id,region__c,Name, mobilephone, email FROM contact WHERE accountid = :accounts.id ORDER BY Name];     



    //This method is to create a new contact while clicking on the Add contact button 

    public pageReference newContact(){  

        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/003/e?&retURL=' + accounts.id);  

        return pageRef; 

    } 

    //This method is to edit the existing contact record while clicking the Edit link 

    public pageReference editContact(){  

        String contactId = Apexpages.currentpage().getParameters().get('contactId');  

        pageReference pageRef = new pageReference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + contactId + '/e?retURL=' + accounts.id);  

        return pageRef;  

    }    


 //This method is to delete the contact record while clicking the Del link 

    public pageReference deleteContact(){  

        String contactId = Apexpages.currentpage().getParameters().get('contactId');  

        contact contactList = [SELECT Id FROM contact WHERE id = : contactId LIMIT 1]; 

        delete contactList; 

        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm(); 

        PageReference redirectPage = new PageReference(baseUrl+'/'+accounts.id); 

        return redirectPage;  

    }   

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha