I am building a lightning component in which I am using force:recordData component. I noticed that when I use field attribute with fields from the object and even if one of the fields does not have read access it return a null object when fetched. What is the reason for this? I understand if we fetch a field for which we don't have access, we should get a NULL value returned. But if we are fetching a field that has access then why should a NULL be returned?
Ex:
Scenario 1:
Name, NumberOfEmployees => Read Access
Custom_Field1__c, Custom_Field2__c => No Access
Component:
<aura:attribute name="account" type="Object" />
<force:recordData aura:id="recordLoader"
fields="Name, NumberOfEmployees, Custom_Field1__c, Custom_Field2__c"
recordId="{!v.recordId}"
targetFields="{!v.account}"
targetError="{!v.recordError}"
mode="VIEW"
/>
Controller
var numberofemployees = component.get("v.account.NumberOfEmployees");
console.log('Number of employees: ', numberofemployees); => NULL (returns NULL even when NumberOfEmployees has read access)
console.log('Account: ', component.get("v.account")); => NULL (returns NULL on the object)
Scenario 2:
Name, NumberOfEmployees => Read Access
Custom_Field1__c, Custom_Field2__c => Read Access
#Same code as above
console.log('Number of employees: ', numberofemployees); => #value (returns a value)
console.log('Account: ', component.get("v.account")); => Proxy {} (returns a value)
Best Answer
The underlying API returns a fatal error when you try to query a record with fields you don't have access to, rather than just returning the fields you do have access to. If you check the
targetError
, you'll see an error like No such column 'Custom_Field1__c' exists on entity 'Account'. Consider usinglayoutType="FULL"
if you want to just get all fields for which the user has access to.