官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 托克托县| 高安市| 日喀则市| 迭部县| 石泉县| 毕节市| 三亚市| 建始县| 卢氏县| 修武县| 遂川县| 桃园县| 保德县| 安新县| 班戈县| 庆安县| 灵寿县| 安多县| 宁晋县| 民权县| 朔州市| 上思县| 温州市| 谢通门县| 广德县| 台山市| 陇川县| 昌平区| 亳州市| 周宁县| 团风县| 澄迈县| 乌什县| 临汾市| 曲阳县| 琼中| 五大连池市| 深圳市| 遵化市| 龙胜| 青龙|