0

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?

  • Define an action in your VF page, or call the method in Controller, – Ashish Jul 29 '15 at 15:55
3

@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}">

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

Try this it should work

1

Why are you querying the User object? You already have the User Id and you are using that in your second query so the first query and variable are not necessary. Avoid running extra SOQL queries if you don't have to. You have access to the Global User Var already. Try running your query in the execute anonymous console and make sure you have a record that matches. I think you are getting a null response.

  • I believe the suggestions and questions need to be added in comments , not in the answer section – Ashish Jul 29 '15 at 16:22
  • Hi @Ashish, I believe I was answering John also, I cannot comment on others yet as I don't have 50 pts – binaryLady Jul 29 '15 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.