3

I am in the process of porting an AngularJS application to Visualforce and Apex. For some of my services, I am using a custom controller with remote actions. Since this app will be included in a managed package, I am attempting to use the namespace safe JS remote action call:

Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.MyController.myAction}', param, function(result) { 
    //callback 
});

When I first attempted this...nothing happened. No exception in the console. Just nothing. After doing some debugging, I finally manually called this from the Dev Tools console and got the error TypeError: Cannot read property 'OppMapController' of undefined. I attempted to call the remote action the non-namespace safe alt way:

MyController.myAction(param, function(result) {
    //some stuff
});

This worked as intended. After fussing about for a bit, I decided to create an Angular constant and set its value to '{!$RemoteAction.MyController.myAction}'. I inspected this constants value in the browser console and saw that it resolved to MyController.myAction and when passed it the namespace safe remoting call, everything worked as intended.

Now, my question is whether or not this call will function as intended when the app is packaged:

Visualforce.remoting.Manager.invokeAction("MyController.myAction", param, function(result) {
    ....stuff
});

Please note the following: all of this is being called from a JS file included in a static resource file.

5

Now, my question is whether or not this call will function as intended when the app is packaged:

Visualforce.remoting.Manager.invokeAction("MyController.myAction",param, function(result) {
    ....stuff 
});

When this code is run within an org that has a namespace prefix, no, it will not work --- you would have to prepend the namespace prefix, e.g.

Visualforce.remoting.Manager.invokeAction("mynamespace.MyController.myAction",param, function(result) {
     ....stuff 
 });

As far as why this code (using the Visualforce Global Variable $RemoteAction) did not work for you:

Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.MyController.myAction}', param, function(result) { 
    //callback 
});

this will only work if you run it within a Visualforce Page --- if you include this code within a StaticResource JavaScript file, it will not work because it includes Visualforce merge variables, which will only be processed within code written directly into a Visualforce Page or Component --- JavaScript code stored in StaticResources and loaded in via <apex:includeScript value="path_to_js_resource/> or <script src="path_to_js_resource"></script> cannot include any Visualforce merge variables, as these merge variables will not be evaluated and will instead be interpreted literally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.