Plat-Dev-301 practice question 7 of 10

A developer is developing a reusable Aura component that will reside on an sObject Lightning pagewith the following HTML snippet:HTML<aura:component…

Full set: 160 questions

A developer is developing a reusable Aura component that will reside on an sObject Lightning page

with the following HTML snippet:

HTML

<aura:component implements="force:hasRecordId,flexipage:availableForAllPageTypes">

<div>Hello!</div>

</aura:component>

How can the component's controller get the context of the Lightning page that the sObject is on

without requiring additional test coverage?

Answer options
A:

A. Create a design attribute and configure via App Builder.

B:

B. Set the sObject type as a component attribute.

C:

C. Use the getSObjectType() method in an Apex class.

D:

D. Add force:hasSObjectName to the implements attribute.

Correct answer
Correct Answer:
D. D. Add force:hasSObjectName to the implements attribute.
Explanation

The force:hasSObjectName interface is a marker interface specifically designed for this use case. By adding it to the component's implements attribute, the Lightning Framework automatically provides the API name of the current sObject in a component attribute named sObjectName. This approach retrieves the page context declaratively without requiring any Apex controller code. Since no Apex is involved, no additional test coverage is needed, directly satisfying all requirements of the question.

Why Incorrect

A. A design attribute requires manual configuration by an administrator in the App Builder for each instance of the component, making it less reusable and not context-aware.
B. This is not a mechanism for automatically getting context. It would require the developer using the component to manually pass the sObject type in the markup.
C. Using an Apex method like getSObjectType() requires a server-side call and, more importantly, any Apex code must have associated test coverage, which violates a key constraint.

References

Salesforce Lightning Aura Components Developer Guide, "Component-Level Interfaces", Section: "force:hasSObjectName". The documentation states, "Implement this interface to enable a component to be aware of the current record's object API name."

Scroll to Top