0

I am trying to use the below function and I have implemented hasRecordId at the component level.

handleClick: function (component, event, helper) {
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": recordID // how can I use hasRecordId here?
        });
        navEvt.fire();
    } 

Instead of mentioning the hard-coded value of ID here can someone please confirm how can I use the hasRecordID?

I have added this component in home page with lightning app builder.

  • Can you explain your use case? – sanket kumar Sep 18 '18 at 18:16
1

When you implement, hasRecordId interface, Aura creates an Attribute in your component 'recordId' automatically. And as with any other attribute in lightning development, you would need to do the following to get it in JS controller:

"recordId": component.get('v.recordId')

From the documentation

Add the force:hasRecordId interface to a Lightning component to enable the component to be assigned the ID of the current record. The current record ID is useful if the component is used on a Lightning record page, as an object-specific custom action or action override in Lightning Experience or the Salesforce app, and so on.

  • It is still not working :( getting an error "Invalid key RecordId" – gs650x Sep 18 '18 at 14:10
  • It will work if your component is being used from with a Lightning record page, i.e. Account record page, where the account name is "Acme" and its Id will be in the "recordId" attribute. – Mahmood Sep 18 '18 at 14:12
  • I am using it from the lighting app builder – gs650x Sep 18 '18 at 14:12
  • As I mentioned, it will work if are you using it in the lightning record page, as the platform picks the current record. In case of app builder, there is no current record. – Mahmood Sep 18 '18 at 14:17
  • Do we have any alternative to achieve the same from the home page? – gs650x Sep 18 '18 at 14:18
0

When you implement the force:hasRecordId interface an attribute called recordId is included automatically in your lightning component.

This is the equivalent markup that is added in the background: <aura:attribute name="recordId" type="String"/>

So in your javascript controller, you can reference this as usual: "recordId": component.get("v.recordId")

Documentation is here.

  • Hi Fred, Thank you for the response, I am using my component at home page and I have tried the above method already but still didn't get any luck, could you please suggest any other way? – gs650x Sep 18 '18 at 15:55
  • 1
    Which record Id are you expecting? – fred Sep 18 '18 at 16:00
  • I have a component which iterating the name of the Account on a page now I have added hyperlink to the name so that whenever we click on a name it will take us to the record page so I am expecting the record Id of the account name I am clicking on. – gs650x Sep 18 '18 at 16:02

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.