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
Robert GallahueRobert Gallahue 

Issues with keyField in Lightning DataTable

Having an issue getting the Record Ids of the selected record in Lightning Datatable. 

Here is my controller
<aura:component implements="lightning:availableForFlowScreens" access="global">
    
    <!-- attributes -->
    <aura:attribute name="dataArr" type="String[]"/>
    <aura:attribute name="data" type="Object"/>
    <aura:attribute name="columnsStr" type="String"/>
    <aura:attribute name="columns" type="List"/>
    <aura:attribute name="maxRowSelection" type="Integer" default="1"/>
    <aura:attribute name="numOfRowsSelected" type="Integer" default="0"/>
    <aura:attribute name="key" type="String" default="Id"/>
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="recordIds" type="String" />
    
    <!-- handlers-->
    <aura:handler name="init" value="{!this }" action="{! c.doInit }"/>
	
    
    
    <div style="height: 300px">
        <lightning:datatable keyField="{!v.key}"
                data="{! v.data }"
                columns="{! v.columns }"
                maxRowSelection="{! v.maxRowSelection }"
            	onrowselection="{! c.setRecordId }"
                 />
    </div>
	
</aura:component>

and here is my setRecordId function
 
setRecordId : function(component, event, helper){
      
        var selectedRows = event.getParam('selectedRows');
        var key = component.get('v.key');
        var recIds = '';
        console.log(selectedRows);
        if(selectedRows){
            if(selectedRows.length === 1){
                console.log(selectedRows.id)
                console.log(selectedRows[key])
                console.log(selectedRows[0][key])
                component.set('v.recordId', selectedRows[0][key]);
            }
            else{
                for(let i = 0; i < selectedRows.length; i++){
                    recIds += selectedRows[i][key] + ',';
                }
                component.set('v.recordIds', recIds);
                component.set('v.numOfRowsSelected', selectedRows.length);
            }
        }
    },

Var selectedRows returns the correct selected row as an object within an array but i can't seem to find the correct syntax to access that records ID for some reason. Let me know if any additional information is needed here.
appreciate the help
Best Answer chosen by Robert Gallahue
Robert GallahueRobert Gallahue
Had an error on the Flow level that was causing Id to not be included in what selectedRows was pulling

All Answers

Maharajan CMaharajan C
Please try the below controller:

setRecordId : function(component, event, helper){
      
        var selectedRows = event.getParam('selectedRows');
        var key = component.get('v.key');
        var recIds = '';
        console.log(selectedRows);
        if(selectedRows){
            if(selectedRows.length === 1){
                console.log(selectedRows[0].Id)
                component.set('v.recordId', selectedRows[i].Id);
            }
            else{
                for(let i = 0; i < selectedRows.length; i++){
                    recIds += selectedRows[i].Id + ',';
                }
                component.set('v.recordIds', recIds);
                component.set('v.numOfRowsSelected', selectedRows.length);
            }
        }
    }

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Robert GallahueRobert Gallahue
That did not work unfortunately (i used 0 instead of 'i' in the component.set as it is not in a loop
Robert GallahueRobert Gallahue
Had an error on the Flow level that was causing Id to not be included in what selectedRows was pulling
This was selected as the best answer