Skip to main content
Tweeted twitter.com/StackSalesforce/status/807255657648943104
added 404 characters in body
Source Link
nicstella
  • 589
  • 1
  • 7
  • 21

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.

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?

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.

edited tags
Link
Mohith Shrivastava
  • 91.9k
  • 19
  • 161
  • 214
added 404 characters in body
Source Link
nicstella
  • 589
  • 1
  • 7
  • 21

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 code callingobjects are stored in a component attribute:

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

Which is acted on by the server actioncontroller method:

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

When the attribute is thisset, 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?

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 code calling the server action is this:

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?

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?

Source Link
nicstella
  • 589
  • 1
  • 7
  • 21
Loading