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

(1236)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply