9

We are consolidating our common methods into an Abstract controller base class. Here's an example:

public abstract class CommunityControllerBase {

    /*
        Returns a select option list of Gender for use with lightning:combobox.
        See: https://help.salesforce.com/articleView?id=000212327&type=1
     */
    @AuraEnabled
    public static List<SelectOption> getGenderPicklistEntries() {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Contact.Gender__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry f: ple) {
            options.add(new SelectOption(f.getValue(), f.getLabel()));
        }
        return options;
    }
}

We are extending the class and calling it from a Lightning Component component:

public with sharing AwesomeController extends CommunityControllerBase {
    // some fancy code ...
}

I can use anonymous Apex to call the base class method getGenderPicklistEntries and get an appropriate result via the extending class:

AwesomeController.getGenderPicklistEntries();

However, when we call the getGenderPicklistEntries method from Lightning, we get an error:

Unable to find action 'getGenderPicklistEntries' on the controller of...

If I copy the method getGenderPicklistEntries into the extending class and comment it out on the base class, it works (finds the method and pulls the list of genders).

Why can't our Lightning component see our base method in the base class, but can see it when it's copied in the extending class?

  • 3
    In addition to Jayant's answer, LWC answers this problem by allowing you to import methods from multiple classes at once, eliminating the need for "extends", since you can mixin any methods you'd like. – sfdcfox Apr 24 at 21:40
8

Why can't our Lightning component see our base method in the base class, but can see it when it's copied in the extending class?

Going through the documentation, it seems that all @AuraEnabled methods used from JS Controller in an Aura Component need to be explicitly annotated in the Class (emphasis mine).

Only methods that you have explicitly annotated with @AuraEnabled are exposed.

While Static methods defined in parent class can be invoked from a child class even if not defined in the child itself, but in case of Lighting Aura/Web Components, even though it's not very well documented for scenarios like inheritance, based on what documentation says, you will need to ensure that the annotated method is explicitly available in the Controller class. So if in your component you have a AwesomeController declared as a Controller, the method you are trying to invoke should be available in that class itself.

In your scenario, if you want to refactor the code, you can achieve it by using something as below. You don't follow inheritance here but at least this way you will be still able to consolidate your common code at one place and be able to utilize it.

public class AwesomeController {

    @AuraEnabled
    public static List<SelectOption> getGenderPicklistEntries() {
        return CommunityControllerBase.getGenderPicklistEntries()
    }
}
  • 1
    You could add a method with @AuraEnabled and the same signature, that simply calls the same method on the parent class. That's probably the best you're going to get. The super keyword won't even work in a static context. – Charles T Apr 24 at 21:41
  • 1
    @JayantDas This question has been bothering me for a couple years and this answer puts my mind to ease. Thank you! – Brian Miller Apr 24 at 22:27
  • 1
    And that static methods defined in a parent class are not extended or inherited in a subclass, but they are hidden -- methods are hidden only if subclass defines a method with same signature. In this case, subclass did not define method with same signature but still hidden? Also, if it is hidden, how is it working in Anonymous execution window? – javanoob Apr 25 at 4:40
  • 1
    @javanoob You were right, I think my answer was a bit confusing too. I have updated it to reflect what exactly is going here. – Jayant Das Apr 25 at 11:24
0

The thing that we discovered that worked pretty well is this sorta hybrid approach - use modular services when needed ad-hoc, but then for more common areas of application, abstract services into one piece. Have a look into this repo:

https://github.com/metacursion/sfdc-lax-benedict

I kept meaning to describe this pattern for a while and only got to do it in last 30 minutes as I saw this question. Let me know if you need more help.

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.