Difference between action support and action function

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9  ** Article 10

Before understanding the difference between Action support and Action Function let us go through what they do and their similarities:

1. Both action support and action function can be used to call a controller method using an AJAX request.
   * for example call controller onclick of a inputcheck box
   * or call a controller method onfocus of a input field
Well, they both do the same thing of calling controller method.

Difference between both:


1. Action function can call the controller method from java script.
2. Action support adds AJAX support to another visualforce component and then call the controller method.
    for example:

     <apex:outputpanel id="outptpnl">
             <apex:outputText value="click here"/>
         <apex:actionSupport event="onclick" action="{!controllerMethodName}"  rerender="pgblck" />
     </apex:outputpanel>
 
Here action support adds AJAX to output panel, so once you click on output panel controller method will be called.
 
3. Action function cannot add AJAX support to another component. But from a particular component which has AJAX support(onclick, onblur etc) action function can be called to call the controller method.
Example:

 <apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myactionfun" />


In this example onlick of input checkbox "myactionfun" action function is called from where controller method "actionFunMethod" gets called.
Apart from this, the main difference between the "two" action support and action function is that, the action function can also be called from java script.
Example:

<apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/>
 <apex:inputcheckbox onclick="myJavaMethod()" />
<script>
   function myJavaMethod(){
     myactionfun();// this call the action function
  }
  </script>

Here onclick of the inputcheck box java script is called from where the action function gets called and ultimately your controller method.
Lets demo both as a full fledged example:

Click in the Input text to call controller method using action support
Click the input check box to call Java script, then confirm in java script, upon confirmation controller method is called using action function.

Tex in the lower pageblock gets changed depending on whether the controller method is called by action support or action function.












Visualforce Page:

<apex:page controller="ActionSupFunController">
 <apex:form >
  <h1>Demonstration of difference between Action function and Action Support</h1>

  <apex:actionFunction name="myactionfun"  action="{!actionFunMethod}" reRender="outptText"/><br></br> <br></br>
 
  Input Text <apex:inputText >
                <apex:actionSupport action="{!actionSupMethod}" event="onclick" reRender="outptText" />
             </apex:inputText> <br></br>
            
Click me to call action function method   <apex:inputcheckbox onclick="myJavaMethod()" /><br></br> <br></br>  

    <apex:pageBlock >
        <apex:outputText value="{!Display_This_String}" id="outptText"/>
    </apex:pageBlock>        

  <script>
   function myJavaMethod(){
   var checkinput = confirm('Are sure you wnat to call action function method?');
   if(checkinput == true)
      myactionfun();
  }
  </script>
 </apex:form>
</apex:page>


 -- Controller --

Public with sharing class ActionSupFunController {
Public string Display_This_String{get;set;}

    Public ActionSupFunController (){
     Display_This_String = 'value set in constructor';
    }
   
    Public void actionFunMethod(){
      Display_This_String = 'value set in action function method';
    }
   
    Public void actionSupMethod(){
      Display_This_String = 'value set in action Support method';
    }
}

 
Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10

Click For : Salesforce Interview Question And Answers

42 comments:

  1. Excellent.. what a way to explain thanks for this

    ReplyDelete
  2. It is very good Explanation......

    ReplyDelete
  3. nice article bro.........

    ReplyDelete
  4. Visualforce component for pagination - http://cloudforce4u.blogspot.in/2013/06/visualforce-page-component-for.html

    ReplyDelete
  5. Its such a nice n helpful explanation . Iam reli thank full to u ..

    ReplyDelete
  6. wonderful explanation bro.............

    ReplyDelete
  7. Just wish to say your article is as surprising.
    The clarity in your post is just nice and i
    can assume you are an expert on this subject.
    Well with your permission let me to grab your feed to keep updated with forthcoming post.
    Thanks a million and please continue the gratifying work.


    Look at my page fun cake topper

    ReplyDelete
  8. Really helpful.....Good Job.... :)

    ReplyDelete
  9. wonderful explanation Yoganand Gadekar

    ReplyDelete
  10. Excellent work! You have put in a lot of effort into this for others, truly appreciable! Way to go Yoganand! -- Shalini

    ReplyDelete
  11. One of the best articles which explains the difference in an easy way to understand. You really did a very good job writing this article.

    ReplyDelete
  12. very nice article.. it is really very helpful :)

    ReplyDelete
  13. Super.. good explanation dude.

    ReplyDelete
  14. gud explanation

    ReplyDelete
  15. very nice explanation

    ReplyDelete
  16. Thanks a lot bro for clear cut explanation. Was looking for these type of articles.

    ReplyDelete
  17. Thanks so much for this Article Mr. Gadekar, it really helped me, I was in search for this kind of simple explanation since long and finally got it. Keep motivated to post more Articles on other topics too. May be some one else is looking for some explanation on some topic which you have understood very well. Its not so easy to make people understand on a concept and you have taken your time, helped us to understand this, great Job!!!

    I really appreciate.

    Thanks,

    ReplyDelete
  18. Thank you very much Yoganand for such a clear explanation

    ReplyDelete
  19. Thank you very much yoganand for such a clear explanation

    ReplyDelete
  20. Very Good Explanation. So clear! Thank you!

    ReplyDelete
  21. well explained. Thanks.

    ReplyDelete
  22. Good explanation!! helped a lot!!

    ReplyDelete