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
SScholtzSScholtz 

The proper way to refer to sObject fields in Lightning Component/JS

What is the proper way to refer to fields on an sobject in your JS controller?  Assuming I have an attribute "salesCall" of type "Sales_Call__c", is it....
 
var salesCall = component.get('v.salesCall');
console.log(salesCall.myField);

or
 
console.log( component.get('v.salesCall.myField') );

The former seems intuitive - console.log() the salesCall var and you get a JSON object - but is giving me radically wonky and inconsistent results.  Sometimes I get a value, sometimes I get undefined, even though the JSON object has it.  And forget about trying to actually set a value on it.

The latter method seems to perform more consistently and I'm getting much better results with that.

But is there a technical reason why the former won't work?  Some thing to do with Aura's data binding?
SScholtzSScholtz
PS - setting a variable also seems to work out the same way....
 
var salesCall = component.get('v.salesCall');
salesCall.Some_Field__c = "whatever";

...does not really work at all, but ...
component.set('v.salesCall.Some_Field__c', 'whatever');

...does.
 
SScholtzSScholtz
Edit: Just to clarify, the times when I use salesCall.Some_Field__c and I'm not getting a consistent result when I'm handing an event and that field has been bound to an input field.  I'm expecting that salesCall.Some_Field__c would have a value from the input field, but it doesn't.

However, if I ask for component.get('v.salesCall.Some_Field__c'), then I get the value as it stands in the input field.

I don't know the inner workings of the Aura framework, but I'm guessing that
component.get('v.salesCall');

...and...
component.get('v.salesCall.Some_Field__c');

...are distinct and unique from each other.  Those are string keys after all, even if it looks like dot notation.  They're separate from each other and updating one doesn't update the other until later in Aura's rendering cycle...or something.  I'm just guessing at this point! :P