+ Start a Discussion
GAURAV SETHGAURAV SETH 

lightning component- get the dynamic record Id on lightning component without Apex call

I need to create a lightning component page to show Person details which is a custom object. I am able to to see the data when putting recordid value directly in lightning component but not able to get record Id value dynamically. here is my code

<aura:component implements="force:hasRecordId,force:hasSObjectName,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global">
    <aura:attribute name="recordId" type="String" access="public" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:recordViewForm recordId="{v.getRecordId}"
        objectApiName="Person__c">
    <div class="slds-grid">
        <div class="slds-col slds-size_2-of-3">
            <lightning:outputField fieldName="First_Name__c" />
            <lightning:outputField fieldName="Last_Name__c" />
        </div>
        <div class="slds-col slds-size_1-of-3">
            <lightning:outputField fieldName="Email__c" />
            <lightning:outputField fieldName="Home_Phone__c" />
        </div>
    </div>
</lightning:recordViewForm>
</aura:component>

Controller code:

doInit : function(cmp) {

    var recordId = cmp.get("v.recordId");
    var output = '{"record": "' + recordId + '"}';
    // alert('Recordid' +output);
    cmp.set("v.getRecordId", output);

It is showing record Id with above alert but component is not showing data.
What wrong I am doing ? I need to call this component from my person record page. I added this component by Edit Page.
Help is appreciated

Thanks,
Gaurav
 
Raj VakatiRaj Vakati
Use lightning data services

Use Lightning Data Service to load, create, edit, or delete a record in your component without requiring Apex code. Lightning Data Service handles sharing rules and field-level security for you. In addition to simplifying access to Salesforce data, Lightning Data Service improves performance and user interface consistency.
At the simplest level, you can think of Lightning Data Service as the Lightning components version of the Visualforce standard controller. While this statement is an over-simplification, it serves to illustrate a point. Whenever possible, use Lightning Data Service to read and modify Salesforce data in your components.
Data access with Lightning Data Service is simpler than the equivalent using a server-side Apex controller. Read-only access can be entirely declarative in your component’s markup. For code that modifies data, your component’s JavaScript controller is roughly the same amount of code, and you eliminate the Apex entirely. All your data access code is consolidated into your component, which significantly reduces complexity.
 
<aura:component implements="flexipage:availableForRecordHome, force:hasRecordId">
    <lightning:recordForm 
        recordId = "{!v.recordId}"
        objectApiName="Account"
        layoutType="Compact"
        columns="2" />
</aura:component>



https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service.htm

http://sfdcmonkey.com/2017/07/31/lightning-data-service/

https://sfcure.com/2017/09/26/lightning-component-display-records-in-a-dynamic-table-using-fieldset-on-any-object/
https://medium.com/@josef.ondrejcka/lightning-components-dynamic-design-attributes-4a988892367d

https://medium.com/salesforcesummaries/simplify-lightning-component-development-with-lightning-data-service-a89c0c4c649e
Maharajan CMaharajan C
Use the aura:if condition to load the record view form once the record id assignment is done in Init.

<aura:component implements="force:hasRecordId,force:hasSObjectName,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global">
    <aura:attribute name="recordId" type="String" access="public" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="showForm" type="boolean" default="false" /> 
    
    <aura:if isTrue="{!v.showForm}">
        <lightning:recordViewForm recordId="{v.getRecordId}"
            objectApiName="Person__c">
        <div class="slds-grid">
            <div class="slds-col slds-size_2-of-3">
                <lightning:outputField fieldName="First_Name__c" />
                <lightning:outputField fieldName="Last_Name__c" />
            </div>
            <div class="slds-col slds-size_1-of-3">
                <lightning:outputField fieldName="Email__c" />
                <lightning:outputField fieldName="Home_Phone__c" />
            </div>
        </div>
        </lightning:recordViewForm>
    </aura:if>
</aura:component>


doInit : function(cmp) {
    var recordId = cmp.get("v.recordId");
    var output = '{"record": "' + recordId + '"}';
    // alert('Recordid' +output);
    cmp.set("v.getRecordId", output);
    cmp.set("v.showForm", true);

Thanks,
Maharajan.C