• Pawan Kumar 407
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
The Lightning Components Developer Guide warns about using the same name for client and server controller actions:
Use unique names for client-side and server-side actions in a component. A JavaScript function (client-side action) with the same name as a server-side action (Apex method) can lead to hard-to-debug issues.
I was unlucky enough to miss this tip before I debugged one of those hard-to-debug issues. To prevent others from making the same mistake, I wanted to share my debug path in case you miss the memo about naming conventions and run into similar symptoms:

My component contains a button that runs a client controller action named "c.getPricing"

<aura:component controller="ServerController">
  <aura:attribute name="products" type="ServerController.ProductPrice[]"></aura:attribute>
  <ui:button aura:id="buttonGetPricing" label="Get Pricing" press="{!c.getPricing}"></ui:button>

The controller action calls a helper action with the same name: "hlp.getPricing"

    getPricing: function(cmp,evt,hlp){
        console.log('in c.getPricing');

My helper attempts to call a server-side action "c.getPricing", which is the same name as the client-side action "c.getPricing"

    getPricing : function(cmp,hlp)
        console.log('in hlp.getPricing');
        var action = cmp.get('c.getPricing');
        var products = cmp.get('v.products');
        action.setParams({productList : products});
            console.log('in hlp.getPricing callback');
            var state = res.getState();
            console.log('hlp.getPricing ' + state);
            if (state === "SUCCESS") {
                var pricing = JSON.parse(res.getReturnValue().body);

The server code successfully passes unit testing, but when launched from my Lightning Component helper class, no "/aura" logs are created on the server, indicating the server code is never touched.

public class ServerController{

    public static Response getPricing(List<ProductPrice> productList) {...}

Back To The Client...
So the server action is never run, and my javascript console is growing a fearsome loop of the following log lines:
in c.getPricing             # client controller action triggered
in hlp.getPricing           # helper action triggered
in hlp.getPricing callback  # callout to "c.getPricing" triggered
hlp.getPricing SUCCESS      # callout successfully returned
undefined                   # the results of res.getReturnValue() are empty
in c.getPricing
in hlp.getPricing
in hlp.getPricing callback
hlp.getPricing SUCCESS

The Moral of this Story
When the helper attempts to "get" a server action from the component, it receives the client action instead, because they use the same name. This causes an endless loop on the client, where the controller executes itself again and again via the helper's action call. The callback won't throw any errors and happly continues with a "SUCCESS" state.

The fix to this all, of course, is quite simple.

First change the name of the server controller action:

// public static Response getPricing(...) {...}
public static Response getServerPricing(...){...}

Then update the helper reference to the server controller:

// var action = cmp.get('c.getPricing');
var action = cmp.get('c.getServerPricing');

Happy Lightning Development!
  • March 27, 2016
  • Like
  • 1