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
elpaso750elpaso750 

Parent fields values not showing in child visualforce page

Hello,

 

what I'm trying to achieve is to have a visualforce page to show both Parent and child fields (VF page object is child)

 

 

I have a custom object 'Tests_to_run__c' with two look-up relations to two custom objects.  
'Tests_to_run__c' is the child.
Object parent 1 is 'Software_Testing__'c and child relationship name is 'Tests2run__r'
Object parent 2 is 'Testing_Item__c' and child relationship name is  'Test_item_to_run__r'
 I'm trying to have the visualforce page to show some fields from the parent Objects here's the page :
<apex:page standardController="Tests_to_run__c" showHeader="true" extensions="mytest2runedit">
<apex:sectionHeader title="Testing the functionality" subtitle="{!ftt.name}"/> 
<apex:form id="Test2run" >
  <apex:messages />
  <apex:pageBlock title="Testing the functionality" id="Test_Block" mode="edit">
  <apex:messages />
      <apex:pageBlockbuttons >
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockbuttons> 
  
   <apex:pageBlockSection title="Complete the Test assigned to you" columns="2" collapsible="False">

      <apex:outputField value="{!Tests_to_run__c.Name}"/>
      <!--  apex:outputField value="{!Tests_to_run__c.Owner}"/-->
      <apex:outputField value="{!st.Software_to_Be_Tested__c}"/> 
	  <apex:outputField value="{!st.Version__c}"/> 

      <apex:outputField value="{!ftt.Software__c}"/>
      <apex:outputField value="{!ftt.Description__c}"/>

      <apex:inputField value="{!Tests_to_run__c.Compulsory__c}"/>
      <apex:inputField value="{!Tests_to_run__c.Assigne_to_User__c}"/>
      <apex:inputField value="{!Tests_to_run__c.Completed__c}"/>      
      <apex:inputField value="{!Tests_to_run__c.Test_Comments__c}"/>            
  
  </apex:pageBlockSection>    
 
  
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

and the controller extensions :
public class mytest2runedit {

public Testing_Item__c ftt {get;set;}
public Software_Testing__c st {get;private set;} 

public mytest2runedit(ApexPages.standardController controller){
	   Tests_to_run__c t2r = (Tests_to_run__c)controller.getRecord(); 
              
       t2r = [select id, Software_testing__c, Testing_Item__c, Completed__c, Compulsory__c, Assigne_to_User__c from Tests_to_run__c where id=:controller.getRecord().Id limit 1];
       
       Software_Testing__c st = [select id, version__c, Software_to_Be_Tested__c from Software_Testing__c where id =: t2r.Software_Testing__c limit 1];
       Testing_Item__c ftt = [select id, Software__c, name, Description__c from Testing_Item__c where id =: t2r.Testing_Item__c limit 1];
}
}

 

If I run the code in the controller from the System Log, I can actually see the above retrieves the parent fields values correctly, but when running the page the values from the parent fields do not appear in the page.
I'm probably justy missing something which I'm unable to figure out.
Any suggestions ?

 

 

Thanks in advance for any help on this.

 

Alex

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

You're declaring local variables for the 2 parent objects ('st' and 'ftt') instead of using the respective class variables declared at the top. That's why the class variables aren't being assigned a value. Also, you can query both parent objects (and the child object) in a single SOQL query. That would be more efficient. Try the following code:

 

 

public class mytest2runedit {

public Testing_Item__c ftt {get;set;}
public Software_Testing__c st {get;private set;} 

public mytest2runedit(ApexPages.standardController controller){
	   Tests_to_run__c t2r = (Tests_to_run__c)controller.getRecord(); 
              
       t2r = [select id, Software_testing__c, Testing_Item__c, Completed__c, Compulsory__c, Assigne_to_User__c, 
		      Software_Testing__r.version__c, Software_Testing__r.Software_to_Be_Tested__c,
			  Testing_Item__r.Software__c, Testing_Item__r.name, Testing_Item__r.Description__c,
			  from Tests_to_run__c where id=:controller.getRecord().Id limit 1];
       
       st = t2r.Software_Testing__r;
       ftt = t2r.Testing_Item__r;
}
}

 

 

All Answers

forecast_is_cloudyforecast_is_cloudy

You're declaring local variables for the 2 parent objects ('st' and 'ftt') instead of using the respective class variables declared at the top. That's why the class variables aren't being assigned a value. Also, you can query both parent objects (and the child object) in a single SOQL query. That would be more efficient. Try the following code:

 

 

public class mytest2runedit {

public Testing_Item__c ftt {get;set;}
public Software_Testing__c st {get;private set;} 

public mytest2runedit(ApexPages.standardController controller){
	   Tests_to_run__c t2r = (Tests_to_run__c)controller.getRecord(); 
              
       t2r = [select id, Software_testing__c, Testing_Item__c, Completed__c, Compulsory__c, Assigne_to_User__c, 
		      Software_Testing__r.version__c, Software_Testing__r.Software_to_Be_Tested__c,
			  Testing_Item__r.Software__c, Testing_Item__r.name, Testing_Item__r.Description__c,
			  from Tests_to_run__c where id=:controller.getRecord().Id limit 1];
       
       st = t2r.Software_Testing__r;
       ftt = t2r.Testing_Item__r;
}
}

 

 

This was selected as the best answer
elpaso750elpaso750

Hi,

 

yep you fully right and I'm just like a fish getting lost in a glass of water.

 

thanks for this, and by the way, like the nickname :-)