5

I am trying to use salesforce @RemoteAction with an action that is generic to a couple of other VF Pages (other vf pages will be calling the same action). So what I want to do is instead of creating duplicates of the same code for the @remoteAction, I am trying to have it as a separete class, that I can call it everytime I need it. My question is how to call it if the controller of the VF page is diferent to the remote class, for instance a VF page with remote will look like:

    <apex:page controller="ChildRemoteController" >
        <script type="text/javascript">
            function sayHello(helloTo) {
                ChildRemoteController.sayHello(helloTo, function(result, event){
                    if(event.status) {
                        document.getElementById("result").innerHTML = result;
                    }
                });
            }
        </script>
        ......
    </apex:page>

    global with sharing class ChildRemoteController 
        extends ParentRemoteController { }
    global virtual with sharing class ParentRemoteController 
        extends GrandparentRemoteController { }

    global virtual with sharing class GrandparentRemoteController {
        @RemoteAction
        global static String sayHello(String helloTo) {
            return 'Hello ' + helloTo + ' from the Grandparent.';
        }
    }

How can I have something to look like

    <apex:page controller="OtherController" >
        <script type="text/javascript">
            function sayHello(helloTo) {
                ChildRemoteController.sayHello(helloTo, function(result, event){
                    if(event.status) {
                        document.getElementById("result").innerHTML = result;
                    }
                });
            }
        </script>
        ....
    </apex:page>

I was trying something like

    publi class OtherController {
        public OtherController() {
            ChildRemoteController crc = new ChildRemoteController();
        }
    }

    global with sharing class ChildRemoteController 
        extends ParentRemoteController { }
    global virtual with sharing class ParentRemoteController 
        extends GrandparentRemoteController { }

    global virtual with sharing class GrandparentRemoteController {
        @RemoteAction
        global static String sayHello(String helloTo) {
            return 'Hello ' + helloTo + ' from the Grandparent.';
        }
    }

1 Answer 1

8

If you have the remote action in a class called MyRemoteClass

global class MyRemoteClass{

     @RemoteAction
     global static string sayHello(String val){
        return 'Hello ' + val;
     }

}

then all you do in the VF page is call

MyRemoteClass.sayHello(helloTo, function(result, event){
                    if(event.status) {
                        document.getElementById("result").innerHTML = result;
                    }
                });

The remote action does not need to be in the controller that the page is using. That is why it is a static method with the appropriate access modifier (You will still need to add the class to the page extensions though)

3
  • 3
    AFAIK,remote action method needs to be part of the controller or extension on the page otherwise you get a Reference error`..
    – Sam
    Commented Jan 27, 2015 at 6:42
  • @theGreatDanton - You are correct which is why you add the class to the extensions list of the page
    – Eric
    Commented Jan 27, 2015 at 18:08
  • Your code returns Unknown constructor 'MyRemoteClass. MyRemoteClass(ApexPages.StandardController controller). You need to implement the extension controller constructor
    – NSjonas
    Commented Apr 7, 2017 at 7:04

You must log in to answer this question.

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