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.
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;
}
}
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.
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;
}
}
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.
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.