Unable to read sObject error when saving Record From a Lightning Component’s Controller

I am trying to insert a record in a custom sObject ‘Master_a__C’ using lightning component. When i am clicking on save button, ‘Unable to read sObject‘ error is coming upon controller’s logic. Below are all the source code files.
Any help is appreciated.

Component File :

<aura:component controller=RandomWorkSpace_Cntrl'> 
       <aura:attribute name="master_a" type="Master_a__C" default="{'sObjectType' : 'Master_a__C', 'Name' : ''}"/>

<form>
    <ui:inputText aura:id="name" value="{!v.master_a.Name}"  />
    <ui:button press="c.saveMaster" label="save"/>
</form>

</aura:component>

RandomWorkSpace_Cntrl :

public with sharing class  RandomWorkSpace_Cntrl{

@AuraEnabled
public static Master_a__C saveRecord(Master_a__C mas){
    upsert mas; 
    return mas; 
} }

Controller.js :

({
saveMaster : function(component, event, helper) {

    var master = component.get("v.master_a"); 
    var action = component.get("c.saveRecord");
    action.setParams({"mas" : master});

    console.log("mastera ---->" + JSON.stringify(master));

    action.setCallback(this, function(response){
       var state = response.getState();
        if(state == "SUCCESS" && component.isValid()){
           console.log("success") ;

        }
        else{
            console.log("failed  ::: " + response.getError()[0].message); // Unable to read sObject

        }

    });

    $A.enqueueAction(action);


}})

Answer

The error is due the case-sensitivity issue.It should haven been sobjectType instead of sObjectType in the default attribute of master_a

Always remember that attributes and it’s values in component markup and variables you declare in controller.js and helper.js are all case-sensitive.

Attribution
Source : Link , Question Author : s_saiyan , Answer Author : Praveen

Leave a Comment