0

I have a custom controller with a variable I'm assigning a value to and then trying to display that value on a VF page but the value is not displaying. It's a very simple case - what am I missing?

Controller:

public with sharing class myController {
    public string pid {get;set;}

    public void myController() {
        pid = 'hard-coded value in controller';   
    }
}

VF page:

<apex:page controller="myController"
  showHeader="false">

<apex:PageBlock title="My title">
PID: {!pid}
</apex:PageBlock>

</apex:page>
  • Did you actually link to the controller? Can you edit your post to include your <apex:page> declaration? – Adrian Larson May 5 '17 at 22:09
  • Interesting! Where is the VF page used? Is it a force.com site? or how are you accessing it? Seems like a permission issue to me. And also state the User who is accessing it. – Mahmood May 5 '17 at 23:05
1

You need to specify controller in apex:page

<apex:PageBlock title="My title" controller="myController">
PID: {!pid}
</apex:PageBlock>

Refer Creating a Custom Controller Class

  • Santanu thank you for your response. I had "controller" in apex:page but didn't format paste my code correctly above so not all of the code was displayed. I've corrected it. So with the corrected code above the pid does not display on the page. Any other ideas? – David G May 5 '17 at 22:57
1

You have the setting of the pid property in a method given the void return type. Remove void from the declaration and the method now becomes the constructor. Then the pid value will be available to the page on load

public with sharing class myController {
    public string pid {get;set;}

    public myController() {
        pid = 'hard-coded value in controller';   
    }
}

If you must meet the method you will need to at least call it from the constructor to be visible when the page loads like so

public with sharing class myController {
    public string pid {get;set;}

    public myController() {
          PopulatePID();
    }

    Public void populatePID(){
         pid = 'hard-coded value in controller'; 
    }
}
  • Awesome Eric! Missed that 'void' in the code. – Mahmood May 6 '17 at 6:17
0

Hi Can you please try like this!

Public with sharing class myController{
Public string pid{get;set}
Public myController(){
   Init();
}

Public void init(){ pid='assign the values'; } }

Cheers!!

  • Your controller is not setting the property pid at all. What value do you think the page will show? – Mahmood May 5 '17 at 23:07

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.