4

I have a visualforce page which on load should call 4 methods one after the other. For that purpose, I tried using apex:actionFunction

<apex:page sidebar="false" showHeader="false" controller="PullFromTaleo">
  <apex:form >
    <apex:actionFunction name="Method1" action="{!Method1}" immediate="false"/>
    <apex:actionFunction name="Method2" action="{!Method2}" immediate="false" />
    <apex:actionFunction name="Method3" action="{!Method3}" immediate="false"/>
    <apex:actionFunction name="Method2.1" action="{!Mehtod2}" immediate="true"/>
    <apex:actionFunction name="Method4" action="{!Mehtod4}" immediate="true"/>
 </apex:form>
</apex:page>

However none of the above functions are executed. Not sure how to call multiple functions in sequence from a visualforce page.

11

If you need to call multiple functions in sequence (via JavaScript) using multiple ActionFunction tags you can do so by leveraging the oncomplete attribute of the ActionFunction. To begin the domino effect, simply call the first in the sequence. As each one completes, it will invoke the next function as defined in the oncomplete attribute value.

Note: JavaScript function names cannot include periods, renamed Method2.1 to Method5 in the example.

<apex:page sidebar="false" showHeader="false" controller="PullFromTaleo">
    <apex:form >
        <apex:actionFunction name="Method1" action="{!Method1}" oncomplete="Method2();" />
        <apex:actionFunction name="Method2" action="{!Method2}" oncomplete="Method3();" />
        <apex:actionFunction name="Method3" action="{!Method3}" oncomplete="Method5();" />
        <apex:actionFunction name="Method5" action="{!Mehtod2}" oncomplete="Method4();"/>
        <apex:actionFunction name="Method4" action="{!Mehtod4}" oncomplete="alert('All done with the chain of events');" />
    </apex:form>

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

</apex:page>
4

actionFunction does not call a function on the controller. It provides a javascript function that can be used to call the apex controller function. So in your example you have

<apex:actionFunction name="Method1" action="{!Method1}" immediate="false"/>

This means that if you have some javascript which calls Method1(); (a javascript method on the page) then it will in turn call your controller and run Method1 (an apex controller method).
Why are you trying to call multiple methods? What event is occurring on the page that makes you want to call these methods, is it as soon as the page loads? Why not define one method in apex which does what you want, e.g.

public PageReference masterMethod() {
   Method1();
   Method2();
   ... etc.

If you want these methods to always run when someone comes to the page, then the typical way would be to put them in the constructor of your controller class. Then they will be run before your page is rendered.

public PullFromTaleo() {
    Method1();
    Method2();
    /// etc.
}

Otherwise, if they need to do DML (which is not allowed in a controller constructor) then the trick would be to call a method from the page load. That method can then call all the other methods you want. You would do this by including an action on the apex page, like this :

<apex:page sidebar="false" showHeader="false" controller="PullFromTaleo" action="masterMethod">
3

You just declared the methods, you need to call them to make it work, ex:

<script>
window.onload = function() {
   Method1();
   Method2();
   ....
   ...
   Method4();
};
</script>

Though better approach would be to make a single action function, and wrap all the apex logic into that. Such form of actionFunction calling will invite lot of issues.

Even faster would be to use JS remoting.

2
  • 2
    This particular script pattern will likely cause problems in the state of the page and create an error condition. The functions will be executed almost instantaneously, albeit in sequence, without waiting for the prior function call to complete before calling the next action. To put this behavior in another context, it is similar to a user clicking a submit button on a form in rapid succession while believing that the button click is "doing nothing", where the desired behavior was click, wait, continue and not click, click, click, click. Edit: the noted 'better approach' is much better. – Mark Pond May 24 '13 at 17:44
  • Surely the "better approach" is the one to go for, I just put the code snippet to resolve original query of asker. As suggested by @Doug B as well, a "masterMethod" wrapping all the calls is best way to go. – Abhinav Gupta May 25 '13 at 4:14
2

I can think if 2 solutions

As and when you load the page the constructor of the controller is called. So what you can do is call the methods in the constructor, but if you're doing any DML in these methods then it is a problem, coz you cannot do DML operations in constructor.

The other solutions is to add the action tag in the page attribute. And add all the methods in the method which you need to all in the method that you have assigned in the action method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.