Lightning component calling wrong overloaded apex class method

I have a lightning component that displays Knowledge Articles based on Topic IDs that it is configured for. This works great so I went to create my test coverage for it and discovered that the inserted test Knowledge Articles can only ever be ‘Draft’ making my tests fail since the apex class only looks for ‘Online’. Instead of rewriting my component I decided to overload the apex method so that when called without the status parameter it passes in ‘Online’ allowing my tests to pass in ‘Draft’ instead. However, the first method signature does not get called from the component resulting in null for the status parameter.

Here is the component controller:

({
    "retrieveArticles" : function(component, event, helper) {
        var action = component.get("c.returnArticlesWithTwoTopicIds");
        action.setParams({
            mainTopicId : component.get("v.mainTopicId"),
            secondaryTopicId : component.get("v.secondaryTopicId"),
            limitAmount : component.get("v.limitAmount")
        });

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                // handle SUCCESS
            }
            else if (component.isValid() && state === "ERROR") {
                // handle ERROR
            }
        });
        $A.enqueueAction(action);
    }
})

Here is the apex class:

public with sharing class KnowledgeArticleVersionCtrl {
    @AuraEnabled
    public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, Integer limitAmount) {
        return returnArticlesWithTwoTopicIds(mainTopicId, secondaryTopicId, limitAmount, 'Online');
    }

    @AuraEnabled
    public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, Integer limitAmount, String status) {
        // build query string
        List<KnowledgeArticleVersion> articles = Database.query(query);
        return articles;
    }
}

Is there something about the component that would cause it to not use the first signature, the one it should match, in place of the second one, which it shouldn’t match?

Answer

I think in general it’s asking for trouble to overload a method and make both versions @AuraEnabled. You’ve seen that it will allow you to fire the method with missing parameters, so I suggest you just keep only the second method and check for a null:

@AuraEnabled
public static List<KnowledgeArticleVersion> returnArticlesWithTwoTopicIds(String mainTopicId, String secondaryTopicId, Integer limitAmount, String status) {
    status = (status == null) ? 'Online' : status;
    // build query string
    List<KnowledgeArticleVersion> articles = Database.query(query);
    return articles;
}

And if you have a scenario where overloads are useful in your Apex code but only one version ever needs to be called from Lightning, then only make that one overload @AuraEnabled.

Attribution
Source : Link , Question Author : Matthew Green , Answer Author : Charles T

Leave a Comment