1

I'm trying to call an Apex Controller method from JS Helper of the Lightning component. But when I try to pass a parameter of the type of a custom object the apex method is not getting invoked.

I don't understand what is causing this error, no error is shown on Debug Log. There are no namespaces used in the org.

Kindly help me out.

I have shared the code fragment.

Lightning Component

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="MSIApplicationFormLightningController">

  <aura:attribute name="application" type="Applications__c" />

  <div aura:id="page-content" class="toggle">

      <ui:inputText value="{!v.application.Major__c}"
                    class="slds-input question-input" 
                    required="true" />      

       <div class="button-grid">
           <ui:button class="custom-button" label="SAVE DRAFT" press="{!c.saveApplicationAsDraft}"/>
       </div>

  </div>

</aura:component>

JS Controller

({       
    saveApplicationAsDraft: function(component, event, helper){

            helper.saveApplication(component, event);

    }

})

JS Helper

({
    saveApplication: function(component, event){  

        var action = component.get("c.saveMSIApplication");


        action.setParams({ "application" : component.get("v.application")});

        action.setCallback(this, function(response){          

            console.log(response.getState());

            console.log(JSON.stringify(response.getReturnValue()));

            if(component.isValid() && response.getState() == "SUCCESS"){

                console.log(response.getReturnValue());
            }

        });

        $A.enqueueAction(action);

    }
})

APEX Controller

public without sharing class MSIApplicationFormLightningController {

  @AuraEnabled
    public static String saveMSIApplication(Applications__c application){

      String appId = 'test';


      return appId; 
    }

}//End of class

Console log:

 "ERROR" for response.getState() 
 "null" for response.getReturnValue()
  • 1
    Try logging response.getError() and see what it tells you – Fernando Gavinho Oct 20 '16 at 15:25
  • How are you supplying application to your component? It's not set on init nor is a default value declared. – André Dion Oct 20 '16 at 16:27
  • Thanks a lot for everybody, after specifying the default attribute value the Apex Controller method got invoked. – Ram Oct 21 '16 at 5:17
2

Always declare a default value for your attribute so that it is not undefined

<aura:attribute name="application" type="Applications__c"  default="{ 'sobjectType': 'Applications__c',
                      'Major__c':''
 }">
</aura:attribute>
| improve this answer | |
  • Thanks a lot, after specifying the default attribute value the Apex Controller method got invoked. – Ram Oct 21 '16 at 5:17

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.