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
Gautam_KasukhelaGautam_Kasukhela 

Unable to read sObject (while fetching Picklist values for a Lightning component)

Hello All,
    Need help in resolving an issue. I am trying to display the Status picklist field on a lightning component. For this I have written a server side controller to fetch the values and set in the component. 
During the callback in the helper, I keep encountering an error where the reposne is ERROR and the message in the ERROR reads "Unable to read sObject". Below is the code: 

Component Code
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"
                access="global"
                controller="FetchValuesFromDatabase">

<aura:handler name="init" value="this" action="{!c.doInit}"/>

<aura:attribute name="case" type="Case" default="{'sObjectType': 'case'}"/>
<aura:attribute name="listOfStatusPicklistValues" type="list" default="[]" description="List of Status Picklist values on Case object"/>

<aura:attribute name="statusFieldAPI" type="string" default="status" description="API name of the Status field whose picklist values we want to fetch"/>
<aura:attribute name="originFieldAPI" type="string" default="origin" description="API name of the Origin field whose picklist values we want to fetch"/>

    <!-- CREATE NEW CASE -->
    <div aria-labelledby="newCaseForm">
        <!-- BOXED AREA -->
        <fieldset class="slds-box slds-theme--default slds-container--small">
        <legend id="newCaseForm" class="slds-text-heading--small 
          slds-p-vertical--medium">
          Add Case
        </legend>
  
        <!-- Create New Case Form : START -->
        <form class="slds-form--stacked">          
            <lightning:select aura:id="newCaseForm" label="Status"
                              name="caseStatus"
                              value="{!v.case.Status}"
                              required="true">
                <aura:iteration items="{!v.listOfStatusPicklistValues}" var="status">
                    <option value="{!status}">{!status} </option>
                </aura:iteration>              
                              

            </lightning:select>                  
            <lightning:select aura:id="newCaseForm" label="Case Origin"
                              name="caseOrigin"
                              value="{!v.case.Origin}"
                              required="true"/>                   
            <lightning:textarea aura:id="newCaseForm" label="Description"
                             name="caseDescription"
                             value="{!v.case.Description}"/>
            
    
        </form>
        <!-- Create New Case Form : END -->
      </fieldset>
      <!-- / BOXED AREA -->
    </div>
    <!-- / CREATE NEW CASE -->
</aura:component>
Controller Code:-
({
    doInit : function(component, event, helper) {
        //get the details of the Object and its fields that we want to retrieve. These have been defined
        // as attributes in the component.
        var picklistFieldAPI = component.get("v.statusFieldAPI");
        var objDetails       = component.get("v.case");

        //call the helper to fetch the values of the picklist from database
        helper.fetchPickListValues(component,objDetails,picklistFieldAPI);
        

    }
})

Helper Code
({
    fetchPickListValues : function(component,objDetails,picklistFieldAPI) {
            //invoke the server-side controller to get the picklist values
            var action = component.get("c.getPicklistValues");
            action.setParams({
                'objDetails' : objDetails,
                'picklistFieldAPI' : picklistFieldAPI
            });
        //set callback action
        action.setCallback(this,function(response){
            debugger;
            if(response.getState() == "SUCCESS"){
                //set the response in the component field
               component.set("v.listOfStatusPicklistValues",response.getReturnValue());
            } else if(response.getState() == "ERROR"){
                var errors = response.getError();
                alert(errors);
            }
        });
			$A.enqueueAction(action);
    }
})

Client-Side Controller Code
public class FetchValuesFromDatabase {
   
    @AuraEnabled
    public static List<String> getPicklistValues(SObject objDetails, string picklistFieldAPI){
        List<String> pickListValues = new List<String>();

        //Check if the object type passed from the component is not null. I yes, return empty list
        Schema.sObjectType objType = objDetails.getSObjectType();
        if(objType == null){
               return pickListValues; 
        }

        //Check if the field API names passed from the component are present on the object.
        //If not, return an empty list
        //Get the fields corresponding to the object in a MAP
        Map<String,Schema.SObjectField> objFieldsMap = objType.getDescribe().fields.getMap();
        if(!objFieldsMap.containsKey(picklistFieldAPI)){
            return pickListValues;
        }
        
        //Get the picklist values of the field
        List<Schema.PicklistEntry> pickListEntries = objFieldsMap.get(picklistFieldAPI).getDescribe().getPicklistValues();
        for(Schema.PicklistEntry ple: pickListEntries){
            pickListValues.add(ple.getValue());
        }
        System.debug('pickListValues: '+pickListValues);
        return pickListValues;
    }
}

what am i missing ?​​​​​​​

 
Best Answer chosen by Gautam_Kasukhela
Gautam_KasukhelaGautam_Kasukhela
Figured it out 

Make sure 'sobjectType' in your attribute definition is the correct case. I had 'sObjectType' and it took me forever to figure out why it wasn't even calling the Apex controller. Got the answer from 
https://salesforce.stackexchange.com/questions/152577/unable-to-read-sobject-error-in-lightning-component-when-record-has-been-queried

All Answers

Gautam_KasukhelaGautam_Kasukhela
Just to add, I have all the permissions required on the Case object (System admin profile).
Gautam_KasukhelaGautam_Kasukhela
Figured it out 

Make sure 'sobjectType' in your attribute definition is the correct case. I had 'sObjectType' and it took me forever to figure out why it wasn't even calling the Apex controller. Got the answer from 
https://salesforce.stackexchange.com/questions/152577/unable-to-read-sobject-error-in-lightning-component-when-record-has-been-queried
This was selected as the best answer