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
megmooPDXmegmooPDX 

display all fields of most recent related list record on parent object?

I have three custom objects (A, B, C with A being the parent of B; B being the parent of C).

Goal: Create a single console screen with :
  • all details for Object A
  • all details for the most recent Object B
  • all records for Object C that are related to the most recent Object B
     
1. Is this possible?
2. Can anyone give me a high-level starting point so I can start researching from there

I've tried lots of things with creating different Visualforce pages to display different details from the various Objects, but i just can't get it to work. 
Best Answer chosen by megmooPDX
GlynAGlynA
<pre>
public class risk_ControllerExtension
{
    public Object_A__c          aRecord     { get; set; }

    public Risk_Profile__c      riskProfile
    {
        get
        {
            if ( riskProfile == null )
            {
                riskProfile =
                [   SELECT  Id, Attributed_NPI__c, Attributed_Provider__c,
                            Total_Member_Priority_Level__c, Total_Potential_Financial_Value__c,
                            Total_Potential_Risk_Score__c
                    FROM    Risk_Profile__c
                    WHERE   Object_A__c = :aRecord.Id
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }
            return riskProfile;
        }
        private set;
    }

    public List<Object_C__c>    cRecords
    {
        get
        {
            if ( cRecords == null )
            {
                cRecords =
                [   SELECT  Id  //  and other fields...
                    FROM    Object_C__c
                    WHERE   Risk_Profile__c = :riskProfile.Id
                ];
            }
            return cRecords;
        }
        private set;
    }

    public risk_ControllerExtension( ApexPages.StandardController sc )
    {
        aRecord = (Object_A__c) sc.getRecord();
    }
}

<apex:page standardController="Object_A__c" extensions="">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection title="Object_A Information" >
            <apex:outputField value="{!aRecord.Name}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Risk Profile Information">
            <apex:outputField value="{!riskProfile.Attributed_NPI__c}"/>
            <apex:outputField value="{!riskProfile.Attributed_Provider__c}"/>
            <apex:outputField value="{!riskProfile.Total_Member_Priority_Level__c}"/>
            <apex:outputField value="{!riskProfile.Total_Potential_Financial_Value__c}"/>
            <apex:outputField value="{!riskProfile.Total_Potential_Risk_Score__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockTable value="{!cRecords}" var="cRecord">
            <apex:column value="{!cRecord.Field_1__c}">
                <apex:facet name="header">Field 1</apex:facet>
            </apex:column>
            <apex:column value="{!cRecord.Field_2__c}">
                <apex:facet name="header">Field 2</apex:facet>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
</pre>

All Answers

GlynAGlynA
Create a page that uses the standard controller for "A".  Create a controller extension that gets the "A" record from the standard controller, and queries for the most recent "B" record.  Something like this:

B bRecord = [SELECT Id, other fields FROM B ORDER BY CreatedDate DESC LIMIT 1];

Then you can get all the "C" records related to the "B" record:

List<C> cRecords = [SELECT Id, other fields FROM C WHERE B_Lookup__c = :bRecord.Id];

In the VF page, display the details for the "A" and "B" records, then use a "pageBlockTable" (or maybe a "repeat") to show all the "C" records:

<apex:pageBlockTable value="{!cRecords}" var="crecord" >
    <apex:outputField value="{!crecord.A_Field__c}"/>
    and so on...
</apex:pageBlockTable>

I hope this helps.  Let me know if you have any more questions.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
megmooPDXmegmooPDX
Thanks, Glyn! Using your advice and some code I found in a blog post, I successfully created the controller to get B record like this:

public class risk_Controller
    {
        public String  propNPI{ get; set; }
        public String  propAttProvider{ get; set; }
        public Double  propPriorityLevel{ get; set; }
        public Double  propFinancialValue{ get; set; }
        public Double  propRiskScore{ get; set; }
       

    public risk_Controller()
        {
              Risk_Profile__c obj = [SELECT   id,
                                              Attributed_NPI__c,
                                              Attributed_Provider__c,
                                              Total_Member_Priority_Level__c,
                                              Total_Potential_Financial_Value__c,
                                              Total_Potential_Risk_Score__c 
                                     FROM Risk_Profile__c limit 1];
              propNPI = obj.Attributed_NPI__c;
              propAttProvider = obj.Attributed_Provider__c;
              propPriorityLevel = obj.Total_Member_Priority_Level__c;
              propFinancialValue = obj.Total_Potential_Financial_Value__c;
              propRiskScore = obj.Total_Potential_Risk_Score__c;
                
             

        }
     }

***************************************
Now I need to get the C records. I'm a newbie to apex coding, so I'm not sure how to put the records into the list variable. Can you possibly provide any guidance on the code for your psuedocode related to Object C?

List<C> cRecords = [SELECT Id, other fields FROM C WHERE B_Lookup__c = :bRecord.Id];

Thank you for any help you can provide - much appreciated!
GlynAGlynA
One possible problem with your code is that, while it gets a Risk_Profile__c record, it's an arbitrary one - not necessarily the one associated with your "Object A" record.

The code in the following post is an alternative.  I've included some VF code as well to see how the controller extension is used.  Let me know if you have any questions or find any typos.

-Glyn

GlynAGlynA
<pre>
public class risk_ControllerExtension
{
    public Object_A__c          aRecord     { get; set; }

