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

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

If you need to call apex controller method after the visualforce page load then its possible with the help of javascript.

apex:actionFunction

A component that provides support for invoking controller action methods directly from JavaScript code using an AJAX request

using actionFunction we can call the apexclass method with the help of javascript after the page load like

<apex:page sidebar=”false” showHeader=”false” controller=”testApexClass”>
<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
<apex:form >
<apex:actionFunction name=”callMethod1″ action=”{!apexMethod1}”  />
</apex:form>

—–
—–
—–
finally call the below script, then its called to apex class method after the page initialised (after the page loaded)
<script>
// kick it off
callMethod1();
</script>
</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_actionFunction.htm

You may also like

Leave a Comment