Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Visualforce Development Cookbook

You're reading from  Visualforce Development Cookbook

Product type Book
Published in Sep 2013
Publisher Packt
ISBN-13 9781782170808
Pages 334 pages
Edition 1st Edition
Languages
Author (1):
Keir Bowden Keir Bowden
Profile icon Keir Bowden

Table of Contents (16) Chapters

Visualforce Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. General Utilities 2. Custom Components 3. Capturing Data Using Forms 4. Managing Records 5. Managing Multiple Records 6. Visualforce Charts 7. JavaScript 8. Force.com Sites 9. jQuery Mobile 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.

You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013 Publisher: Packt ISBN-13: 9781782170808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}