[SalesForce] Passing Date field from Lightning component to Apex controller shows null

I have a custom salesforce object with a Date field. I am creating a new object of this type in a Lightning component, and trying to push this to the server.

The objects are stored in a component attribute:

<aura:attribute name="objectsToInsert" type="Object__c[]"/>

Which is acted on by the controller method:

cmp.set("v.objectsToInsert", objectsToInsert);
helper.insertObjects(cmp, cmp.get("v.objectsToInsert")); 

When the attribute is set, the date field has already been set. It is set as a string in the format "YYYY-MM-DD" as shown below.

helper.insertObjects() includes:

var insertAction = cmp.get("c.insertObjects");
insertAction.setParam("objects", objects);      
console.log("entries: " + JSON.stringify(objects));

The log statement there shows a date field

"Day__c":"2016-12-07"

Outputting the passed objects in the controller like this

@AuraEnabled
public static void insertObjects(List<Object__c> objects) {
    System.debug(objects);
}

The debug statement here shows the field as

 Day__c=null

I have tried looking at other answers that were getting errors with Date fields, but can't find any solution or other examples of where the value just gets nulled. How do I fix this?

UPDATE ON SOLUTIONS: The accepted answer below describes a very simple workaround which will probably be sufficient. For others who would rather not use this method, the field containing the Date (Day__c) comes out of the serialiser with a value of Day__c=2016-12-09 00:00:00, i.e. it has a (zero'd out) time string, which may be necessary for apex to understand the data coming from Lightning.

Best Answer

Try this ;)

You have to pass function parameter value in String format then deserialize them;

@AuraEnabled
public static void insertObjects(String objects) {
    List<Object__c> objList = (List<Object__c>) Json.deserialize(objects, List<Object__c>.class);

    System.debug(objList);
}
Related Topic