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
Mike LeachMike Leach 

Reading a Visualforce Parameter from Controller

Is there anyway to declare a customizable parameter in Visualforce that can be read from the controller?

 

The controller is a managed package global class that (ideally) derives behavior from declarative config values in the VF page template.

 

Something like:

 

<apex:Page controller="myController">
    <apex:??? value="foo" id="myParam" />
</apex:Page>

global class myController(){
   public myController(){
      string configParam = myParam.value;
      if(configParam == 'foo'){
         //do this
      }
      else{
         //do something else
      }
   }
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Mike LeachMike Leach

Right. What I'm proposing would break MVC orthogonality. I'll stick with custom settings.

 

Thanks again for the feedback!

 

-Mike

All Answers

sforce2009sforce2009

You can do it this way. And I am not sure if this is what you are looking for

<apex:Page controller="myController">
<apex:inputHidden value="{!paramVal}" id="myParam" />
<script>document.getElementById('{!$Component.myParam}').value = 'foo';</script>
</apex:Page>

global class myController(){
public string paramVal
{ get;set; }
public myController(){
string configParam = paramVal;
if(configParam == 'foo'){
//do this
}
else{
//do something else
}
}
}
Mike LeachMike Leach

Thanks Srinivas. I did try a variation of your suggestion. A javascript post back to the controller from the client works, but I'd like to find out if the controller can get a VF page value on the server during initial page render.

 

I'll probably resort to using custom settings instead, but it was worth a try.

 

Thx!

 

-Mike

sforce2009sforce2009

Yes. But you do not have to post back. Use this.

 

<apex:page controller="myController" sidebar="false" standardStylesheets="false">
 <apex:form>
     <apex:inputHidden value="{!paramVal}" id="myParam" />
    <script>document.getElementById('{!$Component.myParam}').value = 'foo';</script>
    <apex:actionFunction name="callLoadPage" action="{!loadPage}" reRender="dummyOP"/>
    <apex:outputPanel id="dummyOP"></apex:outputPanel>
 </apex:form>
 <script>
 callLoadPage();
 </script>
</apex:page>

 

global class myController
{
   public string paramVal
   { get;set; }

   public void loadPage()
   {
      string configParam = paramVal;
      System.debug('-------' + paramVal);
      if(configParam == 'foo'){
         //do this
      }
      else{
         //do something else
      }
   }
}

aballardaballard

If you are trying to provide a value to the controller on the initial page render you would have to either do it via custom settings or a database field, or via a query parameter that you add to the request. 

 

Your controller can access query parameters via ApexPages.currentPage().getParameters()

Mike LeachMike Leach

Right. What I'm proposing would break MVC orthogonality. I'll stick with custom settings.

 

Thanks again for the feedback!

 

-Mike

This was selected as the best answer
ahab1372ahab1372

are you thinking about doing something like

/apex/yourpage?yourParameter=yourValue

 

Then aballard's suggestion should work

Mike LeachMike Leach

This Site app runs without params. The type of configurable behaviors in this particular managed controller should not be accessible via params.

 

A Developer will need to specifically declare the custom setting. Because the Developer is already working in a VF page (in this case) I'd hoped to avoid the extra step of going to custom settings and just set a value in the page.

 

Not giving up entirely. It looks like there are some unused/deprecated attributes in <apex:page /> that might be readable from the controller.