Locker Service: Cannot set field values that were null on records from SOQL or SObjectType.newSObject()

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 lightning:input or 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.

Question

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.

Answer

Salesforce support confirmed that the root cause is same as this Known Issue:

LockerService – New properties added to an object in JavaScript are not visible

Summary
When LockerService is activated new properties added to an object in JavaScript are not visible.

This is being fixed as a part of release 208 which is a summer release scheduled on 22nd April 2017. I am trying to get an early access in our sandbox, will update this space after I test the fix.

Attribution
Source : Link , Question Author : dsharrison , Answer Author : Community

Leave a Comment