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
Laurie DrewLaurie Drew 

Help displaying a Related List

Hi All,
Not sure if this is even possible but here we go...
I have 2 custom objects 'History' and 'Data' that are both children of the Contact object.  Is there a way that I can display 'History' as a related list on the page layout for a record in the 'Data' object if both are children of the same contact record?  There can be more than one 'History' record and more than one 'Data' but they are always tied back to the parent Contact.  I would really appreciate any ideas/help anyone can suggest?
Best Answer chosen by Laurie Drew
Wilfredo Morillo 20Wilfredo Morillo 20

You should not use a related list. Create an extension for data with a list of History where the history contact = data contact. Check this link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

Your class would look something like this but i dont know your fields:
 
public class dataExtension {

	private data__C myData;
    private list<History__c> RelatedHistory;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.myData = (Data__c)stdController.getRecord();
    }

    public List <History> getRelatedHistory() {
        try{
        	RelatedHistory = [Select id,name from History__c where contactid = :myData.contactId];
        }Catch (Exception e){
        	RelatedHistory = null;

        }
        retrun RelatedHistory;
    }
}

And then use  related history list with an <Apex:pageblockTable>.

Wil, 

All Answers

Wilfredo Morillo 20Wilfredo Morillo 20
You can use Visualforce and an extension controller for Data. 

wil, 
Laurie DrewLaurie Drew
I created a lookup relationship on Data to History and can get History to display as a related list on Data, but when any new data record is created it does not display in the list.  Since there can be multiple History records I then created the following Map<String to query History and return all the field values:

public class Data {
    
    Map<String, History__c> myMap = new Map<String, History__c>(); 
        for(History__c objDH : [Select H.Id, H.Donated__c,  H.Amount__c, H.Date__c From History__c H])
        myMap.put(objDH.Name, objDH);

}
But cannot save, receive problem message:
Expecting '}' but was: 'for'


Then I created the following vf page:

<apex:page standardController="Data">
<apex:relatedList list="History__c"  subject="{Data.objDH}"/>
</apex:page>


But cannot save, receive problem message
'Formula expression is required for attribute subject in in Data at line 2 column 62.'

Help please??
Wilfredo Morillo 20Wilfredo Morillo 20

You should not use a related list. Create an extension for data with a list of History where the history contact = data contact. Check this link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

Your class would look something like this but i dont know your fields:
 
public class dataExtension {

	private data__C myData;
    private list<History__c> RelatedHistory;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.myData = (Data__c)stdController.getRecord();
    }

    public List <History> getRelatedHistory() {
        try{
        	RelatedHistory = [Select id,name from History__c where contactid = :myData.contactId];
        }Catch (Exception e){
        	RelatedHistory = null;

        }
        retrun RelatedHistory;
    }
}

And then use  related history list with an <Apex:pageblockTable>.

Wil, 
This was selected as the best answer
Laurie DrewLaurie Drew
Thank you so much!