function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pooja NekkalapudiPooja Nekkalapudi 

bring the record id of the page I am on into my lightning component

Im trying to create a related list lightning component that does inline editing .. everything so far works fine if I pass the Parent id .. I would like it to dynamically pull the parent id depending on the record im on .. 

Apex controller : Project__c should be dynamically pulled depending on the record im on ( project is parent Historical p&l is child)
public class DataTableApexClass {
    @AuraEnabled
  
  public static List<Historical_Projected_P_L__c> getHPPL(){
 
      //  String url =  ApexPages.currentPage().getUrl();
     //  string str = url.right(23).substring(0,15);
    List <Historical_Projected_P_L__c> returnList = new List<Historical_Projected_P_L__c>();
        returnList = [select id, Project__c,Closed_Deal_Data__c,Label2__c, FY0__c,FY1__c,FY_3__c,FY_1__c,LTM__c,FY_2__c,FY2__c,FY3__c from Historical_Projected_P_L__c  where Project__c='a0w0d000003yohCAAQ' ];
        //Project__c= :currentRecordId];   
        return returnList;  
    } 
    @AuraEnabled
    public static void updateHPPL(List<Historical_Projected_P_L__c> HPPL ){
        update HPPL;
    }}

Lightning component :
<aura:component controller="DataTableApexClass"  implements="flexipage:availableForAllPageTypes,force:appHostable,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global">
    
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="errors" type="Object" default="[]"/>
    <aura:attribute name="draftValues" type="Object" default="[]"/>
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    <b>this is recordID {!v.recordID}</b><br/>
    <div class="slds-p-around_medium">
        <h1 class="slds-text-heading_large">Inline edit Data table</h1>
    </div>
    
    <!-- the container element determine the height of the datatable -->
    <div style="height: 300px">
        <lightning:datatable aura:id="dtTable"
                             columns="{! v.columns }"
                             data="{! v.data }"
                             strRecordId ="{! v.strRecordId}"
                             keyField="Id"
                             errors="{! v.errors }"
                             draftValues="{! v.draftValues }"
                             onsave="{! c.handleSaveEdition }"
                             />
    </div>
    
    
</aura:component>

Lightning controller : ({
    init: function (cmp, event, helper) {
        cmp.set('v.columns', [
            {label: 'Index', fieldName: 'Index__c', type: 'Number' , editable: true},        
            {label: 'Project', fieldName: 'Project__c', type: 'text' ,editable: false},
            {label: 'Financial Metrics', fieldName: 'Label2__c', type: 'text' ,editable: false},
            {label: 'FY-3', fieldName: 'FY_3__c', type: 'number' ,editable: true},
            {label: 'FY-2', fieldName: 'FY_2__c', type: 'number' ,editable: true},
            {label: 'FY-1', fieldName: 'FY_1__c', type: 'number' ,editable: true},
            {label: 'FY3', fieldName: 'FY3__c', type: 'number' ,editable: true},
            {label: 'FY2', fieldName: 'FY2__c', type: 'number' ,editable: true},
            {label: 'FY1', fieldName: 'FY1__c', type: 'number' ,editable: true}
        ]);
        helper.fetchData(cmp,event, helper);
    },
    handleSaveEdition: function (cmp, event, helper) {
        var draftValues = event.getParam('draftValues');
        console.log(draftValues);
        var action = cmp.get("c.updateHPPL");
        action.setParams({"HPPL" : draftValues});
        action.setCallback(this, function(response) {
            var state = response.getState();
            $A.get('e.force:refreshView').fire();        
        });
        $A.enqueueAction(action);     
    },
})


helper: 
({
    fetchData: function (cmp,event,helper) {
        var action = cmp.get("c.getHPPL");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var data = response.getReturnValue();
                cmp.set('v.data',data);
            }
            // error handling when state is "INCOMPLETE" or "ERROR"
        });
        $A.enqueueAction(action);
    }
})


ANy help will be appreciated 
 
