4

I am not able to pass the parameter from lightning controller to apex controller. In below apex controller "accNo" is null where as in lightning controller I am getting value of accountNumber. Don't know whats wrong in my code. Please help.

Apex Controller

    @AuraEnabled
    public static Banking__c searchAccountApex(String accNo){
       system.debug(accNo);
       Banking__c searchbank = [select Account_Name__c,Balance__c, from banking__c where name = :accNo ];  // Getting accNo null
       return searchbank;
     }

Component

<aura:component controller="Bank_Controller">
<aura:attribute name="AccountNumber" type="String" default=""/>
<aura:attribute name="record" type="Banking__c" default="{'sobjectType':'Banking__c', 'Account_Name__c':'','Deposit_Amount__c':''}"/>
<aura:attribute name="searchResult" type="boolean" default="false" />
<ui:inputNumber label="Account Number" aura:Id="accountNumber1" value="{!v.AccountNumber}" class="Account_Number__c"  />
<ui:button label="search" press="{!c.searchAccount}" /> <br></br>
<aura:if isTrue="{!v.searchResult}">
    Account Name    : <ui:outputText value="{!v.record.Account_Name__c}"  />  <br></br>
    Account number  : <ui:outputText value="{!v.record.name}" /> <br></br>  
    Account Balance : <ui:outputCurrency value="{!v.record.Balance__c}" />  <br></br>
</aura:if>

JavaScript Controller

({
searchAccount : function(component, event, helper) {
     var accountNumberComponent=component.find("accountNumber1");
    var accountNumber = accountNumberComponent.get("v.value");
    console.log(accountNumber);    // getting account number using component.find method
    console.log(component.get("v.AccountNumber"));     // getting account number directly from accoountNumber Attributr
    var action = component.get("c.searchAccountApex");
    action.setParams({
        accNo : component.get("v.AccountNumber")    // Here when if I pass "AccountNumber" attribute, it is not getting passed. getting null in apex controller. 
    });
    action.setCallback(this,function(response){
        if(response.getState()==="SUCCESS"){
            component.set("v.record",response.getReturnValue());
            component.set("v.searchResult",true);

        } 
    });
    $A.enqueueAction(action);

}
})
  • 1
    I just tried your code and I am getting correct value in the apex controller. Did you add a System.debug in the apex method and checked the value of accNo? – Santanu Halder Apr 14 '17 at 9:45
  • yes I checked twice. Its null. I don't know whats the problem and the strange thing is that if i pass a sObject (i.e "record" attribute) it is getting passed but if I pass other variables("accountNumber" variable) ,they are getting null in apex controller. – Muzammil Bajaria Apr 14 '17 at 9:51
  • I tried too... It's alright. works as expected – Rohit Mourya Apr 14 '17 at 9:52
  • Let me check once again – Muzammil Bajaria Apr 14 '17 at 10:06
  • I am still getting null value. – Muzammil Bajaria Apr 14 '17 at 11:20
2

Its very strange, I created a new component and copied code from old component and now its working. I think problem was something else, not in the code. Thank You guys for your help. I really appreciate.

| improve this answer | |
  • This is happening with me as well. I can create a component I guess but wanted to know the root cause, if anyone is aware. – the_phantom Feb 6 '18 at 10:43
  • Same thing is happening to me – SallyRothroat May 22 '19 at 22:32
  • No. Still don't know exactly what was the root cause but never faced this issue again. – Muzammil Bajaria May 23 '19 at 10:48
0

I just had the same problem. I already had 2 parameters and wanted to add 3rd, but it was always null or internal server error, no matter what i did. So in act of desperation I have cleared cookies for the site and it worked.

So if anybody else has this problem again - clear your cookies!

| improve this answer | |
0

Clearing cookies solved this issue 100%

| 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.