+ Start a Discussion
DevelopersDevelopers 

How to execute apex method from javascript in VF page?

Hi,

 

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.

 

Regards,

Kumar

 

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

Link:
http://www.salesforce.com/us/developer/docs/ajax/

Regards,
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.
DevelopersDevelopers

Hi Satish,

 

Thanks for the response.

 

Can you please explain in detail about the below function.

 

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

 


Thanks,

Kumar

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,

http://cloudforce4u.blogspot.in/2013/06/actionfunction-in-apex.html

 

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

 

Jerun JoseJerun Jose

Refer to the example in the following link.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_and_ajax.htm

 

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">
            <script>
                    function myJavascriptFunction()
                    {
                    CallmyApexMethods() ;
                    }
            </script>

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

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

public PageReference myActionInController() {
return null ;
   }
}