7

I have a parent class that provides a set of remote action methods that are common to my other controllers:

global virtual with sharing class MyBaseClass {
    @RemoteAction
    global static void remoteMethodA () {
        // stuff
    }
    @RemoteAction
    global static void remoteMethodB () {
        // stuff
    }
}

and child classes that inherit the base class. One of the child classes needs a different implementation of one of the remote action methods:

global with sharing class MyChildClass extends MyBaseClass {
    @RemoteAction
    global static void remoteMethodA () {
        // this is different from MyBaseClass
    }
}

Per Salesforce's Remote Methods and Inheritance, static remote action methods are "inherited"* by child classes. The problem is that Salesforce doesn't know not to inherit remoteMethodA in MyChildClass, and the Visualforce remote object throws an error on page load:

Visualforce Remoting: Unable to create method 'remoteMethodA': already exists on object 'MyChildClass'

It seems to default to using the child class remote action method, as the page works properly, but I would like to clear up the error if I can. Is there a way to tell Visualforce that the child class method overrides the parent class method?

* Static methods are not inherited in any OOP sense - Visualforce walks up the chain of inheritance to build a list of available remote actions.

5
  • Maybe you could just have the @RemoteAction methods in the base class. Then have the body of that method defined somewhere that could be cleanly overridden by the child class. Commented Jul 9, 2015 at 21:52
  • Actually, scratch that. It wouldn't be possible for the base classes static method to call the child classes constructor unless you passed in a parameter indicating which one to make. Commented Jul 10, 2015 at 1:20
  • I can't understand what seems to be the problem in your case. In the example of MyBaseClass and MyChildClass (extending the base class), you could clearly have a different implementation in the remoteMethodA(). Further on, you say "The problem is that Salesforce doesn't know not to inherit remoteMethodA in MyChildClass" - why would you want the Salesforce not to inherit methodA in the child class if you made the child class to extend the base class?
    – dino ursic
    Commented Jul 14, 2015 at 7:09
  • Even further, you say "It seems to default to using the child class remote action method" - well, if you use child class, the remoteMethodA from child class will ofcourse be used. If you'd like to invoke the method from the base class, don't use the child class which 'overrides' the method. Or did I get the question completely wrong? :)
    – dino ursic
    Commented Jul 14, 2015 at 7:11
  • Normally in OOP, static methods are not inherited at all (and indeed, this is the case in APEX). The problem is that Salesforce walks up the inheritance tree and tries to build remote action end points for every @RemoteAction in every class in the inheritance tree. When it reaches a second @RemoteAction with the same name and signature (remoteMethodA in MyBaseClass in this case), it throws an error.
    – asgallant
    Commented Jul 14, 2015 at 21:43

1 Answer 1

2

I guess you have two options:

  1. Raise a bug on the developer forums to see if Salesforce will specifically handle the case where both the parent and child class define the same static remote action method.
  2. Rename the child static method to something else, such as remoteMethodC

I'd recommend the second approach for a few reasons:

  1. It will be clearer to others that the child class is doing something different to the remote action to the base class. Developers probably wouldn't immediately go looking for the definition of the static method in both classes.
  2. You aren't gaining much by using the same method name. It's not like the usage will be polymorphic, as the Visualforce page would need an explicit reference to the child class. Any code reuse would require the child class to call the base classes static method. This isn't dependent on the child class method name.
  3. You won't need to raise a bug with Salesforce support and wait for it to be resolved.

You must log in to answer this question.

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