i want to call a apex method from a custom Button
I realise we can achieve this 2 ways
Calling a VF page which in turn calls the method on the page attribute
<apex:page standardController="Opportunity" extensions="oppExt" action="{!changeStatus}"/>
use javascript to call the webservices method
The question i have is on the which is best practice to it for this scenario.
Thanks
It depends on your use case. The API supports both Controllers and Extensions and JavaScript Remoting or JavaScript with Web Services. These methods are all based on defined standards; it's really up to the developer or development team.
JavaScript is more suited for interfaces that display smaller amounts of data, but it is generally more user friendly.
I'd recommend the AJAX Toolkit over JavaScript Remoting for this task. It's better suited for single field updates; JavaScript Remoting is better suited for accessing Apex methods through JavaScript. However, the same result can be accomplished with either.
Prady, your question is confusingly worded, but if I understand you correctly from the title of your post, you are trying to call Apex from a Custom Button's OnClick JavaScript. In this scenario, your assessment of your options is nearly complete. Here are the options, in order of most preferable to least preferable:
- Use AJAX toolkit's sforce.connection.[update|insert|delete] to do standard DML operations on the records you are dealing with. If the action you need to take is not extremely straightforward (i.e. if you are doing more than just updating some fields, inserting new records, or deleting records), then you could have a special "trigger" field on the records in question which you can use the AJAX toolkit to update, and then this in turn will kick off some Apex trigger code on the backend, which will perform your more complex logic.
- Use AJAX toolkit's sforce.apex.execute() method to execute a custom Apex WebService method. I'm tempted to put this 3rd because it requires writing a WebService, which is SOAP-based and therefore slightly slower than a REST-based approach. However, it is more straightforward to leverage in Custom Button On-Click JS, and is a time-tested standard practice for developers working with Standard Page Layouts. Also, if you are including this Custom Button code in a Managed Package, this will be preferable to REST-API based methods because to use the REST API, admins have to configure a Remote Site corresponding to their particular SFDC instance --- not a big deal, but a slight hassle nevertheless.
- Use JavaScript/AJAX to asynchronously call the REST API or custom Apex REST endpoints. Use jQuery's $.get() or $.ajax() methods to do a GET/POST to these endpoints. This works because the REST API will always be in the same domain as whatever Standard Page Layout contains your Custom Button.
- Redirect the user to a Visualforce Page, at which point you can do whatever you want in Apex using the Page's Controller, and then send the user back to the original page. Least preferable because it requires the user to be doubly-redirected.
I don't follow your question. Calling apex methods from controller is as easy as typing controllerObj.methodName (assuming the method is in another controller), or just typing methodName, if the method is in the same controller. Or I may have misunderstood your question. Can you add more details please?