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
Mohmad Sohel 2Mohmad Sohel 2 

Call apex controller’s method using actionSupport

We can call apex controller’s method using actionSupport inside visualforce page.

In the below example, we are retrieving the Account Number and displaying in Contact visualforce page upon selecting the Account.
Syntax:
<apex:actionSupport event="onchange" reRender="ajaxReq" action="{!retrieveAccountNumber}"/>
 
Here, “{!retrieveAccountNumber}” is method of standard controller extension to retrieve the Account Number.
Create the custom field "AccountNumber(API Name: AccountNumber__c) in Contact object. And then write the below code.

<apex:page standardController="Contact" extensions="ContactAccountCtr">
    <apex:sectionHeader title="Contact" subtitle="Page"/>
    <apex:form id="ajaxReq">
        <apex:pageBlock mode="mainDetail" >
            <apex:pageBlockSection title="Contact Information">
                <apex:inputField value="{!Contact.Salutation}"/>
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Account Information">
            <apex:pageBlockSectionItem >
               <apex:outputLabel value="Account"></apex:outputLabel>
               <apex:actionRegion >
               <apex:inputField value="{!Contact.AccountId}">
                   <apex:actionSupport event="onchange" reRender="ajaxReq" action="{!retrieveAccountNumber}"/>
               </apex:inputField>
               </apex:actionRegion>
            </apex:pageBlockSectionItem>
         
            <apex:pageBlockSectionItem rendered="{!IF(Contact.AccountId!=null, true, false)}">
                   <apex:outputLabel value="Account Number"></apex:outputLabel>
                   <apex:outputField value="{!Contact.AccountNumber__c}" />
            </apex:pageBlockSectionItem>
        
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
 
public with sharing class ContactAccountCtr {
    public Contact con {get; set;}
    public Account acc {get; set;}
    public ContactAccountCtr(ApexPages.StandardController controller) {
        con= (Contact)controller.getRecord();
         acc = new Account();
     }
   public void retrieveAccountNumber()
   {
       if(!String.IsBlank(con.AccountId))
       {
           acc= [select Id, AccountNumber from Account where Id=:con.AccountId limit 1];
            con.AccountNumber__c = acc.AccountNumber;
       }
       else
       {
           con.AccountNumber__c =null;
       }
   }   
 
}