You need to sign in to do that
Don't have an account?

Display Lookup Fields Value on VF Page
Hello All,
I am currently working on a page which displays the Name of the payer and the payee details.
This is my VF Page code:
<apex:page controller="newExpenditureController" > <apex:pageBlock > <p>Welcome, <b>{!$User.FirstName} {!$User.LastName}</b></p> <p>Please select any following options to proceed</p> </apex:pageBlock> <!-- Section containing all the Action Buttons --> <apex:form > <apex:pageBlock title="Select Choice"> <apex:pageBlockSection > <apex:commandButton action="{!addExpend}" value="Add Expenditure" disabled="{!b_addexp}"></apex:commandButton> <apex:commandButton action="{!closeAll}" value="Close All" disabled="{!b_cloall}"></apex:commandButton> </apex:pageBlockSection> </apex:pageBlock> </apex:form> <apex:pageBlock title="Create New Expenditure" mode="edit" rendered="{!addexp}"> <apex:pageMessages /> <apex:form > <apex:pageBlockSection columns="1"> <apex:inputField value="{!bb.Date__c}"/> </apex:pageBlockSection> <apex:pageBlockSection columns="2" > <apex:inputField value="{!bb.Payed_By__c}" required="true" /> <apex:inputField value="{!bb.Payed_For__c}" required="true" /> </apex:pageBlockSection> <apex:pageBlockSection columns="2"> <apex:inputField value="{!bb.Amount__c}"/> <apex:inputField value="{!bb.Description__c}" required="true"/> </apex:pageBlockSection> <apex:pageBlockSection > </apex:pageBlockSection> <!-- SAVE Option --> <center> <apex:pageBlockSection columns="1" showHeader="true"> <apex:commandButton value="Save" action="{!save}"/> </apex:pageBlockSection> </center> </apex:form> </apex:pageBlock> <apex:form > <apex:pageBlock title="Current Details" mode="edit"> <apex:dataTable value="{!Bill_Book}" var="bbc" width="100%"> <apex:detail /> <apex:column ><apex:facet name="header"><b>Payed By</b></apex:facet> {!bbc.Payed_By__c}</apex:column> <apex:column ><apex:facet name="header"><b>Payed For</b></apex:facet> {!bbc.Payed_For__c}</apex:column> <apex:column ><apex:facet name="header"><b>Amount</b></apex:facet> {!bbc.Amount__c}</apex:column> <apex:column ><apex:facet name="header"><b>Date</b></apex:facet> {!bbc.Date__c}</apex:column> <apex:column ><apex:facet name="header"><b>Description</b></apex:facet> {!bbc.Description__c}</apex:column> </apex:dataTable> </apex:pageBlock> </apex:form> </apex:page>
And the following is my controller code:
public class newExpenditureController { //Object Definition public Boolean addexp {get; set;} public Boolean b_addexp {get; set;} public Boolean b_cloall {get; set;} public Bill_Book__c bb {get; set;} private final String RedirectUrl = 'https://c.ap1.visual.force.com/apex/New_Expenditure?sfdc.tabName=01r900000001gV7'; ID id = null; //Constructor public newExpenditureController() { Id id = ApexPages.currentPage().getParameters().get('id'); addexp = false; b_addexp = false; b_cloall = true; //object bb creation bb = new Bill_Book__c(); } public List<Bill_Book__c> getBill_Book() { return [SELECT Payed_By__c, Payed_For__c, Amount__c, Date__c, Description__c FROM Bill_Book__c ORDER BY LastModifiedDate DESC LIMIT 10]; } //Toggle Button public PageReference addExpend() { //Page Access addexp = true; //Button access b_addexp = true; b_cloall = false; return null; } public Bill_Book__c getAccount() { return bb; } public PageReference save() { bb.Status__c = 'P'; try { upsert(bb); } catch(System.DMLException e) { ApexPages.addMessages(e); return null; } return (redirectTab()); } public PageReference closeAll() { addexp = false; //Button access b_addexp = false; b_cloall = true; return null; } public String getName(String id) { String name = null; name = [SELECT Name, Age__c FROM Person__c WHERE ID=:id].Name; return name; } public PageReference redirectTab() { PageReference newPage = new PageReference(RedirectUrl); newPage.setRedirect(true); return newPage; } }
This Object (Bill_Book__c ) has 2 lookup fields Payed_By__c and Payed_For__c, which link to another object Person__c through local relationship.
Currently this code provides me only the ID of the Payed By and Payed For details.
What I require is to display the First Name of these fields.
(First Name is a field of Person__c object)
Please help in this issue. Thanks.......... :)
The error occured because you did not query the first name in your query before displaying it in the VF page. Modify the query to include Payed_By__r.First_Name__c
All Answers
Use Payed_By__r.First_Name__c [The API name of First Name here] and
Payed_For__r.First_Name__c [The API name of First Name here] instead of
Payed_By__c and Payed_For__c
This should work!
Hi Shruthi,
I tried your method and changed the code for the display pageblock as follows:
When i save the code i get the following error:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Bill_Book__c.Payed_By__r
Please suggest the issue. Thanks....... :)
The error occured because you did not query the first name in your query before displaying it in the VF page. Modify the query to include Payed_By__r.First_Name__c
Thanks Shruthi,
You are the Best
Thank you.