Implementing lightning:hasPageReference
interface, we can access the recordTypeId
in lightning JS controller. lightning:hasPageReference
provides access to the pageReference attribute.
The pageReference
attribute can be populated only for the following page types:
- standard__component
- standard__navItemPage (for Lightning component tab only)
- standard__recordPage
- standard__objectPage
Example:
Lightning Component:
<aura:component implements="force:hasRecordId,lightning:actionOverride,lightning:hasPageReference"> <!--Declare Attributes--> <aura:attribute name="selectedRecordId" type="Id" /> <!--Declare Handler--> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <!--Component Start--> <div class="slds-m-around--xx-large"> Selected Record Type Id : {!v.selectedRecordId} </div> <!--Component End--> </aura:component>
Lightning JS Controller:
({ doInit: function(component, event, helper) { //get record type Id var recordTypeId = component.get("v.pageReference").state.recordTypeId; component.set("v.selectedRecordId", recordTypeId); //get action name edit/new var actionName = component.get("v.pageReference").attributes.actionName; console.log('actionName-' + actionName); //get object API name var objectApiName = component.get("v.pageReference").attributes.objectApiName; console.log('objectApiName-' + objectApiName); }, })