function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael Kolodner 13Michael Kolodner 13 

How the heck do I use force:hasRecordID?

I'm trying to build a Lightning Component that I think should be very very simple. I understand that force:hasRecordID should allow a component that's on a Lightning Record Page to know the ID of the record being displayed. I, therefore, would like to take that ability to make a simple component that would show that record ID. (And before you ask, "Why not do it as a formula field?" The answer is that to do that I'd have to build a formula field on every object, whereas this component would be object agnostic. Plus it's a chance for me to learn a little about components...) I thought I could just use the string {!v.recordID}, but clearly I have to take some action to actually get the ID and put it into a variable. I don't know Javascript, so I'm stumped. But I know enough to know that someone who actually knew what they were doing could probably thew this together in seconds.

As I understand I, I think I should need only two things:

1. The component (.cmp)
The code I've got so far is:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="recordID" type="String" default="ID hasn't been set yet!"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <b>This is recordID {!v.recordID} </b>

2. The controller (.js)
The code I've got for that so far is below. But I've been trying all sorts of things based on Googling and looking on Trailheads and the like, so this part could be way off:
    doInit : function(cmp) {
        var action = cmp.get("v.recordId");
            var state = response.getState();
            if(state === "SUCCESS"){
                cmp.set("v.recordID", response.getReturnValue());

Right now when I put the component on a page it just displays the text, but the string {!v.recordID} is replaced with the default text (or, when I didn't have that, just with nothing).

Help, please?
Alain CabonAlain Cabon
  1. Javascrpt is case-sensitive.
  2. <aura:attribute name="recordId" type="String"/> is always mandatory in the component.
  3. You cannot use: aura:attribute name="recordId" and aura:attribute name="recordID" in the same component (internal constraint).
  4. but you can use:  aura:attribute name="myRecordID" instead.
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global" >
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="myRecordID" type="String" default="ID hasn't been set yet!"/>
    <aura:attribute name="sObjectName" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <b>This is recordID {!v.recordId} </b><br/>
    <b>This is myRecordID {!v.myRecordID} </b><br/>
    <b>This is SObjectName {!v.sObjectName} </b><br/>
    doInit : function(cmp) { 
        var toastEvent = $A.get("e.force:showToast");        
            "title": "Success!",  
            "message": "doInit executed."
        }); ;     

User-added image

The toast is just a nice message in Lex.

Michael Kolodner 13Michael Kolodner 13
Thank you, Alain!