[SalesForce] Displaying Record/Fields on VF Page Load

On the load of a VF page, I want to query for a record and then display certain fields from that record on the page.

Controller:

public with sharing class SS_ObjectController {
public SS_ObjectController() {

}
private User thisUser;
public SS_Object__c CurrentOOO{get;set;}


@TestVisible 
private void setup(){
    User thisUser = [select Id from User where Id = :UserInfo.getUserId() ];

    List<SS_Object__c> current_ooo = [select Id, SS_Start_Time__c, SS_End_Time__c, SS_Message__c
                                            from SS_Object__c 
                                            where CreatedById = :UserInfo.getUserId() 
                                                    AND SS_Valid__c = TRUE
                                            order by CreatedDate DESC limit 1]; 
    if(!current_ooo.isEmpty()){
        CurrentOOO = current_ooo[0];
    }
}
}

VF Page:

<apex:page controller="SS_ObjectController" showHeader="true" sidebar="false" >
<h1>Testing</h1>
<apex:outputText label = "Start Time" value="{!CurrentOOO.SS_Start_Time__c}"/>
</apex:page>

However, with what I've got here, I am not able to see my field in the VF page. Any guesses as to what I'm missing?

Best Answer

@John: As Ashish already mentioned, you have defined method "setup" to query user and set it in class property. But, this method "setup" is not being invoked from anywhere, hence you do not see any values.

There are two options:- 1. Modify your class constructor to invoke setup:-

public SS_ObjectController() {
    setup();
}
  1. OR, invoke it via Visualforce page's action attribute:-

    <apex:page controller="SS_ObjectController" showHeader="true" sidebar="false" action="{!setup}">

Related Topic