+ Start a Discussion
Richard Houston 20Richard Houston 20 

Populating parent fields into a visualforce page

Hi,

I'm having trouble getting some of the parent record information to display on my visualforce page. I have a custom controller extension that is returning the values when I open the logs. However, the fields do not populate on the Visualforce page.

Parent Obejct - Opportunity
Child Object - Pursuit Survey

I'm trying to get StageName from opportunity to show on the custom visualforce page.

 
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;  
        }     
        Opportunity ParentOpp = [SELECt name, id, stagename, priority__c from Opportunity where id =: parentid];
    system.debug(PursuitChild);
    system.debug(PursuitChild.Opportunity__c); 
    system.debug(ParentOpp);
    }

When I look for the debug of ParentOpp, it does return all of the fields and values as expected:
 
14:21:18:203 USER_DEBUG [15]|DEBUG|Opportunity:{Name=Pursuit Survey Process, Id=006e000000An8XJAAZ, StageName=Pursuing, Priority__c=Target, RecordTypeId=012i00000002n45AAA}

When I try and call the StageName in my visualforce page with: 
Opportunity Stage: <apex:outputfield value="{!ParentOpp.StageName}" />

Nothing is rendered. It remains a blank space (and I'll write your name....to all the Taylor fans).

Thanks so much for any help!

Richard 
Best Answer chosen by Richard Houston 20
Vishal_GuptaVishal_Gupta
Hi Richard,

Please use the below code :
 
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;  
        }     
         ParentOpp = [SELECt name, id, stagename, priority__c from Opportunity where id =: parentid];
    system.debug(PursuitChild);
    system.debug(PursuitChild.Opportunity__c); 
    system.debug(ParentOpp);</b>
    }

I hope it will work for you. Let me know if issue will not resolve.

Thanks,
Vishal

All Answers

Vishal_GuptaVishal_Gupta
Hi Richard,

Please use the below code :
 
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;  
        }     
         ParentOpp = [SELECt name, id, stagename, priority__c from Opportunity where id =: parentid];
    system.debug(PursuitChild);
    system.debug(PursuitChild.Opportunity__c); 
    system.debug(ParentOpp);</b>
    }

I hope it will work for you. Let me know if issue will not resolve.

Thanks,
Vishal
This was selected as the best answer
Richard Houston 20Richard Houston 20
Vishal,

Thanks so much! That did the trick. 

I ran into a couple of issues where I had placed the statement, so I moved it into the If statement and included an else assignment. Here's the final code I'm using for future reference. 

 
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;  
            ParentOpp = [SELECt name, id, stagename, priority__c from Opportunity where id =: parentid];
            
        }   else{
            parentOpp = [SELECT Name, ID, StageName, Priority__c from Opportunity where id =: PursuitChild.Opportunity__c];
            
        }
       // Opportunity ParentOpp = [SELECt name, id, stagename, priority__c from Opportunity where id =: parentid];
    system.debug(PursuitChild);
    system.debug(PursuitChild.Opportunity__c); 
    system.debug(ParentOpp);
  //  system.debug(ParentOpp.Priority__c);
    }