22

the following code is causing an infinite loop in my lightning component when I click the ui:button. Checking debug logs, nothing ever goes to the apex controller.

.cmp layout

<aura:attribute name="account" type="Account" default="{ 'sobjectType': 'Account'}"/>
<aura:attribute name="deliveryMethod" type="string[]" />
<ui:inputSelect value="{!v.account.Delivery_Method__c}" multiple="true">
  <aura:iteration items="{!v.deliveryMethod}" var="p">
    <ui:inputSelectOption text="{!p}" label="{!p}"/>
  </aura:iteration>
</ui:inputSelect>
<div>
 <ui:button label="Update Preferences" press="{!c.saveAccount}" />
</div>

js controller:

saveAccount : function(component,event,helper){
    var a = component.get("v.account");
    var saveAction = component.get("c.saveAccount");
    saveAction.setParams({
        acct : a
    })
    saveAction.setCallback(this,function(res){
        console.log('callback');
    });
    $A.enqueueAction(saveAction);
}

and my Apex Controller:

@AuraEnabled
public static ResponseDto saveAccount(Account acct){
    try{
        upsert acct;
    }catch(Exception e){
        ResponseDto res = new ResponseDto(false, 'System Error has occurred:' + e.getMessage());
        return res;
    }
    // Create Object for response as Success Completion. Insert the return value to Map
    ResponseDto res = new ResponseDto(true, '');
    return res;
}

public class ResponseDto {
    @AuraEnabled public Boolean isSuccess { get; set; }
    @AuraEnabled public String message { get; set; }
    @AuraEnabled public Map<Object, Object> values { get; set; }
    public ResponseDto(Boolean isSuccess, String msg){
        this.isSuccess = isSuccess;
        this.message = msg;
        this.values = new Map<Object, Object>();
    }
}
  • 1
    when it is going in infinite, on load or while save ? can you attach screenshot ? – Himanshu Mar 2 '16 at 20:04
  • Also how are you binding response to UI? – Mohith Shrivastava Mar 2 '16 at 20:06
  • it happens when I click the save button. – Jason Benkert Mar 2 '16 at 20:07
  • right now I'm not binding the response to anything - I'm just confirming that it will save the record. Even with just what I have, after I click I get the console output of "callback" infinitely. – Jason Benkert Mar 2 '16 at 20:08
  • Ugh - Just ran into this today. Spent the last 2 hours trying to figure out what the heck was going on. Only when I was posting my question did I see this (after I typed it all out unfortunately. Thanks @JasonBenkert for posting this question and those who answered – Eric Jan 9 '17 at 18:09
45

I found the solution. DO NOT GIVE YOUR JAVASCRIPT METHOD AND YOUR APEX CONTROLLER METHOD THE SAME NAME. When I defined the saveAction:

var saveAction = component.get("c.saveAccount");

the saveAction was set as the js controller's saveAccount method, and not my Apex controller's saveAccount method. I assumed that anytime we referenced "c.methodName" from the js controller we were referencing the Apex method - that's not the case and this seems pretty buggy to me.

Probably should be added as a best practice or perhaps a known issue somewhere.

I changed the js method name to "saveAcct" and everything works now.

| improve this answer | |
  • This just saved me and I can confirm this is great advice. We are now adopting a convention of a "Js" suffix for JS controller methods. – jkraybill Mar 31 '16 at 22:54
  • Hey Jason, you should probably mark this answer as accepted if you resolved your issue. – Adrian Larson May 26 '16 at 2:00
  • 2
    Saved my day! This is really poor design my the Salesforce team. – Chris Ciszak Jan 9 '17 at 15:19
  • You can give the same name, but I'd put title case for Apex methods (for being global) and lowercase for JS one's. – dzh Aug 9 '17 at 15:19
0

As a side note, if you call saveAction.setStorable(); before the call to $A.enqueueAction(saveAction); (as we are encouraged to do) then you get an immediate error: Assertion Failed!: setStorable() cannot be called on a client action.

Which reveals the nature of the Aura Framework bug more clearly. saveAction is not supposed to be a client action!

| improve this answer | |

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.