SlideShare a Scribd company logo
Action Function in Salesforce Pg: 1 of 6
It’s easy to call a controller method for most of the attributes using action="{!Yourmethode_Name}",
but what if you were to call the controller method from java script?
One way to do this is by using Action Function. Expertise will definitely be able to use this in complex
scenarios. This post is for those who haven't had hands on action function before and want to know how
to use it.
Lets take a scenario and work on it:
You have a checkbox and you are calling javascript function on click of this checkbox. And now once you
are in js you wish to modify some variable or do something in your controller class.
Say you want to put some value for a variable in controller and then display it on your page. this will
require calling your class method from js.
Lets understand this by example:
-- Visualforce page--
<apex:page standardcontroller="Account" extensions="MyController"
tabStyle="Account">
<apex:form >
<apex:actionFunction name="actionFunName"
action="{!ActionFunMethode}" reRender="outputtxtId"/>
<apex:pageBlock >
<apex:outputLabel for="inptCheckBox" value="Check this box
to call Controller method from js using ActionFunction"
style="color:green;"></apex:outputLabel>
<apex:inputcheckbox onclick="javaScrpt()"
id="inptCheckBox"/>
</apex:pageBlock>
<apex:outputText value="{!MyString_From_Methode}"
id="outputtxtId"></apex:outputText>
</apex:form>
<script>
function javaScrpt(){
actionFunName();
}
</script>
</apex:page>
Action Function in Salesforce Pg: 2 of 6
--Controller--
Public class MyController {
Public string MyString_From_Methode{get;set;}
public MyController(ApexPages.StandardController controller) {
}
public string ActionFunMethode(){
MyString_From_Methode = 'Method called from js using Action
function';
return null;
}
}
Let’s debug this for understanding:
<apex:actionFunction name="actionFunName"
action="{!ActionFunMethode}" reRender="outputtxtId"/>
In the above statement "actionFunName" is the name given to this action function statement and you
should use this name while calling AF in js as you can see in js function below:
function javaScrpt(){
actionFunName(); //Line 2
}
Line 2 call our action function statement it does not call controller method from here, your controller
method will be called from AF statement using attribute action="{!ActionFunMethode}"
ActionFunMethode is the method in your controller class.
Public string ActionFunMethode(){
MyString_From_Methode = 'Method called from js using Action
function';
return null;
}
This method puts a value for a variable "MyString_From_Methode" which is then displayed in vf page.
Action function reRenders the outputtext attribute in vf page which displays this variable (that is page is
refreshed and now the new value set in method is displayed).
Action Function in Salesforce Pg: 3 of 6
Difference between action support and action function
Before understanding the difference between Action support and Action Function let us go
through what they do and their similarities:
1. Both action support and function can be used to call a controller method using an AJAX
request.
For example call controller onclick of a inputcheck box Or call a controller method onfocus of
a input field
Well, they both do the same thing of calling controller method.
Difference between both:
1. Action function can call the controller method from java script.
2. Action support adds AJAX support to another visualforce component and then call the
controller method.
For example:
<apex:outputpanel id="outptpnl">
<apex:outputText value="click here"/>
<apex:actionSupport event="onclick"
action="{!controllerMethodName}" rerender="pgblck" />
</apex:outputpanel>
Here action support adds AJAX to output panel, so once you click on output panel controller
method will be called.
3. Action function cannot add AJAX support to another component. But from a particular
component which has AJAX support(onclick, onblur etc) action function can be called to call the
controller method.
Example:
<apex:actionFunction name="myactionfun"
action="{!actionFunMethod}" reRender="outptText"/>
<apex:inputcheckbox onclick="myactionfun" />
In this example onlick of input checkbox "myactionfun" action function is called from where
controller method "actionFunMethod" gets called.
Apart from this, the main difference between the "two" action support and action function is
that, the action function can also be called from java script.
Action Function in Salesforce Pg: 4 of 6
Example:
<apex:actionFunction name="myactionfun"
action="{!actionFunMethod}" reRender="outptText"/>
<apex:inputcheckbox onclick="myJavaMethod()" />
<script>
function myJavaMethod(){
myactionfun();// this call the action function
}
</script>
Here onclick of the inputcheck box java script is called from where the action function gets
called and ultimately your controller method.
Let’s demo both as a full fledged example:
Click in the Input text to call controller method using action support
Click the input check box to call Java script, then confirm in java script, upon confirmation
controller method is called using action function.
Tex in the lower pageblock gets changed depending on whether the controller method is called
by action support or action function.
Visualforce Page:
<apex:page controller="ActionSupFunController">
<apex:form >
<h1>Demonstration of difference between Action function and
Action Support</h1>
<apex:actionFunction name="myactionfun"
action="{!actionFunMethod}" reRender="outptText"/><br></br>
<br></br>
Action Function in Salesforce Pg: 5 of 6
Input Text <apex:inputText >
<apex:actionSupport action="{!actionSupMethod}"
event="onclick" reRender="outptText" />
</apex:inputText> <br></br>
Click me to call action function method <apex:inputcheckbox
onclick="myJavaMethod()" /><br></br> <br></br>
<apex:pageBlock >
<apex:outputText value="{!Display_This_String}"
id="outptText"/>
</apex:pageBlock>
<script>
function myJavaMethod(){
var checkinput = confirm('Are sure you wnat to call action
function method?');
if(checkinput == true)
myactionfun();
}
</script>
</apex:form>
</apex:page>
-- Controller --
Public with sharing class ActionSupFunController {
Public string Display_This_String{get;set;}
Public ActionSupFunController (){
Display_This_String = 'value set in constructor';
}
Public void actionFunMethod(){
Display_This_String = 'value set in action function
method';
}
Action Function in Salesforce Pg: 6 of 6
Public void actionSupMethod(){
Display_This_String = 'value set in action Support
method';
}
}

More Related Content

What's hot (20)

PPTX
Java Script Isn\'t a Toy Anymore
Alexis Williams
 
PDF
Advanced Visualforce Webinar
Salesforce Developers
 
PDF
Generically Call External Classes from Managed Packages
Salesforce Developers
 
DOCX
How to Create Module to Track Affiliate Conversions?
damienwoods
 
PDF
Spring '14 Release Developer Preview Webinar
Salesforce Developers
 
PDF
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
PDF
Look Ma, No Apex: Mobile Apps with RemoteObject and Mobile SDK
Salesforce Developers
 
PDF
Javascript-heavy Salesforce Applications
Salesforce Developers
 
PDF
Summer '13 Developer Preview Webinar
Salesforce Developers
 
PDF
NgForce: A JS Library For Quickly Building Salesforce Apps Using AngularJS
Salesforce Developers
 
PDF
jsForce in Action
Salesforce Developers
 
PDF
JavaScript Patterns and Practices from the Salesforce Experts
Salesforce Developers
 
PPT
JSF Presentation"2"
SiliconExpert Technologies
 
PPTX
10 Principles of Apex Testing
Salesforce Developers
 
PPTX
MVVM In Use
Chris Charabaruk
 
PPT
Mobile pack developer webinar
Raja Rao DV
 
PPTX
AngularJS App In Two Weeks
Peter Chittum
 
PDF
Manipulating Magento - Meet Magento Belgium 2017
Joke Puts
 
PPTX
Model View Presenter presentation
Michael Cameron
 
PDF
BDD in practice based on an open source project
Łukasz Chruściel
 
Java Script Isn\'t a Toy Anymore
Alexis Williams
 
Advanced Visualforce Webinar
Salesforce Developers
 
Generically Call External Classes from Managed Packages
Salesforce Developers
 
How to Create Module to Track Affiliate Conversions?
damienwoods
 
Spring '14 Release Developer Preview Webinar
Salesforce Developers
 
Introduction to Javascript Unit Testing With xUnit.js
Salesforce Developers
 
Look Ma, No Apex: Mobile Apps with RemoteObject and Mobile SDK
Salesforce Developers
 
Javascript-heavy Salesforce Applications
Salesforce Developers
 
Summer '13 Developer Preview Webinar
Salesforce Developers
 
NgForce: A JS Library For Quickly Building Salesforce Apps Using AngularJS
Salesforce Developers
 
jsForce in Action
Salesforce Developers
 
JavaScript Patterns and Practices from the Salesforce Experts
Salesforce Developers
 
JSF Presentation"2"
SiliconExpert Technologies
 
10 Principles of Apex Testing
Salesforce Developers
 
MVVM In Use
Chris Charabaruk
 
Mobile pack developer webinar
Raja Rao DV
 
AngularJS App In Two Weeks
Peter Chittum
 
Manipulating Magento - Meet Magento Belgium 2017
Joke Puts
 
Model View Presenter presentation
Michael Cameron
 
BDD in practice based on an open source project
Łukasz Chruściel
 

Recently uploaded (20)

PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Comprehensive Risk Assessment Module for Smarter Risk Management
EHA Soft Solutions
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Tally software_Introduction_Presentation
AditiBansal54083
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Ad

Difference between-action-support

  • 1. Action Function in Salesforce Pg: 1 of 6 It’s easy to call a controller method for most of the attributes using action="{!Yourmethode_Name}", but what if you were to call the controller method from java script? One way to do this is by using Action Function. Expertise will definitely be able to use this in complex scenarios. This post is for those who haven't had hands on action function before and want to know how to use it. Lets take a scenario and work on it: You have a checkbox and you are calling javascript function on click of this checkbox. And now once you are in js you wish to modify some variable or do something in your controller class. Say you want to put some value for a variable in controller and then display it on your page. this will require calling your class method from js. Lets understand this by example: -- Visualforce page-- <apex:page standardcontroller="Account" extensions="MyController" tabStyle="Account"> <apex:form > <apex:actionFunction name="actionFunName" action="{!ActionFunMethode}" reRender="outputtxtId"/> <apex:pageBlock > <apex:outputLabel for="inptCheckBox" value="Check this box to call Controller method from js using ActionFunction" style="color:green;"></apex:outputLabel> <apex:inputcheckbox onclick="javaScrpt()" id="inptCheckBox"/> </apex:pageBlock> <apex:outputText value="{!MyString_From_Methode}" id="outputtxtId"></apex:outputText> </apex:form> <script> function javaScrpt(){ actionFunName(); } </script> </apex:page>
  • 2. Action Function in Salesforce Pg: 2 of 6 --Controller-- Public class MyController { Public string MyString_From_Methode{get;set;} public MyController(ApexPages.StandardController controller) { } public string ActionFunMethode(){ MyString_From_Methode = 'Method called from js using Action function'; return null; } } Let’s debug this for understanding: <apex:actionFunction name="actionFunName" action="{!ActionFunMethode}" reRender="outputtxtId"/> In the above statement "actionFunName" is the name given to this action function statement and you should use this name while calling AF in js as you can see in js function below: function javaScrpt(){ actionFunName(); //Line 2 } Line 2 call our action function statement it does not call controller method from here, your controller method will be called from AF statement using attribute action="{!ActionFunMethode}" ActionFunMethode is the method in your controller class. Public string ActionFunMethode(){ MyString_From_Methode = 'Method called from js using Action function'; return null; } This method puts a value for a variable "MyString_From_Methode" which is then displayed in vf page. Action function reRenders the outputtext attribute in vf page which displays this variable (that is page is refreshed and now the new value set in method is displayed).
  • 3. Action Function in Salesforce Pg: 3 of 6 Difference between action support and action function Before understanding the difference between Action support and Action Function let us go through what they do and their similarities: 1. Both action support and function can be used to call a controller method using an AJAX request. For example call controller onclick of a inputcheck box Or call a controller method onfocus of a input field Well, they both do the same thing of calling controller method. Difference between both: 1. Action function can call the controller method from java script. 2. Action support adds AJAX support to another visualforce component and then call the controller method. For example: <apex:outputpanel id="outptpnl"> <apex:outputText value="click here"/> <apex:actionSupport event="onclick" action="{!controllerMethodName}" rerender="pgblck" /> </apex:outputpanel> Here action support adds AJAX to output panel, so once you click on output panel controller method will be called. 3. Action function cannot add AJAX support to another component. But from a particular component which has AJAX support(onclick, onblur etc) action function can be called to call the controller method. Example: <apex:actionFunction name="myactionfun" action="{!actionFunMethod}" reRender="outptText"/> <apex:inputcheckbox onclick="myactionfun" /> In this example onlick of input checkbox "myactionfun" action function is called from where controller method "actionFunMethod" gets called. Apart from this, the main difference between the "two" action support and action function is that, the action function can also be called from java script.
  • 4. Action Function in Salesforce Pg: 4 of 6 Example: <apex:actionFunction name="myactionfun" action="{!actionFunMethod}" reRender="outptText"/> <apex:inputcheckbox onclick="myJavaMethod()" /> <script> function myJavaMethod(){ myactionfun();// this call the action function } </script> Here onclick of the inputcheck box java script is called from where the action function gets called and ultimately your controller method. Let’s demo both as a full fledged example: Click in the Input text to call controller method using action support Click the input check box to call Java script, then confirm in java script, upon confirmation controller method is called using action function. Tex in the lower pageblock gets changed depending on whether the controller method is called by action support or action function. Visualforce Page: <apex:page controller="ActionSupFunController"> <apex:form > <h1>Demonstration of difference between Action function and Action Support</h1> <apex:actionFunction name="myactionfun" action="{!actionFunMethod}" reRender="outptText"/><br></br> <br></br>
  • 5. Action Function in Salesforce Pg: 5 of 6 Input Text <apex:inputText > <apex:actionSupport action="{!actionSupMethod}" event="onclick" reRender="outptText" /> </apex:inputText> <br></br> Click me to call action function method <apex:inputcheckbox onclick="myJavaMethod()" /><br></br> <br></br> <apex:pageBlock > <apex:outputText value="{!Display_This_String}" id="outptText"/> </apex:pageBlock> <script> function myJavaMethod(){ var checkinput = confirm('Are sure you wnat to call action function method?'); if(checkinput == true) myactionfun(); } </script> </apex:form> </apex:page> -- Controller -- Public with sharing class ActionSupFunController { Public string Display_This_String{get;set;} Public ActionSupFunController (){ Display_This_String = 'value set in constructor'; } Public void actionFunMethod(){ Display_This_String = 'value set in action function method'; }
  • 6. Action Function in Salesforce Pg: 6 of 6 Public void actionSupMethod(){ Display_This_String = 'value set in action Support method'; } }