function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srikanthpallasrikanthpalla 

VF pages action function

Hi

Please in which way we can use Actionfunction in VF pages
Best Answer chosen by srikanthpalla
DeepthiDeepthi (Salesforce Developers) 
Hi Srikanth,

actionFunction:
A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.

Example:
<apex:page controller="actionFunctionController" tabStyle="Account">
    <apex:form >
        <apex:actionFunction name="priorityChangedJavaScript" action="{!priorityChanged}" rerender="out"/>
        <apex:pageBlock >
            <apex:pageBlockSection title="If you will select High Customer Priority then phone textbox will be shown" columns="1" id="out" collapsible="false">
                <apex:inputField value="{!acc.CustomerPriority__c}" onchange="priorityChangedJavaScript()"/>
                <apex:inputField value="{!acc.Phone}" rendered="{!showPhone}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class actionFunctionController {
    public Account acc{get;set;}
    public Boolean showPhone{get;set;}
 
    public actionFunctionController(){
        acc = new Account();
        showPhone = false;
    }
 
    public PageReference priorityChanged(){
        if(acc.CustomerPriority__c == 'High'){
            showPhone = true;
        }
        else{
            showPhone = false;
        }
        return null;
    }
}

Also, refer the below links:
http://sfdcsrini.blogspot.com/2014/07/visualforce-action-function-example.html  

https://success.salesforce.com/answers?id=90630000000hU2kAAE 

https://developer.salesforce.com/forums/?id=906F0000000BTIPIA4 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Hope this helps you!
Best Regards,
Deepthi

All Answers

Virendra ChouhanVirendra Chouhan
https://developer.salesforce.com/forums/?id=906F0000000BTIPIA4
DeepthiDeepthi (Salesforce Developers) 
Hi Srikanth,

actionFunction:
A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request. An <apex:actionFunction> component must be a child of an <apex:form> component.

Example:
<apex:page controller="actionFunctionController" tabStyle="Account">
    <apex:form >
        <apex:actionFunction name="priorityChangedJavaScript" action="{!priorityChanged}" rerender="out"/>
        <apex:pageBlock >
            <apex:pageBlockSection title="If you will select High Customer Priority then phone textbox will be shown" columns="1" id="out" collapsible="false">
                <apex:inputField value="{!acc.CustomerPriority__c}" onchange="priorityChangedJavaScript()"/>
                <apex:inputField value="{!acc.Phone}" rendered="{!showPhone}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class actionFunctionController {
    public Account acc{get;set;}
    public Boolean showPhone{get;set;}
 
    public actionFunctionController(){
        acc = new Account();
        showPhone = false;
    }
 
    public PageReference priorityChanged(){
        if(acc.CustomerPriority__c == 'High'){
            showPhone = true;
        }
        else{
            showPhone = false;
        }
        return null;
    }
}

Also, refer the below links:
http://sfdcsrini.blogspot.com/2014/07/visualforce-action-function-example.html  

https://success.salesforce.com/answers?id=90630000000hU2kAAE 

https://developer.salesforce.com/forums/?id=906F0000000BTIPIA4 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Hope this helps you!
Best Regards,
Deepthi
This was selected as the best answer