Tuesday, December 6, 2016

Visualforce - Beyond Basics Series 3 - Pass params to action methods

What is action methods ?

After the page load, we can send requests to server asynchronously to perform some actions which is called action methods. This is achieved by sending POST request along with necessary parameters in headers.


When it will be useful ?

This is useful in cases where user needs to take small actions & update the small portion of the screen without reloading the whole page.

How to pass values to action methods ?

We can pass parameters using param tag for these four action tags,

  1. apex:commandButton
  2. apex:commandLink
  3. apex:actionSupport
  4. apex:actionFunction


Where is the code ?

Here's the illustration for passing parameters for these four different action methods,


<apex:page controller="testcls3_06122016">
<apex:form>
<apex:actionFunction name="actionFn_Js" reRender="UserMessage">
<apex:param name="actionName" value="Link-ActionFunction" assignTo="{!actionName}"/>
</apex:actionFunction>
<apex:panelGrid columns="2">
<!-- commandbutton -->
<apex:outputText value="CommandButton"/>
<apex:commandButton value="Click me" reRender="UserMessage">
<apex:param name="actionName" value="CommandButton" assignTo="{!actionName}"/>
</apex:commandButton>
<!-- commandlink -->
<apex:outputText value="CommandLink"/>
<apex:commandLink value="Click me" reRender="UserMessage">
<apex:param name="actionName" value="CommandLink" assignTo="{!actionName}"/>
</apex:commandLink>
<!-- actionsupport -->
<apex:outputText value="ActionSupport"/>
<apex:selectList size="1">
<apex:actionSupport event="onchange" reRender="UserMessage" >
<apex:param name="actionName" value="SelectList-ActionSupport" assignTo="{!actionName}"/>
</apex:actionSupport>
<apex:selectOption itemValue="Option1" itemLabel="Option1" />
<apex:selectOption itemValue="Option2" itemLabel="Option2" />
</apex:selectList>
<!-- actionfunction -->
<apex:outputText value="ActionFunction"/>
<apex:outputLink onclick="actionFn_Js('Link-ActionFunction');return false;">
Click me
</apex:outputLink>
<!-- User Output Message -->
<apex:outputPanel id="UserMessage">
<apex:outputText value="User Message" rendered="{!actionName != Null}"/>
<apex:outputText rendered="{!actionName != Null}">
User clicked this action - {!actionName}
</apex:outputText>
</apex:outputPanel>
</apex:panelGrid>
</apex:form>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub
public class testcls3_06122016 {
public String actionName{get;set;}
}
view raw gistfile1.txt hosted with ❤ by GitHub

3 comments:

Thanks for reading my blog !