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
Quan Dao 8Quan Dao 8 

Populate Record's Name instead of Record's ID on Visualforce related list

Hi, 

I am new to Developing and have ran into a problem while playing around in my Dev Org. 

I am trying to create a VF page to display related records. However, I am getting the record Id instead of the Record's Name. Please help.

The issue comes from the fact that I am utilizing the Game__c's custom object Team__c  (which is retrieving the record id) . It doesn't allow me to retrieve the Game__c.Team__r.Name on the VF page as the controller is a different custom object.

Note: The Game__c object looks up to both the Team__C object and the My_Profile__c object.

Controller 
public with sharing class GamesPlayedController
{
    ApexPages.StandardController sc;
    public list <Game__c> Game {get; set;}
    public list <my_Profile__c> MyProfile {get; set;}
    
    
    public GamesPlayedController (ApexPages.StandardController sc)
    {
        this.sc = sc;
        
        Game = [SELECT Game_Outcome__C, Team__c, Player__c,  Team_Score__c, Opponent_Score__c,
                      Opponent_Team_Name__c, Player_s_Point__c,
                      Opponent_Name__c, CreatedDate, Team__r.Name, Player__r.Name
               FROM Game__c
               WHERE X2K__c IN ('2K19','2K18')
               ORDER BY CreatedDate
               DESC
               LIMIT 100];
       
       
         }     
    
    public ApexPages.PageReference SaveBoth()
    {
        insert Game;
        return sc.Save();   
        }  
            
     }

VF:
<apex:page standardController="My_Profile__c" lightningstylesheets="true" extensions="GamesPlayedController">

<Apex:form>
    <apex:pageBlock title="Games Played">
        <apex:pageBlockTable value="{!Game}" var="record">
            //<apex:repeat value="{!Game}" var="Field2">
            <apex:column>
                <apex:facet name="header"> 
                    Game Outcome
                </apex:facet>
                <apex:outputText value="{!record.Game_Outcome__C}"/>
            </apex:column>
            
            <apex:column>
                <apex:facet name="header">
                    Team
                </apex:facet>
                <apex:outputLink value="/{!record.Team__c}">                  
                        <apex:outputText value="{!record.Team__c}"/>                  
                </apex:outputLink>
            </apex:column>
            
            <apex:column>
                <apex:facet name="header">
                    Team Score
                </apex:facet>
                <apex:outputText value="{!record.Team_Score__c}"/>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Opponent Score
                </apex:facet>
                <apex:outputText value="{!record.Opponent_Score__c}"/>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Opponent Team Name
                </apex:facet>
                <apex:outputText value="{!record.Opponent_Team_Name__c}"/>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Player
                </apex:facet>
                <apex:outputLink value="/{!record.Player__c}">             
                        <apex:outputText value="{!record.Player__c}"/>
                </apex:outputLink>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Player's Point
                </apex:facet>
                <apex:outputText value="{!record.Player_s_Point__c}"/>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Opponent Name
                </apex:facet>
                <apex:outputText value="{!record.Opponent_Name__c}"/>
            </apex:column>
            
            <apex:column>
            <apex:facet name="header">
                    Created Date
                </apex:facet>
                <apex:outputText value="{!record.CreatedDate}"/>
            </apex:column>
          // </apex:repeat>

        </apex:pageBlockTable>
    </apex:pageBlock>
</Apex:form>

</apex:page>

User-added image
v varaprasadv varaprasad
Hi Quan,

Change your code like below : 
 
//In class add  Team__r.Name

Game = [SELECT Game_Outcome__C, Team__r.Name, Player__c,  Team_Score__c, Opponent_Score__c,
                      Opponent_Team_Name__c, Player_s_Point__c,
                      Opponent_Name__c, CreatedDate, Team__r.Name, Player__r.Name
               FROM Game__c
               WHERE X2K__c IN ('2K19','2K18')
               ORDER BY CreatedDate
               DESC
               LIMIT 100];

//VF Page Add record.Team__r.Name
			   
	 <apex:outputLink value="/{!record.Team__c}">                  
                        <apex:outputText value="{!record.Team__r.Name}"/>                  
                </apex:outputLink>


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Quan Dao 8Quan Dao 8
Receving this error below when trying to save VF page (after already adding in the code for the Controller):

User-added image

Please note this VF page is on a custom object called My_Profile__c. The object in my Controller is the Game__c object which looks up to the My_Profile__c object as well as the Team__c object. I want to display the Team__c object's name related to the Game__c object on the VF page on the My_Profile__c object. 
v varaprasadv varaprasad
Try This : 
 
//In class add  Team__r.Name

Game = [SELECT Game_Outcome__C, Team__c, Player__c,  Team_Score__c, Opponent_Score__c,
                      Opponent_Team_Name__c, Player_s_Point__c,
                      Opponent_Name__c, CreatedDate, Team__r.Name, Player__r.Name
               FROM Game__c
               WHERE X2K__c IN ('2K19','2K18')
               ORDER BY CreatedDate
               DESC
               LIMIT 100];

//VF Page Add record.Team__r.Name
			   
	 <apex:outputLink value="/{!record.Team__c}">                  
                        <apex:outputText value="{!record.Team__r.Name}"/>

 
Quan Dao 8Quan Dao 8
Hi,

Same issue still..
Quan Dao 8Quan Dao 8
Hi, Same issue still..