<aura:attribute name='Id' type='String'/>
     <div class="wk-badge">
        <aura:if isTrue="{!(v.Id == 'accountBadge')}">
        <lightning:badge label="Account" id="accountBadge"/>
        <aura:if isTrue="{!(v.Id == 'contactBadge')}">
        <lightning:badge label="Contact" id="contactBadge"/>
        <aura:if isTrue="{!(v.Id == 'leadBadge')}">
        <lightning:badge label="Lead" id="leadBadge"/>

                getRecordId: function(cmp, evt, helper){
                        var getRecId = cmp.get("v.Id");
                            cmp.set("v.Id", "accountBadge");
                        }else if(getRecId.startsWith('003')){
                            cmp.set("v.Id", "contactBadge");
                        }else if(getRecId.startsWith('00Q')){
                            cmp.set("v.Id", "leadBadge");

I am unable to get the record Id. What I want is to show the badge based on the record ID.

Here is the widget


Access the Id the same way like you access the other variables like name or phone. If you need calculated fields to control the UI behaviour, calculate them when you retrieve the data from the server.

Misunderstood Question:

Have you added the force:hasRecordId Interface to your lightning component?

You need to check for the v.recordId variable, the variable you are currently checking on is the same you want to set.

How to use the Interface: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation

  • But the force:hasRecordID interface is only for specific object. As in the image it is a widget and it has several records of different objects. So, how will I get the record Id? – H John Oct 12 '20 at 10:38
  • Can you include some more context? Especially about how you access the data for the other fields? – Nina Rosen Oct 12 '20 at 10:41
  • I have widget on the Utility bar and based on the picklist values it shows the records. Picklist values are as follow: All, Account, Contact, Lead. So, when I select any of these values so it shows the record. Now these is one more requirement which is, it should show the badge to easily distinguish the records. I have tried many things but nothing works for me – H John Oct 12 '20 at 10:44
  • In my case as above picture. The first record is Account and the second one is contact and the third one is lead. so for the first it should only shows the Account badge not the others two and for the rest also – H John Oct 12 '20 at 10:46
  • You can access the ID, the same way you access the Phone number or the name. – Nina Rosen Oct 12 '20 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.