Find your content:

Search form

You are here

Call a @RemoteAction from a custom button

 
Share

I would like to call an @RemoteAction method from a custom button. I am aware that it is already possible to call a WebService method.

The view page of my object is already a visualforce page override, if this increases my options.

I have put the following code into a javascript button

Visualforce.remoting.Manager.invokeAction("TokenInputHelper.getExistingTokens", "{!Project__c.Id}", function(result, event) {
    if (event.status) {
        alert(event.message);
        //TODO: Do something with response
    } else if (event.type === 'exception') {
        alert(event.message);
    }
});

but I get the following error message, which makes me think this is not possible:

A problem with the OnClick JavaScript for this button or link was encountered: Visualforce is not defined


Attribution to: Daniel Blackhall

Possible Suggestion/Solution #1

Whenever I've done remote actions I've put the @RemoteAction in the controller extension for the Visualforce page, and then call it like

ControllerExtension.RemoteMethod(DATA, callback);

I've not see the use of Visualforce.remoting.Manager.invokeAction(...) before.


Attribution to: Patrick Connelly

Possible Suggestion/Solution #2

Visualforce includes a bunch of javascript stub methods and helpers based on the controllers used that have @RemoteAction methods in them. Custom buttons lack the intelligence to do this and I believe @RemoteAction calls are not possible in this way from buttons.

That at least would be the official answer, if you were willing to reverse engineer how @RemoteAction methods actually pass data back to the server it may be possible to make an unsupported implementation.


Attribution to: ca_peterson
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/205

My Block Status

My Block Content