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
Admin User 10568Admin User 10568 

Apex Class Child to Parent SOQL for Page Block Table

Hi, 

Hoping someone could help me out.

I have the following apex class: 

public class PriceListAscending{
private String sortOrder = 'REPRO__Project_Name__c';
public list<REPRO__Project__c> getEvents() {
    list<REPRO__Project__c> results = Database.query(
        ' SELECT REPRO__Project_Name__c, REPRO__Properties__r.REPRO__Property__c, REPRO__Properties__r.REPRO__City__c, REPRO__Properties__r.Property_Level__c, REPRO__Properties__r.Price_List_Property_Name__c, REPRO__Properties__r.REPRO__Type__c,REPRO__Properties__r.REPRO__Bdr__c,REPRO__Properties__r.REPRO__Bth__c,REPRO__Properties__r.REPRO__Study__c,REPRO__Properties__r.REPRO__Internal_Size__c,REPRO__Properties__r.REPRO__External_Size__c, REPRO__Properties_r.REPRO__Car_c, REPRO__Properties__r.REPRO__List_Price__c, REPRO__Properties__r.REPRO__Status_c ' + 
        ' FROM REPRO__Project__c'  + 
        ' ORDER BY ' + sortOrder + ' ASC '
    );
    return results;
}
}

There is no error displayed in the editor. 

When I try to call the fields from a pageblocktable for my visual page, I get the following error: Invalid field Property_Level__c for SObject REPRO__Project__c

Here is an example of the call: 

<apex:pageBlockTable value="{!Events}" var="e" align="center" cellpadding="2" border="4"  style=" font-weight: bold; text-align: center" >
  <apex:column value="{!e.Property_Level__c}" style="padding: 10px;order-bottom: 1px solid #ddd;border: 1px solid black; text-align: center; " />

Any help would be greatly appreciated.
 
Best Answer chosen by Admin User 10568
AnudeepAnudeep (Salesforce Developers) 
This is a child to parent SOQL. Can you please check if you have a field named Property_Level__c on the object REPRO__Properties__c
 
REPRO__Properties__r.Property_Level__c

See example below

Traverse multiple level upwards

There is a custom lookup field on contact object called Best Friend and there is a field called likes ice cream. Write a query to retrieve the custom field
 
SELECT Id, Best_Friend__r.Likes_Ice_Cream__c FROM contact

Here we’re traversing a custom lookup field Best_Friend__c on the contact object. Notice how the “__c” changes to a “__r” when traversing that field!
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
This is a child to parent SOQL. Can you please check if you have a field named Property_Level__c on the object REPRO__Properties__c
 
REPRO__Properties__r.Property_Level__c

See example below

Traverse multiple level upwards

There is a custom lookup field on contact object called Best Friend and there is a field called likes ice cream. Write a query to retrieve the custom field
 
SELECT Id, Best_Friend__r.Likes_Ice_Cream__c FROM contact

Here we’re traversing a custom lookup field Best_Friend__c on the contact object. Notice how the “__c” changes to a “__r” when traversing that field!
 
This was selected as the best answer
Admin User 10568Admin User 10568
Hi @Anudeep, 

Thanks for your help. Much appreciated.

Yes there is: REPRO__Properties__r.Property_Level__c

Since posting my earlier question. I tested the following in the Query Editor: 

SELECT (select Property_Level__c from REPRO__Properties__r) FROM REPRO__Project__c

This returned the expected results. 

But the following:

Select REPRO__Properties__r.Property_Level__c from REPRO__Project__c

No dice.

Any thoughts on why my dot notation hasn't traversed correctly here?