[SalesForce] the use of objectApiName in lightning:recordViewForm

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

  1. <lightning:recordViewForm recordId="0066F00000kkQVfQAM" objectApiName="Opportunity">

  2. <lightning:recordViewForm recordId="0066F00000kkQVfQAM" objectApiName="">

  3. <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:

  • that there is a base component that lightning:recordViewForm extends that supports the "new" case where there is no Id and so the type needs to be known
  • having the type up front is an optimisation compared with working out the type from the Id
  • as the component evolved, the implementation did require it in early versions but not in later versions but the attribute has been left required