Tag Archives: Visualforce Page

Passing Parameters from a Command Button to Controller

Sometimes we need to pass parameters to the controller, on click of a Command Button. Here is a simple example how to pass parameters from a Command Button click to controller.

Note: Command Button pass parameters to controller, if we use a rerender attribute. So, here in below example I’ve used a rerender attribute with pageBlock component.

Visualforce Page:

<apex:page standardController="Contact" extensions="SampleControllerExtn">  
    <apex:form>
        <apex:commandButton value="Click Command Button" action="{!processCommandLink}" rerender="pb">
            <apex:param name="firstName" value="{!Contact.FirstName}" assignTo="{!firstName}"/>
            <apex:param name="lastName" value="{!Contact.LastName}" assignTo="{!lastName}"/>
        </apex:commandButton>
        <apex:pageBlock id="pb" rendered="false"></apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:

public class SampleControllerExtn {
    
    //Variables being set from the commandlink
    public String firstName {get; set;}
    public String lastName {get; set;}
    
    //Initialize the controller
    public SampleControllerExtn(ApexPages.StandardController stdCtrl) {
        
    }
    
    //Handle the action of the commandLink
    public PageReference processCommandLink() {
        System.debug('Contact Name - ' + firstName + ' ' + lastName);
        //Process the variable with your logic
        return null;
    }
}

Pass Parameters from a Command Link to Controller

Sometimes we need to pass parameters to the controller, on click of a Command Link. Here is a simple example how to pass parameters from a Command Link click to controller.

Visualforce Page:

<apex:page standardController="Contact" extensions="SampleControllerExtn">  
    <apex:form>
        <apex:commandLink value="Click Command Link" action="{!processCommandLink}">
            <apex:param name="firstName" value="{!Contact.FirstName}" assignTo="{!firstName}"/>
            <apex:param name="lastName" value="{!Contact.LastName}" assignTo="{!lastName}"/>
        </apex:commandLink>
    </apex:form>
</apex:page>  

Apex Class:

public class SampleControllerExtn {
    
    //Variables being set from the commandlink
    public String firstName {get; set;}
    public String lastName {get; set;}
    
    //Initialize the controller
    public SampleControllerExtn(ApexPages.StandardController stdCtrl) {
        
    }
    
    //Handle the action of the commandLink
    public PageReference processCommandLink() {
        System.debug('Contact Name - ' + firstName + ' ' + lastName);
        //Process the variable with your logic
        return null;
    }
}

DML Operation On Visualforce Page Load In Salesforce

As we know when VF page loads, constructor executes first. But we cannot write DML statement in constructor. Due to security issues, Salesforce does not allow DML operation in construtor.

Here is an workaround to do DML operation on VF page load. We can call apex method with DML operation using action attrirbute in VF page.

Visualforce Page:

<apex:page controller="SampleController" action="{!createAccount}">
</apex:page>

Controller:

public class SampleController {
    
    public SampleController(){
        System.debug('Construtor');
    }
    
    public PageReference createAccount(){
        System.debug('DML Operation Method');
        Account acc = new Account(Name = 'Salesforce');
        Insert acc ;
        return null;
    }
}

Disable Copy and Paste in Visualforce Page

Visualforce Page:

<apex:page standardcontroller="Contact">
    <script>
        function DisableCtrlKey(e){
            
            var code = (document.all) ? event.keyCode:e.which;
            if (parseInt(code)==17){
                alert("Please re-type your email address");           
            }   
        }
    </script>
    <apex:form>
        <apex:pageblock title="Create Contact">
            <apex:pageblockbuttons>
                <apex:commandbutton value="Save" action="{!save}"/>
            </apex:pageblockbuttons>
            <apex:pageblocksection columns="1">
                <apex:inputfield value="{!Contact.Firstname}"/>
                <apex:inputfield value="{!Contact.LastName}"/>
                <apex:inputfield value="{!Contact.Email}"/>
                <apex:inputtext value="{!Contact.Email}" label="Re-Type Email Id" onkeydown="DisableCtrlKey(event)"/>              
                <apex:inputfield value="{!Contact.Phone}"/>              
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Output

Passing Variables from Apex to JavaScript

Sometimes we need to pass variables from apex to javascript. Here is an example to pass variables from apex to javascript. In below example I’m invoking one apex method using action function. In the apex method I’m defining the apex variable value. On complete of action function I’m invoking one javascript method and using the apex variable in the javascript method.

Visualforce Page:

<apex:page controller="SampleController">  
    <apex:form>
        <apex:outputPanel id="jspanel"> 
            <script>  
            function onCompleteMethod() {
                alert('{!message}')
            }
            </script>
        </apex:outputPanel>
        <apex:actionFunction name="afHelloWorld" action="{!HelloWorld}" rerender="jspanel"/>
        <apex:commandButton onclick="afHelloWorld();" oncomplete="onCompleteMethod()" value="Click me"/>
    </apex:form>
</apex:page>

Apex Class:

public class SampleController {
    
    public String message {get;set;}
    
    public pageReference HelloWorld() {
        message = 'Hello World!!';
        return null;
    }   
}