1

I have a custom Object App which is having lookup to Account object and Contact object is having lookup to Account.

I have to show all Contact records in App object in related list.

Is this possible through configuration or customization.

  • Is your custom object App__c is the child of Account? – Mahmood Nov 10 '17 at 6:15
  • yes and Contact is child of Account – devsfdc Nov 10 '17 at 6:16
  • Not possible with point-and-click. You will need to have a VF page populating all the contacts against the parent account and then use that VF page as related list, by editing the page layout. – Mahmood Nov 10 '17 at 6:19
  • Thank you @Mahmood, can I ask you for a sample vf code for such scenario.I am noobie in terms of VFpages. – devsfdc Nov 10 '17 at 6:21
3

This code works for me

<apex:page standardcontroller="app__c"> <apex:relatedList subject="{!app__c.Account__c}" list="Contacts"/> </apex:page>

| improve this answer | |
  • +1 Nice! I didn't know about the subject attribute in the apex:relatedList component. – Mahmood Nov 10 '17 at 7:38
0

Not possible with point-and-click development. You will need to have a VF page populating all the contacts against the parent account and then use that VF page as related list, by editing the page layout.

Sample Controller extension

public class AccountContactsController 
{
    private final id accId;

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

    public AccountContactsController(ApexPages.StandardController stdController) 
    {
        this.accId = (App__c)stdController.getRecord().Account_id__c == null? null: (App__c)stdController.getRecord().Account_id__c;

        if (accId != null) 
            contacts = [SELECT id, lastname FROM Contact WHERE AccountId =: accId ];
    }
}

Sample VF page

<apex:page standardController="App__c" extensions="AccountContactsController" >
    <apex:repeat value="{!contacts}" var="contact" id="contactlist">
        <apex:outputText value="{!contact}" id="contact" /><br/>
    </apex:repeat>
</apex:page>

Above code is not tested and created on the fly, it is just to give you an idea. For more information you should do the trails related to VF pages Standard controller, controller extensions, and apex basics.

| improve this answer | |
  • I didn't know your exact object name, and just wanted to give you an idea from where you could pick things up. Secondly, I am not sure how are you able to display related contacts without the controller extension. – Mahmood Nov 10 '17 at 7:20
  • I think Subject parameter in <apex:relatedList> is the key here. It's an inbulid thing,so don't know much.But it works by simply putting vf page in 'App' object detail page. – devsfdc Nov 10 '17 at 7:24
0

You can use the code below for your requirement:-

public class customController 

{

public list<account> alist;
public list<App__c> cclist{get;set;}
public list<contact> clist{get;set;}  
public string  op;
set<id> accId;

public customController(ApexPages.StandardController stdController)

{    
    accId=new set<id>();
    list<App__c> cclist=new list<App__c>();
    op=ApexPages.currentPage().getParameters().get('id'); 
    cclist=[select id,account__c from App__c where  id =:op];
 for(App__c c:cclist)
 {
     accId.add(c.account__c);
 }
 clist=[select id,lastname from contact where accountid in:accId];
}}

Page:-

 <apex:page standardController="App__c" extensions="customController" >


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.