Query String Parameters in VF Page

You can get and set query string parameters in the Visualforce page markup.

  • Getting Query String parameters.
  • Setting Query String parameters in Links.
  • Getting and Setting Query String parameters on a single page.

You can reference query string parameters in Visualforce markup by using the $CurrentPage global variable. 

Syntax – $CurrentPage.parameters.parameter_name

 <apex:page standardController="Account">  
   <apex:pageBlock title="Hello ! {!$User.FirstName}">  
     You are displaying values from the {!account.name} account and a separate contact  
         that is specified by a query string parameter.  
   </apex:pageBlock>  
   <apex:pageBlock title="Related Contacts">  
     <apex:dataTable value="{!account.contacts}" var="contact" cellpadding="4" border="1">  
       <apex:column >  
         <apex:facet name="header">Name</apex:facet>  
         {!contact.Name}  
       </apex:column>  
       <apex:column >  
         <apex:facet name="header">Phone</apex:facet>  
         {!contact.Phone}  
       </apex:column>  
     </apex:dataTable>  
   </apex:pageBlock>  
   <apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="false" title="false"/>  
 </apex:page>  

Setting Query String Parameters in Links –

You can set query string parameters in links to pages by constructing the link URL manually, or by using <apex:param> tags within the <apex:outputLink> tag.

 <apex:outputLink value="http://google.com/search?q={!account.name}">  
                Search Google  
 </apex:outputLink>  
 <apex:outputLink value="http://google.com/search">  
       Search Google  
       <apex:param name="q" value="{!account.name}"/>  
 </apex:outputLink>  

Note- In addition to <apex:outputLink>, use <apex:param> to set request parameters for <apex:commandLink>, and <apex:actionFunction>.

Setting and Setting Query String Parameters on a Page –

  • Wrap the datatable in an <apex:form> tag.
  • Each each contact name into an <apex:commandLink> that sets the cid parameter appropriately with an <apex:param> tag.
 <apex:page standardController="Account">  
   <apex:pageBlock title="Hello ! {!$User.FirstName}">  
     You are displaying values from the {!account.name} account and a separate contact  
         that is specified by a query string parameter.  
   </apex:pageBlock>  
   <apex:pageBlock title="Related Contacts">  
     <apex:form>  
       <apex:dataTable value="{!account.contacts}" var="contact" cellpadding="4" border="1">  
         <apex:column >  
           <apex:facet name="header">Name</apex:facet>  
           <apex:commandLink>  
              {!contact.Name}  
              <apex:param name="cid" value="{!contact.Id}"/>  
           </apex:commandLink>            
         </apex:column>  
         <apex:column >  
           <apex:facet name="header">Phone</apex:facet>  
           {!contact.Phone}  
         </apex:column>  
       </apex:dataTable>  
     </apex:form>  
   </apex:pageBlock>  
   <apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="false" title="false"/>  
 </apex:page>  

 

Leave a comment