Best Answer chosen by Pooja Nekkalapudi
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Pooja,
You can use force:hasRecordId interface and recordId attribute here.In lightning components  force:hasRecordId interface  adds an attribute named recordId to your component. 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 current record being viewed.
I have done few changes in your code.Try this code.Kingly modify the code according to your requirements.
public class DataTableApexClass {
    @AuraEnabled
  
  public static List<Historical_Projected_P_L__c> getHPPL(string currentRecordId){
 
      //  String url =  ApexPages.currentPage().getUrl();
     //  string str = url.right(23).substring(0,15);
    List <Historical_Projected_P_L__c> returnList = new List<Historical_Projected_P_L__c>();
        returnList = [select id, Project__c,Closed_Deal_Data__c,Label2__c, FY0__c,FY1__c,FY_3__c,FY_1__c,LTM__c,FY_2__c,FY2__c,FY3__c from Historical_Projected_P_L__c  where Project__c= :currentRecordId]; 
        //Project__c= :currentRecordId];   
        return returnList;  
    } 
    @AuraEnabled
    public static void updateHPPL(List<Historical_Projected_P_L__c> HPPL ){
        update HPPL;
    }}

Lightning component :
<aura:component controller="DataTableApexClass"  implements="flexipage:availableForAllPageTypes,force:appHostable,flexipage:availableForRecordHome,force:hasRecordId,force:hasSObjectName" access="global">
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="errors" type="Object" default="[]"/>
    <aura:attribute name="draftValues" type="Object" default="[]"/>
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    <b>this is recordID {!v.recordID}</b><br/>
    <div class="slds-p-around_medium">
        <h1 class="slds-text-heading_large">Inline edit Data table</h1>
    </div>
    
    <!-- the container element determine the height of the datatable -->
    <div style="height: 300px">
        <lightning:datatable aura:id="dtTable"
                             columns="{! v.columns }"
                             data="{! v.data }"
                             strRecordId ="{! v.strRecordId}"
                             keyField="Id"
                             errors="{! v.errors }"
                             draftValues="{! v.draftValues }"
                             onsave="{! c.handleSaveEdition }"
                             />
    </div>
    
    
</aura:component>

Lightning controller : ({
    init: function (cmp, event, helper) {
        cmp.set('v.columns', [
            {label: 'Index', fieldName: 'Index__c', type: 'Number' , editable: true},        
            {label: 'Project', fieldName: 'Project__c', type: 'text' ,editable: false},
            {label: 'Financial Metrics', fieldName: 'Label2__c', type: 'text' ,editable: false},
            {label: 'FY-3', fieldName: 'FY_3__c', type: 'number' ,editable: true},
            {label: 'FY-2', fieldName: 'FY_2__c', type: 'number' ,editable: true},
            {label: 'FY-1', fieldName: 'FY_1__c', type: 'number' ,editable: true},
            {label: 'FY3', fieldName: 'FY3__c', type: 'number' ,editable: true},
            {label: 'FY2', fieldName: 'FY2__c', type: 'number' ,editable: true},
            {label: 'FY1', fieldName: 'FY1__c', type: 'number' ,editable: true}
        ]);
        helper.fetchData(cmp,event, helper);
    },
    handleSaveEdition: function (cmp, event, helper) {
        var draftValues = event.getParam('draftValues');
        console.log(draftValues);
        var action = cmp.get("c.updateHPPL");
        action.setParams({"HPPL" : draftValues});
        action.setCallback(this, function(response) {
            var state = response.getState();
            $A.get('e.force:refreshView').fire();        
        });
        $A.enqueueAction(action);     
    },
})


helper: 
({
    fetchData: function (cmp,event,helper) {
        var action = cmp.get("c.getHPPL");
        action.setParams({"currentRecordId":cmp.get("v.recordId"));
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var data = response.getReturnValue();
                cmp.set('v.data',data);
            }
            // error handling when state is "INCOMPLETE" or "ERROR"
        });
        $A.enqueueAction(action);
    }
})

Please refer below link which might help you further
https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation
https://www.sfdc-lightning.com/2019/05/forcehasrecordid-and.html

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards