Calling multiple apex methods from visualforce page using Javascript?

If you need to call multiple apex controller method with sequence (like complete one by one) then using multiple ActionFunction tags you can do so by leveraging the oncomplete attribute of the ActionFunction.

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

oncomplete
The JavaScript invoked when the result of an AJAX update request completes on the client.

<apex:page sidebar=”false” showHeader=”false” controller=”testApexClass”>
<apex:form >
<apex:actionFunction name=”callMethod1″ action=”{!apexMethod1}” oncomplete=”callMethod2();” />
<apex:actionFunction name=”callMethod2″ action=”{!apexMethod2}” oncomplete=”callMethod3();” />
<apex:actionFunction name=”callMethod3″ action=”{!apexMethod3}” oncomplete=”callMethod4();” />
<apex:actionFunction name=”callMethod4″ action=”{!apexMethod4}” oncomplete=”callMethod5();”/>
<apex:actionFunction name=”callMethod5″ action=”{!apexMethod5}” oncomplete=”callMethod6();”/>
<apex:actionFunction name=”callMethod6″ action=”{!apexMethod6}” oncomplete=”alert(‘All the apex methods completed’);” />
</apex:form>

<script>
// kick it off
callMethod1();
</script>

</apex:page>

public with sharing class testApexClass {

//constructor
public testApexClass() {
}

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

public string apexMethod2() {
return ‘success2’;
}
public string apexMethod3() {
return ‘success3’;
}
public string apexMethod4() {
return ‘success4’;
}
public string apexMethod5() {
return ‘success5’;
}
public string apexMethod6() {
return ‘success6’;
}
}

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

(787)

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 12x 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