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
Richard Houston 20Richard Houston 20 

Pass parent information to visualforce page when creating child record

Hi,

I'm having an issue getting a record's parent fields to display properly on a new visiualforce page. When I first create the visualforce page the parent ID (Opportunity.ID) is written into a lookup field on the child object record (Pursuit_Survey__c.Opportunity__c). This value returns and renders as expected when creating a new opportunity. 

However, when I try and reference any other related fields on the opportunity I get either blanks, or in the case of dates, the system date/time. 

Thanks for your help! 
Richard 

Controller extension
public with sharing class SurveyParentIDExt{
    public Pursuit_Survey__c PursuitChild{get;set;}
    public Opportunity ParentOpp{get;set;}
    
    public SurveyParentIDExt(ApexPages.StandardController controller){
        PursuitChild=(Pursuit_Survey__c)controller.getRecord();
        String oppid = (PursuitChild.Opportunity__c);
        String parentID = System.currentPagereference().getParameters().get('parentid');
        if (parentID != null){
        	PursuitChild.Opportunity__c=parentid;   
        	}     
    system.debug(PursuitChild.Opportunity__c); 
    }



VF Page snippet
 
<apex:page standardController="Pursuit_Survey__c" extensions="SurveyParentIDExt">

<apex:pageBlock >
                            <apex:pageMessages id="messages"></apex:pageMessages>
                            <h2>Opportunity: &nbsp; <apex:outputfield value="{!Pursuit_Survey__c.Opportunity__c}" /> 
                                <br/><br/>
                            	Award Date: &nbsp; <apex:OutputField value="{!Pursuit_Survey__c.Opportunity__r.CloseDate}"></apex:OutputField>
                                <br/><br/>
                                Contract Value: &nbsp;<apex:outputField value="{!Pursuit_Survey__c.Opportunity__r.Contract_Value__c}"/>
                                <br/><br/>
                                Description: &nbsp; <apex:outputField value="{!Pursuit_Survey__c.Opportunity__r.Description}"/>
                                <br/><br/>
</apex:pageblock>
</apex:page>


Opportunity name renders the lookup information. Howver the other fields are blank until after the record is saved. How do I get them to show before the record is saved?
David ZhuDavid Zhu
I feel it is because you only assigned opportunity__c value, no other fields' value was assigned (and you won't be able to assign to parent object). what if you insert this into controller between line 10 and 11?
PursuiteChild = [select opportunity__c,Opportunity__r.CloseDate,Opportunity__r.Contract_Value__c,Opportunity__r.Description where Opportunity__c= :parentid];

 
William TranWilliam Tran
Try this:

<apex:page standardController="Pursuit_Survey__c" extensions="SurveyParentIDExt">

<apex:pageBlock >
                            <apex:pageMessages id="messages"></apex:pageMessages>
                            <h2>Opportunity: &nbsp; <apex:outputfield value="{!PursuitChild.Opportunity__c}" /> 
                                <br/><br/>
                            	Award Date: &nbsp; <apex:OutputField value="{!PursuitChild.Opportunity__r.CloseDate}"></apex:OutputField>
                                <br/><br/>
                                Contract Value: &nbsp;<apex:outputField value="{!PursuitChild.Opportunity__r.Contract_Value__c}"/>
                                <br/><br/>
                                Description: &nbsp; <apex:outputField value="{!PursuitChild.Opportunity__r.Description}"/>
                                <br/><br/>
</apex:pageblock>
</apex:page>

that is change Pursuit_Survey__c to PursuitChild

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks