2

I tried to pass integer attribute to controller but it didn't pass. Here is the code that i used.

Component

    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" controller="BACSPaymentManagerController">
    <aura:attribute name="NextNDays" type="Integer" />
    <aura:attribute name="BACSConfirmation" type="Boolean" default="false"/>
    <aura:attribute name="paymentRecords" type="List" />
    <aura:attribute name="selectedRows" type="List" />
    <aura:attribute name="rowsSelected" type="List" />
    <aura:attribute name="columns" type="List" />   
    <lightning:card title="BACS" iconName="standard:file">
        <aura:set attribute="actions">
            <lightning:input type="Number" label="Next N days" value="{!v.NextNDays}" />
            <lightning:button class="slds-button_brand" label="Fetch Payments" onclick="{!c.fetchPayments}" />
        </aura:set>

            <div class="slds-col slds-size_12-of-12 slds-p-around_xx-small">

                <lightning:datatable data="{!v.paymentRecords}" columns="{!v.columns}" keyField="Id"
                                     selectedRows="{!v.selectedRows}" onrowselection="{!c.updateSelectedRows}"/>
        </div>
    </lightning:card>
</aura:component>

Controller

({
fetchPayments : function(component, event, helper) {
    helper.fetchPayments(component, event);
})

Helper

({
    fetchPayments : function(component, event) {
        var integerNextNDays = component.get("v.NextNDays");
        var action = component.get("c.getPayments");
        action.setParam({"nextNDays": integerNextNDays});
        action.setCallback(this, function(response){
            console.log(response.getState());
            if(response.getState() == 'SUCCESS'){
                if(response.getReturnValue() != undefined){
                    component.set("v.columns",[
                        {label:'Id', fieldName:'Id',type:'text'},
                        {label:'Name', fieldName:'Name',type:'text',sortable:'true'},
                        {label:'Amount', fieldName:'Amount_Paid__c', type:'currency',sortable:'true', typeAttribute:{currencyCode:'GBP'}},
                    ]);
                    component.set("v.paymentRecords", response.getReturnValue());
                }
            }
        });
        $A.enqueueAction(action);
    }
})

Apex Controller

public class BACSPaymentManagerController {
    @AuraEnabled
    public static List<Payment__c> getPayments(Integer nextNDays){
        if(nextNDays>0){
            try {
                Date dt = system.today() + nextNDays;
                List<Payment__c> allFilteredPayments = [SELECT Amount_Paid__c,Name,Name__c,Account_Number__c,Sort_Code__c,Contact__r.Client_Reference_BACS__c,Date_Due__c FROM Payment__c WHERE Payment_Type__c = 'BACS' AND Confirm_Payment__c = false AND Date_Due__c >= :system.today() AND Date_Due__c <=:dt];
                return allFilteredPayments;    
            }
            catch (Exception e) {
                // "Convert" the exception into an AuraHandledException
                throw new AuraHandledException('Darn it! Something went wrong: '
                                               + e.getMessage());    
            }    
        }
        else
            return [SELECT Amount_Paid__c,Name,Name__c,Account_Number__c,Sort_Code__c,Contact__r.Client_Reference_BACS__c,Date_Due__c FROM Payment__c WHERE Payment_Type__c = 'BACS' AND Confirm_Payment__c = false];

    }
}

Is there anything that i missed? Thanks

4
  • Whats the response of console.log? Commented May 19, 2018 at 17:12
  • Did you receive anything at apex method. Try changing that apex param to String. . Commented May 19, 2018 at 17:13
  • 3
    While calling server side method, setting parameters method name should be setParams? Commented May 19, 2018 at 17:15
  • @PranayJaiswal the response is SUCCESS but it goes to else statement instead of if statement Commented May 19, 2018 at 17:17

1 Answer 1

4

Could be a small issue, try renaming 'setParam' to 'setParams' for setting parameters while calling server side action.

2
  • You are right, i am so embarrassed didn't notice that. Thanks for your help Raul! Commented May 19, 2018 at 17:20
  • 1
    No worries, we all make mistakes and with lightning/JavaScript things sometimes get hard to debug. Happy coding! Commented May 19, 2018 at 17:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.