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
agabagab 

Cannot save Visualforce page when using CurrentPage global variable

Hello,
 
I am opening a new Visualforce page via an URL when I click on a button from the Leads tab page.  I am also passing a parameter in the URL query string i.e. /apex/myVisualforcePage?p=val.
 
I am trying to retrieve the 'p' parameter on the opened Visualforce page using the following: {!$CurrentPageReference.parameters.p}.  The problem is that I cannot even save the Visualforce page.  I get the following error:
 
Error: Field parameters does not exist. Check spelling.
 
Is this not the way to retrieve parameters from a query string, and shouldn't this work?
 
Thanks,
agab-
jwetzlerjwetzler
That should work ($CurrentPage is preferred over $CurrentPageReference, but both should work).

Can you show me how you're using it in your page?  I am able to specify this in my pages.  Please use the SRC button when posting code.


agabagab
Code:
I was trying to save the value of the 'p' parameter in a <apex:inputHidden> tag like this

<apex:form id="theForm">
  <apex:inputHidden id="myId" value="{!$CurrentPage.parameters.p}"></apex:inputHidden>
</apex:form>

so that I could in turn retrieve the value in my javascript using getElementById.

I just discovered that the {!$CurrentPage.parameters.p} expression works when I use it like this:

<apex:outputLabel>{!$CurrentPage.parameters.fn}<apex:outputLabel>

I guess that the expression cannot be used anywhere on the page.  I'll have to capture it in a 
div tag and then use getElementById to retrieve it in my javascript.

Thanks,
agab- 

 
jwetzlerjwetzler
Can you tell me more about how you're intending to use this for your page?  The reason this wasn't working (though the error message was not entirely helpful) is because the value attribute on input elements is expected to be read/write, and query parameters are read only values.

You can access this in your controller by using ApexPages.currentPage().getParameters().get('p');  And then you can expose that as a property on your controller if you need to bind to it elsewhere on the page.  However if you're submitting an apex:form then I don't see the need for an inputHidden when you can simply access the property in your controller.
agabagab
I see.  The page is for display only.  I basically open up the page by clicking on a button on a different page, passing the 'p' parameter in the URL; the newly opened Visualforce page merely runs some javascript (as soon as the page loads) and uses the 'p' parameter value as an input, and displays some info.  I was not planning on creating a controller.   I thought I could set the 'value' attribute of the <apex:inputHidden> tag directly without it being bound to a Controller attribute but I guess it does not work that way.  I hope this clarifies what I am trying to accomplish.

Anyhow, I created a Controller to implement the binding as you mentioned and it now works the way I had wanted it to work. The 'p' value is captured and set in a Controller attribute which is bound to the hidden field on my Visualforce page; my javascript can now retrieve the value via the getElementById method.  Thank you so much for taking the time to explain this to me.

Sincerely,
agab-
jwetzlerjwetzler
if the page is for display only then why do you need a form?  Or any inputs at all?

Seems like if you need to get the value into your javascript function you can just do it in your script tag.

<script>
var param = '{!$CurrentPage.parameters.p}';
</script>


agabagab
That's the first thing I tried, but it looks like the {!$CurrentPage.parameters.p} cannot get resolved inside the <script> tag.
agabagab
My mistake.  {!$CurrentPage.parameters.p} does work inside the <script> tag.  I was somehow referencing a wrong parameter name.

Thanks,
agab-