    public Risk_Profile__c      riskProfile
    {
        get
        {
            if ( riskProfile == null )
            {
                riskProfile =
                [   SELECT  Id, Attributed_NPI__c, Attributed_Provider__c,
                            Total_Member_Priority_Level__c, Total_Potential_Financial_Value__c,
                            Total_Potential_Risk_Score__c
                    FROM    Risk_Profile__c
                    WHERE   Object_A__c = :aRecord.Id
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }
            return riskProfile;
        }
        private set;
    }

    public List<Object_C__c>    cRecords
    {
        get
        {
            if ( cRecords == null )
            {
                cRecords =
                [   SELECT  Id  //  and other fields...
                    FROM    Object_C__c
                    WHERE   Risk_Profile__c = :riskProfile.Id
                ];
            }
            return cRecords;
        }
        private set;
    }

    public risk_ControllerExtension( ApexPages.StandardController sc )
    {
        aRecord = (Object_A__c) sc.getRecord();
    }
}

<apex:page standardController="Object_A__c" extensions="">
<apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection title="Object_A Information" >
            <apex:outputField value="{!aRecord.Name}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Risk Profile Information">
            <apex:outputField value="{!riskProfile.Attributed_NPI__c}"/>
            <apex:outputField value="{!riskProfile.Attributed_Provider__c}"/>
            <apex:outputField value="{!riskProfile.Total_Member_Priority_Level__c}"/>
            <apex:outputField value="{!riskProfile.Total_Potential_Financial_Value__c}"/>
            <apex:outputField value="{!riskProfile.Total_Potential_Risk_Score__c}"/>
        </apex:pageBlockSection>
        <apex:pageBlockTable value="{!cRecords}" var="cRecord">
            <apex:column value="{!cRecord.Field_1__c}">
                <apex:facet name="header">Field 1</apex:facet>
            </apex:column>
            <apex:column value="{!cRecord.Field_2__c}">
                <apex:facet name="header">Field 2</apex:facet>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
</pre>

This was selected as the best answer
megmooPDXmegmooPDX
Thank you so much for your help. Right now, I'm getting the following error (FYI ACA_Member__c is the name of Object A):

SObject row was retrieved via SOQL without querying the requested field: ACA_Member__c.Name__c

I'll post the code as I edited it (with my custom object names). Maybe I've made a typo somewhere? Again, thank you so much for any help you can provide. 


public class risk_ControllerExtension
{
    public ACA_Member__c    aRecord {get;set;}
    public Risk_Profile__c  riskProfile
    {
        get
        {
            if (riskProfile == null)
            {
                riskProfile = [SELECT Id,   Attributed_NPI__c,
                                            Attributed_Provider__c,
                                            Total_Member_Priority_Level__c,
                                            Total_Potential_Financial_Value__c,
                                            Total_Potential_Risk_Score__c
                               FROM         Risk_Profile__c
                               WHERE        ACA_Member__c = :aRecord.Id
                               ORDER BY     CreatedDate DESC
                               LIMIT 1
                               ];
            }
            return riskProfile;
        }
        private set;
    }


public List<HCC__c> cRecords
{
    get
    {
        if (cRecords == null)
        {
            cRecords =
            [   SELECT    Id,
                          Confidence_Level__c,
                          Financial_Value__c
                FROM      HCC__c
                WHERE     Risk_Profile__c = :riskProfile.Id
            ];
        }
        return cRecords;
     }
     private set;
}


public risk_ControllerExtension(ApexPages.StandardController sc)
    {
        aRecord = (ACA_Member__c) sc.getRecord();
    }
}







<apex:page StandardController="ACA_Member__c" extensions="risk_ControllerExtension">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Member Information">
            <apex:outputField value="{!aRecord.Name}"/>
    </apex:pageBlockSection>
   
    <apex:PageBlockSection title="Risk Profile">
        <apex:outputField value="{!riskProfile.Attributed_NPI__c}"/>
        <apex:outputField value="{!riskProfile.Attributed_Provider__c}"/>
        <apex:outputField value="{!riskProfile.Total_Member_Priority_Level__c}"/>
        <apex:outputField value="{!riskProfile.Total_Potential_Financial_Value__c}"/>
        <apex:outputField value="{!riskProfile.Total_Potential_Risk_Score__c}"/>
    </apex:PageBlockSection>
   
    <apex:pageBlockTable value="{!cRecords}" var="cRecord">
   
        <apex:column value="{!cRecord.Confidence_Level__c}">
            <apex:facet name="header">Field 1</apex:facet>
        </apex:column>
       
        <apex:column value="{!cRecord.Financial_Value__c}">
            <apex:facet name="header2">Field 2</apex:facet>
        </apex:column>
       
    </apex:pageBlockTable>
  
    </apex:pageBlock>
   
</apex:form>

</apex:page>


megmooPDXmegmooPDX
Also, FYI, when I remove the line of code <apex:outputField value="{!aRecord.Name}"/> from the VF page, it works beautifully! I just have to do some formatting and bring in more fields (I was using minimal fields just to test) and it'll be perfect. 

I can't thank you enough! If the last little piece can be figured out, it'll be totally perfect! Thank you, Glyn!
GlynAGlynA
In the VF code, replace:

<apex:outputField value="{!aRecord.Name}"/>

with:

<apex:outputField value="{!ACA_Member__c.Name}"/>

This will use the standard controller's version of the record, and will let the standard controller know that you need the Name field.  Any fields you specify this way will be queried for you by the standard conttoller.

-Glyn