Book Image

Visualforce Development Cookbook

By : Keir Bowden
Book Image

Visualforce Development Cookbook

By: Keir Bowden

Overview of this book

Visualforce, in conjunction with Apex, makes it easy to develop sophisticated, custom UIs for Force.com desktop and mobile apps without having to write thousands of lines of code and markup. The "Dynamic Binding" feature of Visualforce lets you develop generic Visualforce pages to display information related to the records without necessarily knowing which data fields to show. This is accomplished through a formula-like syntax, which makes it simple to manage even a complex hierarchy of records. "Visualforce Development Cookbook" provides solutions for a variety of challenges faced by Salesforce developers and demonstrates how easy it is to build rich, interactive pages using Visualforce. Whether you are looking to make a minor addition to the standard page functionality or override it completely, this book will provide you with the required help throughout. "Visualforce Development Cookbook" starts with explaining the simple utilities and builds up to advanced techniques for data visualization and reuse of functionality. This book contains recipes that cover various topics like creating multiple records from a single page, visualizing data as charts, using JavaScript to enhance client-side functionality, building a public website and making data available to a mobile device. "Visualforce Development Cookbook" provides lots of practical examples to enhance and extend the Salesforce user interface.
Table of Contents (16 chapters)
Visualforce Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Passing parameters to action methods


When developers move to Apex/Visualforce from traditional programming languages, such as Java or C#, a concept many struggle with is how to pass parameters from a Visualforce page to a controller action method.

Passing parameters to an action method is key when a Visualforce page allows a user to manage a list of records and carry out actions on specific records. Without this, the action method cannot determine which record to apply the action to.

In this recipe, we will output a list of opportunities and for each open opportunity, provide a button to update the opportunity status to Closed Won. This button will invoke an action method to remove the list element and will also send a parameter to the controller to identify which opportunity to update.

Getting ready

This recipe makes use of a custom controller, so this will need to be created before the Visualforce page.

How to do it…

  1. Navigate to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.

  2. Click on the New button.

  3. Paste the contents of the ActionParameterController.cls Apex class from the code download into the Apex Class area.

  4. Click on the Save button.

  5. Next, create the Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.

  6. Click on the New button.

  7. Enter ActionParameter in the Label field.

  8. Accept the default ActionParameter that is automatically generated for the Name field.

  9. Paste the contents of the ActionParameter.page file from the code download into the Visualforce Markup area.

  10. Click on the Save button to save the page.

  11. Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.

  12. Locate the entry for the ActionParameter page and click on the Security link.

  13. On the resulting page, select which profiles should have access and click on the Save button.

How it works…

Opening the following URL in your browser shows the list of currently open opportunities: https://<instance>/apex/ActionParameter.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

Clicking on the Win button for the Grand Hotels Kitchen Generator opportunity updates the status to Closed Won and redraws the list of opportunities.

The page markup to send the parameter to the controller is as follows:

<apex:commandButton value="Win" action="{!winOpp}" status="status"
        rerender="opps_pb" 
        rendered="{!opp.StageName!='Closed Won'}">
   <apex:param name="oppIdToWin" value="{!opp.Id}" 
assignTo="{!oppIdToWin}" />
</apex:commandButton>

The <apex:param /> component defines the value of the parameter, in this case, the ID of the opportunity, and the controller property that the parameter will be assigned to – oppIdToWin.

Note

Note that there is a rerender attribute on the command button. If this attribute is omitted, making the button a simple postback request, the parameter will not be passed to the controller. This is a known issue with Visualforce as documented in the following knowledge article: http://help.salesforce.com/apex/HTViewSolution?id=000002664&language=en_US.

The property is declared in the controller in a normal way.

public Id oppIdToWin {get; set;}

Finally, the action method is invoked when the button is pressed.

public PageReference winOpp()
{
   Opportunity opp=new Opportunity(Id=oppIdToWin,
                                   StageName='Closed Won');
   update opp;
   return null;
}

The ID of the opportunity to update is assigned to the oppIdToWin controller property before the action method is invoked; thus, the action method can simply access the property to get the parameter value.