Call a apex method from a custom button
Asked Answered
P

3

0

i want to call a apex method from a custom Button

I realise we can achieve this 2 ways

  1. Calling a VF page which in turn calls the method on the page attribute
    <apex:page standardController="Opportunity" extensions="oppExt" action="{!changeStatus}"/>

  2. use javascript to call the webservices method

The question i have is on the which is best practice to it for this scenario.

Thanks

Perk answered 15/3, 2012 at 5:53 Comment(1)
Sorry, i was referring to calling apex from custom button and not controller. edited the questionPerk
L
4

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.

Lunseth answered 15/3, 2012 at 14:4 Comment(2)
thanks Matthew... how safe are we using javascript in terms of data security?Perk
If you're using JavaScript on a Visualforce page directly on Salesforce, I'm fairly sure it's as safe as your https connection is. I'm not an expert on security, though.Lunseth
H
2

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
Highmuckamuck answered 15/3, 2012 at 15:34 Comment(1)
i just need to update one field, nothing more than that.. so your suggestion would be to go for AJAX toolkit's sforce.connection. Thanks a lot for the detailed answerPerk
P
0

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?

Plebiscite answered 15/3, 2012 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.