Passing Parameters from Visualforce Page to Apex Method

To pass parameters from a Visualforce page to an Apex method, you can use the <apex:param> tag within the component that invokes the method.

To pass parameters from a Visualforce page to an Apex method, you can use the <apex:param> tag. This tag needs to be included within an <apex:actionFunction>, <apex:commandButton>, or <apex:commandLink> tag. The <apex:param> tag works by specifying a name-value pair, which is then sent to the server when the parent tag triggers an action.

Method 1

Visualforce Page

<apex:page standardController="Account" Controller="AccountController">
 <apex:outputPanel id="main">
  <apex:form>
    <apex:pageBlock title="Account Detail">
      <apex:pageBlockSection title="Account">
        <apex:outputField value="{!Account.Name}"/>
        <apex:outputField value="{!Account.Description}"/>
        <apex:outputField value="{!Account.Is_Active__c}"/>
      </apex:pageBlockSection>
     </apex:pageBlock>
  </apex:form>
  <apex:commandButton value="Update Status" action="{!updateAccountStatus}" rerender="main">
   <apex:param name="accountId" value="{!Account.Id}" assignTo="{!selAccountId}"/>
</apex:commandButton>
 </apex:outputPanel>

</apex:page>

Controller

public class AccountController 

{
   public String selAccountId {get; set;}
  
   public PageReference updateAccountStatus()
{
    Account acc=new Account(Id=selAccountId,Is_Active__c=true);
   update acc;
   return null;
}

}

Method 2

Passing Parameter using JavaScript

Visualforce Page

<apex:page controller="AccountController" showHeader="false" sidebar="false">    
    <apex:form id="frm">
        <br/><br/>
        <center>
             Account Name :<apex:inputText id="txtAccount" value=""/>
            <input type="button" id="btnSearch" value='Search' onclick="getAccountList();"/><br/><br/>
        </center>
        <apex:actionFunction name="SearchAccount" action="{!getAccountList}" reRender="pbtbl">
            <apex:param value="" name="AccountName"/>
        </apex:actionFunction>
        <apex:pageBlock id="pbtbl">
            <apex:pageBlockTable value="{!lstAcc}" var="acc">
                <apex:column value="{!acc.id}"/>
                <apex:column value="{!acc.Name}"/>
             </apex:pageBlockTable>
        </apex:pageBlock>
        <script>
        
        function getAccountList(){
            var searchString=document.getElementById("{!$Component.frm.txtAccount}").value;
            if(searchString!=null && searchString!='' && searchString!=undefined){
                SearchAccount(searchString);
            }
        }
        </script>
    </apex:form>

 </apex:page>

Controller
 
public class AccountController {
    public List<Account> lstAcc{get;set;}
    public AccountController(){
        getAccoutList();
    }
    public void getAccountList(){
        String AccountName = ApexPages.currentPage().getParameters().get('AccountName');
        String sAccountQuery='SELECT Id, Name FROM Account';
        if(AccountName !=null && AccountName!=''){
            sAccountQuery+=' WHERE Name LIKE \'%' + AccountName + '%\'';
        }
        lstAcc=Database.query(sAccountQuery);
    }

 }

 



Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach