1

As of now, I have a vf page with a save button for a form:

<apex:commandButton action="{! saveFields }" value="Save"/> 

I now want to be able to specify an onClick action causing the current window to close/refresh depending on the outcome of the saveFields action. For that, I thought of calling a js function with the onClick event like so:

<apex:commandButton action="{! saveFields }" status="busy" value="Save" onclick="saveHandler()"/> 

Furthermore, I want to make sure the onclick event is handled only after the action completes.

Is this the right way to go about it?

2
  • 2
    You can use action function oncomplete attribute for this. Commented Jun 9, 2017 at 7:03
  • So, you mean change onclick to oncomplete?
    – coldspeed
    Commented Jun 9, 2017 at 7:04

1 Answer 1

3

Below is the sample code how to use oncomplete attribute in actionfunction. First you need to call actionfunction. Once the actionfunction's "action" method is executed, method specified in oncomplete attribute will be called.

<apex:page>    
   <script>
    function callActionFUnction(){
        call();      // Action function is called using name attribute of action function.
    }
    function saveHandler(){
        // This function will be called after execution of saveFields action in controller. 
    }
   </script>
    <apex:actionfunction action="{!saveFields }" oncomplete="saveHandler()" name="call" reRender="success"/>
    <apex:commandButton status="busy" value="Save" onclick="callActionFUnction()"/>
 </apex:page>
3
  • You can directly call actionFunction from oncomplete, intermediate JS function is optional; would be used only if any additional manipulation is neede to be done.
    – Raul
    Commented Jun 9, 2017 at 8:52
  • true @RahulSharma Commented Jun 9, 2017 at 9:03
  • @MuzammilBajaria What if we give Both action and Onclick event in the same command Button, And when clicking the Command button executes First? Onclick or Action?
    – SFDC
    Commented Jul 3, 2019 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .