Skip to main content
4 of 4
added 1 character in body
sfdcfox
  • 501.7k
  • 21
  • 473
  • 828

Param passed from Lightning Component to Apex controller is read as null

I am trying to pass a string from my Lightning Component to an Apex controller, but when it is received by the Controller it is interpreted as null. The controller works fine and is returning data, but only when it does not process the parameter. Any advice? I have seen that there are other questions similar to this, but I have not found any applicable answers; I did find one that said "paste into a new component and try again." is that really the only fix?

component:

<aura:component
    description="Shelter Nightly Attendance" 
    controller="ShelterNightlyAttendanceController"
    implements="force:appHostable">
<!-- attributes -->
<aura:attribute name="bedNightWrappers" type="BedNightWrapper[]"/>
<aura:attribute name="bedNightDate" type="Date" default=""/>
<aura:attribute name="MorningChore" type="String" default=""/>
<aura:attribute name="EveningChore" type="String" default=""/>

<!-- handlers -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

<!-- page -->
<ui:inputDate label="Attendance for:" value="{!v.bedNightDate}" displayDatePicker="true" />

<table class="slds-table slds-table--bordered slds-table--cell-buffer slds-table--fixed-layout">
    <thead>
    <th>Bed Night Id</th>
    </thead>
    <tbody>
    <aura:if isTrue="{!!empty(v.bedNightWrappers)}">
        <aura:iteration var="bedNightWrapper" items="{!v.bedNightWrappers}" >
            <tr onclick="{!c.selectRecord}">
                <td><ui:outputText value="{!bedNightWrapper.bedNightId}"/></td>
                <td><c:getPicklist label="Evening Chore" objectName="Bed_Night__c" fieldName="Evening_Chore__c" value="{!bedNightWrapper.eveningChore}"/></td>
                <td><c:getPicklist label="Morning Chore" objectName="Bed_Night__c" fieldName="Morning_Chore__c" value="{!bedNightWrapper.morningChore}"/></td>
                <td><ui:outputText value="{!bedNightWrapper.dateOfStay}"/></td>
            </tr>
        </aura:iteration>
    </aura:if>
    </tbody>
</table>
</aura:component>

controller:

({
doInit : function(component, event, helper) {
    var today = helper.getToday();
    component.set('v.bedNightDate', today);
    helper.getBedNightWrappers(component, today);
},

getBedNights : function(component, event, helper) {
    var date = component.get('v.bedNightDate');
    helper.getBedNightWrappers(component, date);
}
});

helper:

({
getToday: function () {
    var today = new Date();
    var monthDigit = today.getMonth() + 1;
    if (monthDigit <= 9) {
        monthDigit = '0' + monthDigit;
    }
    var dayDigit = today.getDate();
    if (dayDigit <= 9) {
        dayDigit = '0' + dayDigit;
    }
    var dateString = today.getFullYear() + "-" + monthDigit + "-" + dayDigit;
    console.log("typeof dateString: " + typeof dateString); // returns string
    return dateString;
},

getBedNightWrappers: function (component, date) {

    var action = component.get("c.getBedNightWrappersServer");
    console.log("Hello from getBedNightWrappers (JS)! " +
        "date: " + date + ", typeof date: " + typeof date); // returns "2018-03-16", string

    action.setParam({
        "bedNightDate" : date
    });
    //Setting the Callback
    action.setCallback(this, function (a) {
        //get the response state
        var state = a.getState();

        //handle the response state
        switch (state) {
            case "SUCCESS":
                var result = a.getReturnValue();
                if (!$A.util.isEmpty(result) && !$A.util.isUndefined(result))
                    component.set("v.bedNightWrappers", result);

                break;
            case "ERROR":
                console.log(a);
                var errors = a.getError();
                alert('Error in calling server side action for getBedNightWrappers.' +
                    errors[0].message); // reports that "an internal server error occured" when I process the date param
                break;
            default:
                return;

        }
    });

    //adds the server-side action to the queue
    $A.enqueueAction(action);
}
});

Apex class:

public class ShelterNightlyAttendanceController {

@AuraEnabled
public static List<BedNightWrapper> getBedNightWrappersServer(String bedNightDate) {
    System.debug('hello from getBedNightWrappersServer (Apex)! ' +
            'bedNightDate: ' + bedNightDate); // returns null
    //Date queryDate = Date.parse(bedNightDate);

    List<Bed_Night__c> bedNights = [SELECT Id,
            Date_Of_Stay__c,
            Evening_Chore__c,
            Morning_Chore__c
    FROM Bed_Night__c /*WHERE Date_Of_Stay__c = :queryDate*/];
    List<BedNightWrapper> bedNightWrappers = new List<BedNightWrapper>();
    If(bedNights.size() > 0) {
        for(Bed_Night__c bedNight : bedNights) {
            bedNightWrappers.add(new BedNightWrapper(bedNight));
        }
    }
    return bedNightWrappers;
}
}

wrapper class:

public class BedNightWrapper {
@AuraEnabled public Id bedNightId;
@AuraEnabled public Date dateOfStay;
@AuraEnabled public String eveningChore;
@AuraEnabled public String morningChore;

public BedNightWrapper() {
    bedNightId = null;
    dateOfStay = null;
    eveningChore = null;
    morningChore = null;
}

public BedNightWrapper(Bed_Night__c bedNight) {
    this.bedNightId = bedNight.Id;
    this.dateOfStay = bedNight.Date_Of_Stay__c;
    this.eveningChore = bedNight.Evening_Chore__c;
    this.morningChore = bedNight.Morning_Chore__c;
}
}

I am banging my head against the wall - I am using primitives so I didn't think I would face any translation issues.

Thom B
  • 85
  • 2
  • 8