We have a number of Apex controllers that are shared between Visualforce and Lightning. When these controllers init record values, there are some cases where no record value exists, so the method uses
SObjectType.newSObject(null, true) to spin up a new record for the user to interact with. This was working great. Recently, we have run into two issues with Locker Service:
Unable to read SObject
When the record is sent back to Apex as a method parameter after being initialized via
SObjectType.newSObject(), the frame work will always crash with "Unable to read SObject". We have seen this before and the workaround was to set the
sobjectType property on the object before setting it as a param. However, this leads into the second issue...
We cannot set fields that did not have a default value
Once we have the record in our lightning component (set to an
aura:attribute with a type of custom object) the
lightning:input components are not updating fields on the record. Printing out the record will show that no keys exist for those fields when it comes back from Apex (presumably because the fields had null default values). After entering data in
ui:inputText fields, no key for the field is added to the object so no data gets set. Additionally, we cannot set the
sobjectType property to prevent the "Unable to read SObject" error.
Is this approach no longer supported now that Locker Service is rolling out? If so, what is the correct (supported) approach to initializing empty records for users to enter values for with Locker Service? I think it would help a lot here to understand the Why for some of these restrictions. Knowing that will help developers create designs that better follow best practices while we are waiting for more detailed documentation. If this information already exists somewhere and I just could not find it I would appreciate a link to it.
Update 1: SOQL behavior
I am also seing this behavior with records retrieved via SOQL. I cannot confirm that this wasn't happening before but I am fairly confident. Now, if a record retrieved via SOQL has a null value for a field, that field cannot be set in our lightning component due to the same issues for new records noted above.