While exploring Lightning Data Service, I came across another option to load record without apex/soql in Lightning components using lightning:recordViewForm
tag, which works in conjunction with lightning:outputField
to display specific fields of the record provided by recordId
attribute.
There is another attribute of this component, objectApiName
and the documentation only gives following information about the attribute
The API name of the object. (this attribute is required)
Now, the fun part is, I just have to put this attribute in the component, no matter what value
I provide against this attribute, the component is rendered successfully on the basis of the recordId
. Few examples given below
Examples-Component rendered successfully
-
<lightning:recordViewForm recordId="0066F00000kkQVfQAM" objectApiName="Opportunity">
-
<lightning:recordViewForm recordId="0066F00000kkQVfQAM" objectApiName="">
-
<lightning:recordViewForm recordId="0066F00000kkQVfQAM" objectApiName="garbage">
Only case where the component is not rendered, when the objectApiName
is not provided, which offcourse is required
<lightning:recordViewForm recordId="0066F00000kkQVfQAM" >
What is the purpose of objectApiName
attribute if it has not impact on the outcome?
Best Answer
Purely speculation, but my guesses are:
lightning:recordViewForm
extends that supports the "new" case where there is no Id and so the type needs to be known