[SalesForce] Lightning Component force:hasSObjectName, sObjectName undefined

Per the force:hasSObjectName interface documentation, I can reference an automatically added attribute named sObjectName to retrieve the object where my custom lightning component is being accessed. However when I try to use this value, it is undefined (code below):
Is this the correct way to reference the sObjectName? I'm aware I can pull this value from the record id, but I'd like to know what I'm doing wrong.

Lightning Component

<aura:component controller="lc_BuyerSellerSearchCriteriaCTRL" implements="force:hasSObjectName,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" description="lc_BuyerSellerSearchCriteriaDynamic" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
    <!--Component Code-->
</aura:component>

Component Controller

init: function(cmp, event, helper) {
    console.log(cmp.get('v.sObjectName'));
}

This statement in the console log shows undefined

Best Answer

With a quick test I can confirm that this works fine as long as your component is within the right context. The same is mentioned in the documentation (emphasis mine).

Important The sObjectName attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home. In all other cases, such as when you invoke the component as a global action, or create the component programmatically inside another component, sObjectName isn’t set, and your component shouldn’t depend on it.

These unsupported contexts include a few contexts that might seem like they should have access to the current record. Examples of these other contexts include the following:

  • Invoking the component from a global action (even when you’re on a record page)
  • Invoking the component from header or footer navigation in a community (even if the page shows a record)

force:hasRecordId and force:hasSObjectName are unsupported in these contexts. While the marker interfaces still add the relevant attribute to the component, accessing either attribute generally returns null or undefined.

You will need to verify that the component you have is placed within the correct record context.

Related Topic