Saturday, May 18, 2019

force:hasRecordId and flexipage:availableForAllPageTypes interface

By using force:hasRecordId interface in lightning component we can assigned the id of the current record to lightning component. This interface adds an attribute named "recordId" to the component. The type of attribute is string and has 18-character Salesforce record ID.

Sample example:

<aura:component implements="force:hasRecordId">


Now let us understand the use of force:hasRecordId with a real example.

Scenario: We are having a requirement to provide the mechanism for the insertion of the contact record from the account detail page.


1) Component: 

We have implemented interface force:hasRecordId to obtain accountid using recordId attribute from account detail page and to make our component available for record page and any other type of page, we need to implement the interface flexipage:availableForAllPageTypes interface.

<aura:component controller="ContactInsertClass" implements="force:hasRecordId,flexipage:availableForAllPageTypes"> 
    <aura:attribute name="firstName" type="string"/>
    <aura:attribute name="lastName" type="string"/>
    <lightning:input label="Enter First Name" value="{!v.firstName}"/>
    <lightning:input label="Enter Last Name" value="{!v.lastName}"/>
    <lightning:button label="Add contact" onclick="{!c.addContact}"/>

2) Javascript controller:


 addContact : function(component, event, helper) {


        var fName=component.get("v.firstName");

        var lName=component.get("v.lastName");

        var action=component.get('c.insertContact');  // Calling Apex Controller method



            parentAccountId:component.get("v.recordId"),  // Passing parameter






            var state=response.getState();



                alert('Contact inserted successfully');              






3) Apex controller:

public class ContactInsertClass {
public static contact insertContact(string parentAccountId,string firstName1,string lastName1)
    contact con=new contact();
    insert con;
    return con;


Image from Account record page:

force:hasRecordId and flexipage:availableForAllPageTypes interface

No comments:

Post a Comment