+ Start a Discussion

How to execute apex method from javascript in VF page?



In my project, i have the below scenario. On Case object, two custom buttons used. For those two custom buttons, they used Visual force pages.


On loading the page, it executes the corresponding action method. Like below:


<apex:page standardController="Case" extensions="Case_Remake_Repair_Controller" action="{!remake}">


Now, on defining custom button, behaviour is selected as "Open in a new window" and displaying some status message as:

"Your REMAKE has been sent to ECC."

Now, i want to show this message instead of new window, as an alert message in javascript.
I thought of using Javascript here. And the behaviour is: Onclick - Execute javascript.


Now, please suggest me how to call the above apex method in javascript while clicking on the custom button.


Thanks in advance.





Well, you can use the Apex Toolkit to call Apex methods from Javascript.


Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Hi Satish,


Thanks for the response.


Can you please explain in detail about the below function.


sforce.apex.execute(arg1, arg2, [agr3,...]).




Yoganand GadekarYoganand Gadekar

If i get it right, you want ot call a controller method from java script...

If that is the case then you can use action function component which can call controller method from java script.


You may hHave a look at this example,



Hit kudos and mark this as answer if it helps you.


Jerun JoseJerun Jose

Refer to the example in the following link.



Please make sure that the profiles get proper permission to the apex class that is being called.

Prashant Pandey07Prashant Pandey07
You can you <apex:actionfuntion> to achive this funcationality..see the below code for your reference.

apex:page controller="sampleClass">
                    function myJavascriptFunction()
                    CallmyApexMethods() ;

            <apex:form >
        <apex:actionFunction name="CallmyApexMethods" action="{!myActionInController}"/>
            <apex:pageBlock >
                    <apex:commandButton value="Click Me" onclick="myJavascriptFunction() ;"/>

//***********************Apex Class**************************//
public class sampleClass {

public PageReference myActionInController() {
return null ;