11

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);


}})
13

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.

  • 1
    Thanks Praveen. It was indeed sobjectType instead of sObjectType. – s_saiyan Jul 5 '16 at 9:32
  • 3
    Curse the lightning gods! Thanks so much for this answer, I would never have figured that out. I've always used sObjectType in apex and never encountered sobjectType lowercase 'o'. Dang. – Doug Ayers Oct 24 '16 at 23:35
  • Blerg, thank you stack overflow. Nothing like trying to guess the deviations from lower camel case in internal apex. – Ralph Callaway Nov 29 '16 at 18:53
1

When method in apex controller having parameter sObject: - Either set sobjectType attribute inside default in aura:attribute component Or object should have Id to call server side method.

When method in apex controller having specific sObject eg. Account: - No need to declare sobjectType inside default attribute of aura attribute component.

If above condition not satisfied it gives error unable to read sObject.

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.