Newer Version Available

This content describes an older version of this product. View Latest

Getting and Setting Query String Parameters on a Single Page

Having seen examples of both getting and setting query string parameters, this example shows how the two actions can be combined on a single page to produce a more interesting result. Based on the example from Getting Query String Parameters, the following page makes the name of each contact in the list a hyperlink that controls the context of the detail component below it.

This is possible by:
  • Wrapping the data table in an <apex:form> tag
  • Turning each contact name into an <apex:commandLink> that sets the cid parameter appropriately with an <apex:param> tag

When used with a standard controller, command links always entirely refresh the current page with the new information added to the page—in this case, an updated cid that updates the contact detail component.

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page standardController="Account">
18    <apex:pageBlock title="Hello {!$User.FirstName}!">
19        You are displaying contacts from the {!account.name} account. 
20        Click a contact's name to view his or her details.
21    </apex:pageBlock>
22    <apex:pageBlock title="Contacts">
23        <apex:form>
24            <apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4" 
25                               border="1">
26              <apex:column>
27               <apex:facet name="header">Name</apex:facet>
28               <apex:commandLink>
29                 {!contact.Name}
30                 <apex:param name="cid" value="{!contact.id}"/>
31               </apex:commandLink> 
32              </apex:column>
33              <apex:column>
34               <apex:facet name="header">Phone</apex:facet>
35               {!contact.Phone}
36              </apex:column>
37            </apex:dataTable>
38        </apex:form>
39    </apex:pageBlock>
40    <apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="false" title="false"/>
41</apex:page>
After saving this markup, refresh your browser with the id query string parameter but without the cid parameter in the URL For example,
1https://Salesforce_instance/apex/MyFirstPage?id=001D000000IRt53
Initially the contact detail page is not rendered, but when you click a contact name the page renders the appropriate detail view.

If you use the id parameter in a URL, it must refer to the same entity referred to in the standard controller.

Note