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
sumit dsumit d 

action function in visualforce

hi all,
     i want to create a visualforce page using Action Function in which we have two drop down lists( Country and State). when i select a country from drop down it should automatically shows the states of that country in other dropdown list automatically without reloading a page .(like AJAX).
any suggestion?
Regards
Amit
Best Answer chosen by sumit d
Sohan Raj GuptaSohan Raj Gupta
Hi Amit,

You can achieve your requirement using following process:
1) Custom Setting
2) Action Function

 1) Custom Setting: Please refer this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm

2) Action Function: Ajay give you best example for this, but in Apex Function you have to create Global picklist OR country and state picklist for each obejct with all country and state values. If you will create custom setting then no need to create country and state picklist for each object with all values.

Custom setting is best way to achieve country and state values.

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 

 

All Answers

Ajay K DubediAjay K Dubedi
Hi Amit,

Please refer the below code snippet hope it helps you write your code.

Action Function

actionfunction component provides support for invoking controller action methods directly from JavaScript code using an AJAX request.

It is different from actionsupport which only provides support for invoking controller action methods from other Visualforce components, actionfunction defines a new JavaScript function which can then be called from JavaScript code.

In the example below, we are showing one picklist with name customer priority. Whenever we will change customer, then javascript method will be called. And we have specified corresponding controller action method in actionfunction component method. In controller method we are checking if customer priority is high then we are setting the boolean variable as true. So phone textbox will be rendered automatically without refreshing full page

Visualforce Code:
 
<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>

Apex Code:

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;
    }
}
Please select as best answer if it helps you.

Thank You,
Ajay Dubedi
Sohan Raj GuptaSohan Raj Gupta
Hi Amit,

You can achieve your requirement using following process:
1) Custom Setting
2) Action Function

 1) Custom Setting: Please refer this https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm

2) Action Function: Ajay give you best example for this, but in Apex Function you have to create Global picklist OR country and state picklist for each obejct with all country and state values. If you will create custom setting then no need to create country and state picklist for each object with all values.

Custom setting is best way to achieve country and state values.

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 

 
This was selected as the best answer
sumit dsumit d
thanks it helps alot
sumit dsumit d
thanks ajay it helps alot