9

I'm trying to call an Apex function from a Visualforce page, but my Apex function isn't getting hit. What am I doing wrong?

I have this

ct_4_sf.RecordingTest.saveRecording(
    paths,
    function(data){
        console.log('data is ',data);
    }
);

Which is supposed to call this:

public with sharing class RecordingTest {

    @RemoteAction
    public static String saveRecording(String data) {
        System.debug('hit save recording' + data);
        return Connector.saveRecording(data);
    }
}

But the System.debug line never gets hit.

I've also tried it this way

Visualforce.remoting.Manager.invokeAction(
    '{ct_4_sf.RecordingTest.saveRecording}',
    paths,
    function(data){
        console.log('data is ',data);
    }
);

with the same result.

For this method, using !$RemoteAction instead of, or in front of ct_4_sf gives me compilation errors. Removing ct_4_sf(which is the namespace under Setup > Create > Packages), from the first method doesn't help.

I'm not getting any errors in the console or anywhere else.

11
  • 1) You should probably make RemoteAction methods global. 2) Are you sure your log levels are high enough? Commented May 5, 2015 at 16:25
  • Is there a particular reason you're using the prefix ct_4_sf? Using RecordingTest.saveRecording( params ) in the JavaScript of your VF page should work as you don't appear to have a namespace to deal with. Commented May 5, 2015 at 16:28
  • @AdrianLarson setting the class and method to global didn't help. Yes, I confirmed that my log levels are high enough. Commented May 5, 2015 at 16:31
  • @MarkPond I removed ct_4_sf prefix, which is the namespace defined in Setup > Create > Namespace Prefix, and that didn't help Commented May 5, 2015 at 16:32
  • What is the data type for the paths var you are passing in? If it is not String that could be the cause... Commented May 5, 2015 at 16:36

2 Answers 2

6

The problem is with this line right here:

'{ct_4_sf.RecordingTest.saveRecording}',

You should be using the $RemoteAction merge value, like this:

'{!$RemoteAction.RecordingTest.saveRecording}',

This automatically takes care of your namespacing problems, too.

Your controller won't be available unless you include it as a controller or extension on the page. You don't automatically get all remote actions for free.

8
  • From the OP: "For this method, using !$RemoteAction instead of, or in front of ct_4_sf gives me compilation errors." He already tried your solution. Commented May 5, 2015 at 17:07
  • 1
    @AdrianLarson I forgot to mention that the page must reference the controller or extension where the remoteaction lives, or it won't compile/resolve $RemoteAction, nor will it include it in the page's script (so it can't be called). Commented May 5, 2015 at 17:13
  • How do I "include my controller as a controller or extension on the page?", if that's a step I'm missing? Commented May 5, 2015 at 17:17
  • 1
    @Houseman <apex:page standardController="..." extensions="RecordingTest" ...> or <apex:page controller="RecordingTest" ...>. Also, if you need it as an extension, you'll need to add public RecordingTest(ApexPages.StandardController controller) { } to your class. Commented May 5, 2015 at 17:19
  • @sfdcfox Good catch. I forgot about that part because my JS is in a Static Resource so I didn't look at the page itself. Commented May 5, 2015 at 17:21
0

Try adding event to your callback function. Usually when I do remoting, it looks something like:

MyExtension.remoteMethod(
    param1, param2, function (result, event) {
        if (event.status) {
            console.log('success');
        } else {
            console.log(event);
        }
    }
);

Also make sure in your page tag you have extensions="MyExtension". Not sure if you are using a standard or custom controller, but your page tag should look something like:

<apex:page standardController="Opportunity" extensions="RecordingTest">
5
  • That didn't help. Commented May 5, 2015 at 17:07
  • What are you seeing in the console? Anything? Commented May 5, 2015 at 17:08
  • I'm not seeing anything in the console Commented May 5, 2015 at 17:09
  • So you are not hitting your callback... Have you tried any debugging in the JS method that calls the RemoteAction? Maybe we need that context to help. Commented May 5, 2015 at 17:11
  • I have a console.log right above my remote call, and that gets hit. The only other meaningful place I can add a log is when the function returns, where logs already are. Commented May 5, 2015 at 17:16

You must log in to answer this question.