• Mohmad Sohel 2
  • NEWBIE
  • 10 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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;
       }
   }   
 
}
Hi Guys,
I have wrote a vf page which is used to display input fields based on LeadSource. If LeadSource == 'web' then it should dispaly Description. But below code is not working..
Help me regarding this. Thank You

<apex:page standardController="Contact">
    <apex:form >
        <apex:pageBlock title="Enter Contact Information">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}" />
            
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Contact.LastName}" />
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Lead Source"/>
                    <apex:actionRegion >
                    <apex:inputField value="{!Contact.LeadSource}" >
                        <apex:actionSupport event="onChange" reRender="ajaxrequest" />
                        </apex:inputField>
                    </apex:actionRegion>
               
                </apex:pageBlockSectionItem>
            
            </apex:pageBlockSection>
        
            <apex:outputPanel id="ajaxrequest">
                <apex:pageBlockSection rendered="{!Contact.LeadSource=='Web'}">
                    <apex:inputField value="{!Contact.Description}"/>
  
                </apex:pageBlockSection>
                <apex:pageBlockSection rendered="{!Contact.LeadSource=='Phone Inquiry'}">
                <apex:inputField value="{!Contact.Phone}"/>
                   </apex:pageBlockSection>
                </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Hi Guys,
I have wrote a vf page which is used to display input fields based on LeadSource. If LeadSource == 'web' then it should dispaly Description. But below code is not working..
Help me regarding this. Thank You

<apex:page standardController="Contact">
    <apex:form >
        <apex:pageBlock title="Enter Contact Information">
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}" />
            
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Contact.LastName}" />
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Lead Source"/>
                    <apex:actionRegion >
                    <apex:inputField value="{!Contact.LeadSource}" >
                        <apex:actionSupport event="onChange" reRender="ajaxrequest" />
                        </apex:inputField>
                    </apex:actionRegion>
               
                </apex:pageBlockSectionItem>
            
            </apex:pageBlockSection>
        
            <apex:outputPanel id="ajaxrequest">
                <apex:pageBlockSection rendered="{!Contact.LeadSource=='Web'}">
                    <apex:inputField value="{!Contact.Description}"/>
  
                </apex:pageBlockSection>
                <apex:pageBlockSection rendered="{!Contact.LeadSource=='Phone Inquiry'}">
                <apex:inputField value="{!Contact.Phone}"/>
                   </apex:pageBlockSection>
                </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>