1

I'm kind of new on Salesforce. I've built an Apex class that makes a callout to a webservice and I want to display on a VFPage the data that the callout returns.

I've just been able to accomplish it by saving the data in records, so I save the data in an object record and then I display the record information. The callout is called when I click on a List button, and I would like to print the list view the data that the callout returns.

How can I do that without the needing of save the data in object records? I mean, if I save the callout return in a String variable, how can I refer to that variable from the VFPage?

1 Answer 1

4

You actually have several choices here. A first, simplistic approach would be to store the data in a string variable and render it as part of the view state. This method certainly works, but it invokes a lot of view state being passed back and forth, plus overhead for the HTML rendering, and is still subject to other limits, like the View State limit of 135kb of data.

I'd recommend a @RemoteAction to simply call the service as a proxy, and then allow the client to render the values. That looks like this:

// Some JavaScript
function doCallback() {
  myController.doCallback(handleCallbackResult);
}
function handleCallbackResult(data, event) {
  // render the new data
}

And on the controller:

@RemoteAction global static String doCallback() {
    Http binding = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(yourEndpoint);
    request.setMethod('GET'); // POST, etc.
    HttpResponse response = binding.send(request);
    return response.body();
}

You can also return complex types, like classes, and apply appropriate error handling, if your request can fail, and also supply parameters to the callout function, such as an ID for a record, some user-provided text, etc. Everything is transparently converted between JavaScript objects and Apex Code objects for you by the RemoteAction library.

You can even wire this whole thing up in your favorite JS library, like jQuery, AngularJS, etc, depending on your familiarity with those libraries.

11
  • Thanks for the answer. Finally, I tried to use a trigger with '@future' action in an Apex Class. Unfortunately, the '@future' action can't return any data but void, so is not useful for may case. Anyway, I'll keep in maind what you've suggested for display data in a page. Mar 30, 2015 at 10:21
  • You can't callout directly from a trigger. It's a system limitation to prevent excessively long locks.
    – sfdcfox
    Mar 30, 2015 at 14:16
  • Thank you, this does seem to be a great approach to go. I'm a bit confused by the method of calling you're doing in the JavaScript. I'm familiar with the JavaScript Remoting calling pattern, but how does the JavaScript know about the this doCallback() function simply within the js script?
    – qarthandso
    Nov 12, 2016 at 5:34
  • @qarthandso The Visualforce framework constructs a JavaScript proxy for your for all @RemoteAction methods in the page's controller and/or extensions. Assuming your class name was myController, the method would be generated as a function at window.myController.doCallback. The method must be global if the page is to be contained in an iframe, though, which is the only thing you need to watch out for. Do feel free to experiment with this, and you'll see how it works.
    – sfdcfox
    Nov 12, 2016 at 5:37
  • 1
    @qarthandso The part about "proxies" isn't explicitly mentioned in the documentation (that I'm aware of), but various error messages you can encounter will mention them. For parameters, they must not be in an array, but set as one parameter per method parameter, for example, if the Apex method has 2 parameters, the JS code must provide three parameters, one for each method and the callback. You can call those proxies directly, or by using $RemoteAction, whichever you prefer. I usually call them direct in personal projects, but $RemoteAction in code I intend to package/distribute.
    – sfdcfox
    Nov 12, 2016 at 16:40

You must log in to answer this question.

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