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
Tom DawsonTom Dawson 

Accessing attributes through related object

Hi All

I'm trying to access field values from an object related to the initial one in the call. Our relationship is as follows:

Contact --> Post Fulfillment --> Post

I am trying to use an iteration to get values from Post when the initial call is to Contact. See code below:

Apex:
@AuraEnabled
    public static List<Contact> getHSList(){
        List<Contact> myHSList = new List<Contact>();
        myHSList = [Select Id, 
                    Name, 
                    Fire_Warden__c,
                    Phone,
                    (SELECT id, Post__c,Post__r.Area__c, Post__r.Floor__c, Post__r.Section__c
                     FROM Contact.Post_Fulfillments__r)
                    From Contact
                    Where Fire_Warden__c = true];
        return myHSList;
    }
Controller
({
    onInit : function(component, event, helper) {
        
        console.log('onInit');
        
        var action = component.get("c.getHSList");
        
        action.setCallback(this, function(a) {
            
            var HandSData = a.getReturnValue();
            console.log(HandSData);
            component.set("v.contactList",HandSData);
            console.log(HandSData.length);
            
        });
        $A.enqueueAction(action);
    }
    
})
Component
<aura:Iteration items="{!v.contactList}" var="con" >
                <tr class="slds-hint-parent">
                    <th data-label="Name" scope="row">
                        <div class="slds-truncate" title="Cloudhub">{!con.Name}</div>
                    </th>
                    <td data-label="Phone">
                        <div class="slds-truncate" title="Cloudhub">{!con.Phone}</div>
                    </td>
                    <td data-label="Close Date">
                        <div class="slds-truncate" title="4/14/2015">{!con.Post_Fulfillments__r.Post__r.Floor__c} place</div>
                    </td>
                </tr>
            </aura:Iteration>

I am able to access both Name and Phone from the contact. I can also see from the Chrome inspector that the field values for Location are coming through, I just cannot access them. 

As you can see i have tried 
{!con.Post_Fulfillments__r.Post__r.Floor__c}
But it doesnt seem to work.

Any help would be great.

Thanks

Tom 


 
Best Answer chosen by Tom Dawson
Soyab HussainSoyab Hussain
Hi Tom Dawson,

We you can't do this, because con.Post_Fulfillments__r this one is an another list.

Incorrect
{!con.Post_Fulfillments__r.Post__r.Floor__c}

Correct
{!con.Post_Fulfillments__r[0].Post__r.Floor__c}

Or we can use another <aura:Iteration> inside the <aura:Iteration> like this.
<aura:Iteration items="{!v.contactList}" var="con" >
                <tr class="slds-hint-parent">
                    <th data-label="Name" scope="row">
                        <div class="slds-truncate" title="Cloudhub">{!con.Name}</div>
                    </th>
                    <td data-label="Phone">
                        <div class="slds-truncate" title="Cloudhub">{!con.Phone}</div>
                    </td>
                    <td data-label="Close Date">
                        <div class="slds-truncate" title="4/14/2015">
                             <aura:Iteration items="{!con.Post_Fulfillments__r}" var="post" >
                                    {!post.Post__r.Floor__c} 
                            </aura:Iteration> place</div>
                    </td>
                </tr>
            </aura:Iteration>



Regards,
Soyab
 

All Answers

Soyab HussainSoyab Hussain
Hi Tom Dawson,

We you can't do this, because con.Post_Fulfillments__r this one is an another list.

Incorrect
{!con.Post_Fulfillments__r.Post__r.Floor__c}

Correct
{!con.Post_Fulfillments__r[0].Post__r.Floor__c}

Or we can use another <aura:Iteration> inside the <aura:Iteration> like this.
<aura:Iteration items="{!v.contactList}" var="con" >
                <tr class="slds-hint-parent">
                    <th data-label="Name" scope="row">
                        <div class="slds-truncate" title="Cloudhub">{!con.Name}</div>
                    </th>
                    <td data-label="Phone">
                        <div class="slds-truncate" title="Cloudhub">{!con.Phone}</div>
                    </td>
                    <td data-label="Close Date">
                        <div class="slds-truncate" title="4/14/2015">
                             <aura:Iteration items="{!con.Post_Fulfillments__r}" var="post" >
                                    {!post.Post__r.Floor__c} 
                            </aura:Iteration> place</div>
                    </td>
                </tr>
            </aura:Iteration>



Regards,
Soyab
 
This was selected as the best answer
Tom DawsonTom Dawson
Hi Soyab

Fantastic - working great now, thanks!!

Tom