[SalesForce] How to use @AuraEnabled base class method in Lightning Component

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?

Best Answer

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()
    }
}
Related Topic