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
Russell Farmer 9Russell Farmer 9 

Display values from a sub query (array) in a datatable

Hi have a lightning component datatable that is populated by a query with a subquery in it. I need the second column to be poulated with a value from the subquery but I can't seem to figure out how to set the field name for the the subquery field. 

Screen shot: (RPF Con should map to sub query field.
User-added imageApex Class
public class RFPOppListController {

        @AuraEnabled
    public static List <Opportunity> fetchOpps (String oppId) {
        //Qyery 10 
        List<Opportunity> oppList = [SELECT Opportunity.Name, (select Contact__r.Name from Opportunity_Contact_Roles__r where Contact_roles__c = 'RFP consultant') FROM Opportunity];
        //return lis
        return oppList;
    }
}

Component:
<aura:component controller="RFPOppListController" access="global" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute type="Opportunity[]" name="oppList"/>
    <aura:attribute name="mycolumns" type="List"/>
    <aura:attribute name="updatedRecord" type="Object[]" />


     
    <aura:handler name="init" value="{!this}" action="{!c.fetchOPPS}"/>
     
    <lightning:datatable aura:id="oppsDataTable"
                         data="{! v.oppList }"
                         columns="{! v.mycolumns }"
                         keyField="Id"
                         hideCheckboxColumn="true"
                         onsave ="{!c.onSave}"
                         />
     
</aura:component>

Controller
({
    fetchOPPS : function(component, event, helper) {
        helper.fetchOPPSHelper(component, event, helper);    

    }
})
Helper
({
    fetchOPPSHelper : function(component, event, helper) {
        component.set('v.mycolumns', [
            {label: 'Opp Name', fieldName: 'Name', editable:'true', sortable:'true', type: 'text'},          
            {label: 'RFP Con', fieldName: 'Opportunity_Contact_Roles__r.Contact__r.Name', editable:'true', sortable:'true', type: 'date'}   
            ]);
        debugger;
        var action = component.get("c.fetchOpps");      
        var operID = component.get("v.recordId");
        console.log("operID" + operID);
        action.setParams({
            oppId:operID
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.oppList", response.getReturnValue());
            }
        }); 
        $A.enqueueAction(action);
    }
})


BTW, here is a debugger screenshot of of the value I am trying to display:
User-added image
Best Answer chosen by Russell Farmer 9
Raj VakatiRaj Vakati
If you want to display from the sub query you can do it like this .. 

Query the SubQuery data along with parent object and show it like below 

 
public class RFPOppListController {

        @AuraEnabled
    public static List <Contact_roles__c> fetchOpps (String oppId) {
        //Qyery 10 
        List<Contact_roles__c> oppList = [select Contact__r.Name from Opportunity_Contact_Roles__c where Contact_roles__c = 'RFP consultant' AND OpprtunityId = 'YOUROPPID' ];
        //return lis
        return oppList;
    }
}

 

All Answers

Raj VakatiRaj Vakati
If you want to display from the sub query you can do it like this .. 

Query the SubQuery data along with parent object and show it like below 

 
public class RFPOppListController {

        @AuraEnabled
    public static List <Contact_roles__c> fetchOpps (String oppId) {
        //Qyery 10 
        List<Contact_roles__c> oppList = [select Contact__r.Name from Opportunity_Contact_Roles__c where Contact_roles__c = 'RFP consultant' AND OpprtunityId = 'YOUROPPID' ];
        //return lis
        return oppList;
    }
}

 
This was selected as the best answer
Emily Johnson 23Emily Johnson 23
I have the same question but don't really understand the solution that was provided. So, do replace your previous query and subquery with the query provided in the answer? If so, what additional modifications would be required to the helper or controller.js?
Dhanalakshmi Vellachamy 3Dhanalakshmi Vellachamy 3
@Emily Johnson 23 Can you please shre if you were able to achieve this requirement. Can you please share your solution even I am not able to follow the solution 
Emily Johnson 23Emily Johnson 23
You know, I honestly can't remember what I was working on. I'm sorry. :(