Home SALESFORCEAPEX How to call the Apex methods from visualforce page before the page load

How to call the Apex methods from visualforce page before the page load?

If you need to call apex controller method before the visualforce page load then its possible with the help of action attributes in apex:page.

apex:page
A single Visualforce page. All pages must be wrapped inside a single page component tag.

action ApexPages.Action The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action=”{!doAction}” references the doAction() method in the controller. If an action is not specified, the page loads as usual. If the action method returns null, the page simply refreshes. This method will be called before the page is rendered and allows you to optionally redirect the user to another page. This action should not be used for initialization.

 

<apex:page sidebar=”false” showHeader=”false” controller=”testApexClass” action=”{!apexMethod1}”>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME=”Generator” CONTENT=”EditPlus”>
<META NAME=”Author” CONTENT=””>
<META NAME=”Keywords” CONTENT=””>
<META NAME=”Description” CONTENT=””>
</HEAD>

<BODY>
//Build your logic
</BODY>
</HTML>

</apex:page>

Apex Class:

public with sharing class testApexClass {

//constructor
public testApexClass() {
}

public string apexMethod1() {
return ‘success1’;
}
}

Source:
http://www.salesforce.com/docs/developer/pages/Content/pages_compref_page.htm

You may also like

Leave a Comment