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

(1056)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 13x 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