4

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.

  • Can you add your component code showing your attributes and client side JavaScript controller – Mohith Shrivastava Dec 7 '16 at 2:12
  • I've update the question with more detail, is this enough? – nicstella Dec 7 '16 at 2:37
  • I need more info on how you are setting date fields to investigate – Mohith Shrivastava Dec 7 '16 at 10:12
4

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);
}
  • 1
    While this is a work around, it is extremely simple to implement and so I will accept it. – nicstella Dec 8 '16 at 20:40
3

This also works if you're dealing with typed parameters directly, i.e. if you have this in your Apex controller:

@AuraEnabled
public static void MyMethodOfDoom(Date dayOfReckoning)
{
    if(System.today() == dayOfReckoning)
        delete [select Id from Contact];
}

And you're passing the data by creating a JavaScript date object:

let doom = new Date();

You're going to be getting null for dayOfReckoning.

Simply changing the Apex controller to take a String parameter so is enough (you can leave the JavaScript as is):

@AuraEnabled
public static void MyMethodOfDoom(String dayOfReckoning)
{
    if(System.today() == Date.valueOf(dayOfReckoning))
        delete [select Id from Contact];
}

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.