Apex Action Function In Visualforce Page

You can call apex action function in visualforce page. We can use AJAX functionality in Visualforce page without writing JavaScript. We write an apex class DemoCtrotroller.cls and a Visualforce Page Demo. In following example getting related one Contact and one Opportunity of Account. There also a Account picklist and on change event getting contact and opportunity asynchronously.

Example of Apex Action Function

:
DemoCtrotroller.cls

public with sharing class DemoCtrl {
    public Account acc{get;set;}
    public Contact con{get;set;}
    public Opportunity opp{get;set;}
    public List accList{get;set;}
    public String selectedAcc{get;set;} 
        
    public DemoCtrl()
    {
        accList = new List();
        for(Account a : [Select id,Name from Account order by Name ASC])
        {
          accList.add(new SelectOption(a.Id,a.Name));    
        }
        
        List acc1 = [Select id,Name,Rating,Phone from Account order by Name ASC limit 1];
        List con2 = [Select id,FirstName,LastName,MobilePhone,Email from Contact Where AccountId=:acc1[0].Id limit 1];
        List opp2=  [Select id,Name,StageName,Probability from Opportunity Where AccountId=:acc1[0].Id limit 1];
        if(acc1.size()>0)
        {
            acc = acc1[0];
        }
        else
        {
            acc = new Account();
        }
        if(con2.size()>0){
          con = con2[0];    
        }
        else
        {
            con = new Contact();
        }
        if(opp2.size()>0){
            opp = opp2[0];
        }
        else
        {
            opp = new Opportunity();
        }

    }
    
    public PageReference changeAccount()
    {
        List acc1 = [Select id,Name,Rating,Phone from Account Where Id=:selectedAcc];
        List con2 = [Select id,FirstName,LastName,MobilePhone,Email from Contact Where AccountId=:selectedAcc limit 1];
        List opp2=  [Select id,Name,StageName,Probability from Opportunity Where AccountId=:selectedAcc limit 1];
        
        if(acc1.size()>0)
        {
            acc = acc1[0];
        }
        else
        {
            acc = new Account();
        }
        if(con2.size()>0){
          con = con2[0];    
        }
        else
        {
            con = new Contact();
        }
        if(opp2.size()>0){
            opp = opp2[0];
        }
        else
        {
            opp = new Opportunity();
        }
        return null;
    }   
}

Demo.page

<apex:page controller="DemoCtrl" sidebar="false">    
    <apex:form >
        <apex:actionFunction action="{!changeAccount}" name="methodOneInJavascript" rerender="alldetail">
            <apex:param name="accid" assignTo="{!selectedAcc}" value="" />
        </apex:actionFunction>
        <apex:sectionHeader title="Demo" />
        <apex:pageBlock >
            <apex:selectList value="{!selectedAcc}" size="1" onchange="methodOneInJavascript(this.value)">
                <apex:outputLabel >Select Account: </apex:outputLabel>
                <apex:selectOptions value="{!accList}"/>                
            </apex:selectList>
        </apex:pageBlock>
        <apex:pageBlock title="Account, Contact and Opportunity" id="alldetail">
            <apex:pageBlockSection title="Account Detail" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Account Name:</apex:outputLabel>
                    <apex:outputField value="{!acc.Name}" label="Account Name"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Phone:</apex:outputLabel>
                    <apex:outputField value="{!acc.Phone}" label="Phone" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Rating:</apex:outputLabel>
                    <apex:outputField value="{!acc.Rating}" label="Rating" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>            
        
            <apex:pageBlockSection title="Contact Detail" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >First Name:</apex:outputLabel>
                    <apex:outputField value="{!con.FirstName}" label="First Name" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Last Name:</apex:outputLabel>
                    <apex:outputField value="{!con.LastName}" label="Last Name" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Cell Phone:</apex:outputLabel>
                    <apex:outputField value="{!con.MobilePhone}" label="Cell Phone" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>            
       
            <apex:pageBlockSection title="Opportunity Detail" columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Opportunity Name:</apex:outputLabel>
                    <apex:outputField value="{!opp.Name}" label="Opportunity Name" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Stage Name:</apex:outputLabel>
                    <apex:outputField value="{!opp.StageName}" label="Stage Name" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Probability:</apex:outputLabel>
                    <apex:outputField value="{!opp.Probability}" label="Probability" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>            
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Output:
Apex Action Function

Very BadBadAverageGoodExcellence (2 votes, average: 5.00 out of 5)
Loading...

Leave a Reply

Your email address will not be published. Required fields are marked *