6

I had encountered the "Unable to read SObject" error when attempting to pass an SObject record from Lightning to an @AuraEnabled Apex method.

Based on this question, I first tried setting the "sobjectType". This was effecting when starting with a new record, but not when the record I wanted to save had first been queried from the database.

I knew I could stringify my record and pass the JSON string, which could then be deserialized in Apex, but running as a sys admin and using a very simple component, I should be able to successfully pass the SObject record directly into Apex.

Here is what I found...

The record from the database included an "attributes" attribute when viewing it stringified in the JavaScript console. This is not included when the component creates a new record.

{"attributes":{"type":"Application__c","url":"/services/data/v38.0/sobjects/Application__c/a0E2C000000RvHCUA0"},"Name":"test","Id":"a0E2C000000RvHCUA0"}

In order to get this SObject passed into Apex, I had to set "attributes" to null.

var appSobject = component.get("v.appSobject");
appSobject.attributes = null;

With or without setting "sobjectType", I was able to pass my queried record back into Apex when I cleared this "attributes" value before sending.

The error is also encountered when a query for the initial record traverses a relationship to include a related value, such as "Account__r.Name". In this case, I also had to set the "Account__r" attribute to null before passing the value into Apex.

I hope this information is helpful to someone in need.

Why do these attributes need to be set to null before passing the record back to Apex?

  • Sounds like a bug to me .Have you raised a case ? – Mohith Shrivastava Dec 15 '16 at 3:41
  • This appears to be related to my serializing and deserializing untyped in Apex before returning the Application__c record to Lightning, trying to use a custom Apex wrapper class to return all responses to Lightning. When working directly with the Application__c record (not first wrapping it in Apex as a generic object), I do not encounter this error. – Brian Piggins Jan 16 '17 at 18:19
  • Any reason you are using typed objects in first place? I find it much easier to use generic objects which i de/serialise manually in JS/Apex. – dzh Aug 17 '17 at 12:57
0

I ran into this today... 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.

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.