1

Passing Date in format [YYYY-M-D] like "2016-1-1" gives error "Unable to read SObject's field value[s]"

Steps to reproduce:

  1. Create custom object Custom_Object__c with Date field Date__c

  2. Create Apex Class

    public class DateIssueCont {
    @AuraEnabled
    public static void saveRecord( Custom_Object__c rec ){
        insert rec;
    }
    }
    
  3. Create an App

    <aura:application controller="DateIssueCont">    
    <aura:attribute name="record" type="Custom_Object__c" default="{'sobjectType': 'Custom_Object__c', 'Date__c': ''}" />
    <ui:inputDate value="{!v.record.Date__c}" displayDatePicker="true" /> 
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <div aura:id="errors" class="slds-hide">
        <div class="slds-form-element" >
            <div class="slds-form-element__control">
                <ui:message title="Error" severity="error" >
                    <ui:outputText aura:id="ErrorMessage" value=""/>
                </ui:message>
            </div>
        </div>
    </div>
    <button class="slds-button slds-button--neutral slds-button--brand" onclick="{!c.perform}">Perform Business Logic</button>
    </aura:application>
    
  4. With Javascript Controller

    ({
    init : function(component, event, helper) {
        component.set("v.record.Date__c", '2016-1-1' );
    },
    
    perform : function(component, event, helper) {
        var rec = component.get( 'v.record' );
        var saveRecord = component.get( "c.saveRecord" );
        saveRecord.setParam( "rec", rec ); 
        saveRecord.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
            } else if (state === "ERROR"){
                var errors = response.getError();
                if (errors && errors[0] && errors[0].message) {
                    $A.util.removeClass( component.find("errors"), "slds-hide");
                    component.find("ErrorMessage").set("v.value", errors[0].message);
                }
                if (errors && errors[0] && errors[0].pageErrors && errors[0].pageErrors[0] && errors[0].pageErrors[0].message ) {
                    $A.util.removeClass( component.find("errors"), "slds-hide");
                    component.find("ErrorMessage").set("v.value", errors[0].pageErrors[0].message);
                }
                if (errors && errors[0] && errors[0].fieldErrors  ) {
                    $A.util.removeClass( component.find("errors"), "slds-hide");
                    for ( var u in errors[0].fieldErrors) {
                        component.find("ErrorMessage").set("v.value", errors[0].fieldErrors[u][0].message);
                    } 
    
                }
            }
        });
        $A.enqueueAction(saveRecord);
    }
    }) 
    
    1. Open an app and click button.

    2. Obtain an error which says "Unable to read SObject's field value[s]".

Error: Unable to read SObject's field value[s]

2 Answers 2

3

There are several workarounds for this issue.

  1. The most simple in this case is to use YYYY-MM-DD.

So we would have to change line

component.set("v.record.Date__c", '2016-1-1' );

into

component.set("v.record.Date__c", '2016-01-01' );
  1. Another possible workaround is mentioned here:

We could add line

rec.Date__c = new Date( rec.Date__c );

just after

var rec = component.get( 'v.record' );

line in perform method of javascript controller.

  1. Another general workaround for a custom date population was described here.
0

Did you try by using Date format instead of string ?

component.set("v.record.Date__c", new Date('2016-1-1') );
1
  • yes, I have mentioned that below
    – Patlatus
    Commented Oct 30, 2016 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .