Activity Forums Salesforce® Discussions What is force:hasRecordId in sfdc?

  • Nikita

    Member
    August 22, 2019 at 10:59 am

    Hi Saddam,

    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.

    The force:hasRecordId interface does two things to a component that implements it.

    1. It adds an attribute named recordId to your component. This attribute is of type String, and its value is an 18-character Salesforce record ID, for example: 001xx000003DGSWAA4.If you added it yourself, the attribute definition would look like the following markup:
      •  <aura:attribute name="recordId" type="String" />
      • Note If your component implements force:hasRecordId, you don’t need to add a recordId attribute to the component yourself. If you do add it, don’t change the access level or type of the attribute or the component will cause a runtime error.
    2. When your component is invoked in a record context in Lightning Experience or the Salesforce app, the recordId is set to the ID of the record being viewed.
  • Deepak

    Member
    August 22, 2019 at 12:41 pm

    This interface is used to indicate that a component takes a record (SObject) as an attribute.

    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. This interface has no effect except when used within Lightning Experience, the Salesforce mobile app, and template-based communities.

    This interface is a marker interface. A marker interface is a signal to the component’s container to add the interface’s behavior to the component. You don’t need to implement any specific methods or attributes in your component, you simply add the interface name to the component’s implements attribute.

    The force:hasRecordId interface does two things to a component that implements it.

    It adds an attribute named recordId to your component. This attribute is of type String, and its value is an 18-character Salesforce record ID, for example: 001xx000003DGSWAA4. If you added it yourself, the attribute definition would look like the following markup:<aura:attribute name="recordId" type="String" />
    Note If your component implements force:hasRecordId, you don’t need to add a recordId attribute to the component yourself. If you do add it, don’t change the access level or type of the attribute or the component will cause a runtime error.
    When your component is invoked in a record context in Lightning Experience or the Salesforce app, the recordId is set to the ID of the record being viewed.
    Important The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home. In all other cases, such as when you invoke the component as a global action, or create the component programmatically inside another component,recordId isn’t set, and your component shouldn’t depend on it.These unsupported contexts include a few contexts that might seem like they should have access to the current record. Examples of these other contexts include the following:

    Invoking the component from a global action (even when you’re on a record page)
    Invoking the component from header or footer navigation in a community (even if the page shows a record)
    force:hasRecordId and force:hasSObjectName are unsupported in these contexts. While the marker interfaces still add the relevant attribute to the component, accessing either attribute generally returns nullor undefined.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos