function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NanduNandu 

Error message: Unable to read SObject

<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" controller="lntg_DataTableClass" access="global" >
    <aura:attribute name="data" type="Object" default="{'sobjectType':'Account'}"/>
    <aura:attribute name="columns" type="List"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:card title="Account Editable Datatable">
        <lightning:datatable
                aura:id="accountDataTable"
                keyField="id"
                data="{!v.data}"
                columns="{!v.columns}"
                onsave ="{!c.onSave}"
                hideCheckboxColumn="false"/>
    </lightning:card>
</aura:component>


({
    doInit : function(component, event, helper) {
        var tableHeder=[
            {label: 'Name', fieldName: 'Name', editable:false, type: 'text'},
            {label: 'Industry', fieldName: 'Industry', type: 'text'},
            {label: 'Phone', fieldName: 'Phone', editable:true, type: 'Phone'},
            {label: 'Rating', fieldName: 'Rating', editable:true, type: 'text'}
        ];
        component.set("v.columns",tableHeder);
        helper.getdata(component,event,helper);
    },
    onSave:function(component,event,helper){
       
        helper.saveDataTable(component,event,helper);
    }
})



({
    getdata : function(component,event,helper) {
        var action=component.get("c.getAccounts");
        action.setCallback(this,function(response){
            var state=response.getState();
            if(state=="SUCCESS"){
                component.set("v.data",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
    saveDataTable:function(component,event,helper){
        var action=component.get("c.editRecordsupdated");
        /*var editRec =  component.find("accountDataTable").get("v.draftValues");
        alert('editRec..'+JSON.stringify(editRec));*/
        
        var draftValues = event.getParam('draftValues');
        var editRecordsLenght=draftValues.length;
        
        alert('draftValues..'+draftValues);
        action.setParams({
            'editReds':draftValues
        });
        action.setCallback(this,function(response){
            var state=response.getState();
            alert('state...'+state);
            if(state=="SUCCESS"){
                if(response.getReturnValue() === true){
                    alert('Record updated');
                    helper.showToast({
                        "title": "Record Update",
                        "type": "success",
                        "message": editRecordsLenght+" Account Records Updated"
                    });
                    helper.reloadDataTable();
                } else{ //if update got failed
                    helper.showToast({
                        "title": "Error!!",
                        "type": "error",
                        "message": "Error in update"
                    });
                }
            }else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
    },
    /*
     * Show toast with provided params
     * */
    showToast : function(params){
        alert('showToast');
        var toastEvent = $A.get("e.force:showToast");
        if(toastEvent){
            toastEvent.setParams(params);
            toastEvent.fire();
        } else{
            alert(params.message);
        }
    },
    
    /*
     * reload data table
     * */
    reloadDataTable : function(){
        var refreshEvent = $A.get("e.force:refreshView");
        if(refreshEvent){
            refreshEvent.fire();
        }
    }
})

public class lntg_DataTableClass {
    @AuraEnabled
    public static List<Account> getAccounts(){
        return[select id,name,industry,phone,rating from Account limit 5];
    }
    @AuraEnabled
    public static Boolean editRecordsupdated(List<Account> editReds){
        try{
            System.debug('beforce editRecords..'+editReds);
            update editReds;
            System.debug('editRecords..'+editReds);
            return true;
        }catch(Exception e){
            System.debug('e.'+e);
            return false;
        }
    }

}

 
Raj VakatiRaj Vakati
Make sure your profile is having access for the account Object with Read and EDIT and delete permission ... Code is woking fine for me without any issues 
NanduNandu
@Raj
That profile having full permitions like view all and modifiy all but it's throwing error like Unable to read SObject

Thank you Raj
Raj VakatiRaj Vakati
Can you try with sharing class ..??
NanduNandu
Not working. same error....Error message: Unable to read SObject
Raj VakatiRaj Vakati
do you have any apex class by Name "Account" .. If so please delete and try 
Raj VakatiRaj Vakati
Go to apex classes and check is there any apex class "Account" whihc is created 
NanduNandu
No apex classes  on "Account" Name
Raj VakatiRaj Vakati
Change it to 
 
<aura:attribute name="data" type="Account" default="{'sobjectType':'Account'}"/>

 
Raj VakatiRaj Vakati
<aura:attribute name="data" type="Object" />

 
NanduNandu
User-added image
it's throwing same error....

my component code
Gonçalo MoreiraGonçalo Moreira
I once had this issue and solved it by using JSON.stringify while setting the params on the Lightning Component and then using System.JSON.deserialize on the Apex Controller to cast the String into the desired data type.

Something like:
 
action.setParams({
'editReds':JSON.Stringify(draftValues)
});

and then on the Apex Controller:
@AuraEnabled
    public static Boolean editRecordsupdated(String editReds){
        try{
            List <Account> editRedsLst = (List<Account>) System.JSON.deserialize(editReds, List<Account>.Class);
            System.debug('beforce editRecords..'+editReds);
            update editRedsLst;
            System.debug('editRecords..'+editRedsLst);
            return true;
        }catch(Exception e){
            System.debug('e.'+e);
            return false;
        }
    }

}