Visualforce actionFunction Explained with simple usecase

VisualForce Actionfunction Explained , Visualforce, actionfuction. apex:actionfunction, sfdc actionfunction, sfdc ajax call, salesforce pada. panda doc, sfdc panda, dineshkumar rajamani, visualforce example, sfdc apex, apex
Actionfunction Explained with Two usecases

1.) Visualforce actionFunction(apex:actionFunction) is a component which supports to invoke controller methods from javascript function through ajax call

The below script will make a call to ‘AFcall’ action method through Visualforce actionFunction
<script>window.setTimeout(AFcall,9000)</script>

JavaScript function ‘AFcall’ being defined in below <apex:ActionFunction> (Visualforce actionFunction)

<apex:form >
<apex:actionFunction name=”AFcall” action=”{!AFcall}” rerender=”out” status=”myStatus”/>
</apex:form>

2.)There is a one more apex:actionFunction (Visualforce actionFunction) use-case possible with  apex:param component, where we can set value to a controller getter/setter variable within VF page using assignTo attribute in apex:param component.

Onclick Event Listener added to the apex:outputPanel, when clicking the text it will trigger the ‘Clickme’  apex:actionFunction with a param

<apex:outputPanel onclick=”Clickme(‘Dk’)” styleClass=”btn”>
  Hello Fella! </apex:outputPanel>

Here in the below code, ‘TempVal is a getter/setter variable, using assignTo attribute in apex:param we are setting value ‘Dk’ to ‘TempVal’ variable within VF page.

<apex:actionFunction action=”{!ActionMethod}” name=”Clickme” rerender=”showtempval”>
<apex:param name=”firstParam” assignTo=”{!TempVal}” value=”” />
</apex:actionFunction>

ActionFunction Example (Apex Class) :-

public class ActionFunEx {
     String usernam;
     public String getUsername() {
         return usernam;
     }

    private String TempVal = 'Dinesh';
            
     public PageReference AFcall() {
         usernam = UserInfo.getName();
         return null;
     }
            
     public void setTempVal(String n) {
         TempVal = n;
     }
            
     public String getTempVal() {
         return TempVal;
     }
           
     public PageReference ActionMethod() {
         return null;
     }

}

ActionFunction Example (VF page) :-

<apex:page controller="ActionFunEx">
  <apex:form >
 <apex:actionFunction name="AFcall" action="{!AFcall}" rerender="out" status="myStatus"/>
  </apex:form>

<apex:outputPanel id="out">
  <apex:outputText value="Hello "/>
  <apex:actionStatus startText="requesting server please wait..." id="myStatus">
  <apex:facet name="stop">{!username}</apex:facet>
  </apex:actionStatus>
  </apex:outputPanel>

 <script>window.setTimeout(AFcall,9000)</script>

 <p><apex:outputText value="Clicked? {!TempVal}" id="showTempVal" /></p> 

<apex:outputPanel onclick=”Clickme(‘Dk’)” styleClass=”btn”> 
   Hello Fella! </apex:outputPanel>

 <apex:form >

<apex:actionFunction action=”{!ActionMethod}” name=”Clickme” rerender=”showtempval”>
 <apex:param name=”firstParam” assignTo=”{!TempVal}” value=”” />
 </apex:actionFunction>

 </apex:form>
 </apex:page>

Point to Remember :-

apex:actionFunction can’t be placed inside iteration components like apex:dataList, apex:repeat and apex:pageBlockTable  after salesforce API updated to version.23

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*