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
DoondiDoondi 

Query to get related records ?

Hi,
I am trying to build a vf page to display related list of records of a custom object
Here is my class and page:
 

public class DeleteFlag
{
	public List<Unit__c> Records {get; set;}
    public DeleteFlag(ApexPages.StandardController controller)
        {
            Records =[Select Name, from Unit__c WHERE Name='a013B000003i1vZ'];
            
        }

<apex:page standardcontroller="Property__c" extensions="DeleteFlag">
    <apex:pageBlock mode="details" >    
        <apex:form >
                <apex:pageBlockTable value="{!Records}" var="index" >    
                    <apex:column value="{!index.Name}"/>
                    <apex:column value="{!index.Property_Unit__c}"/>
                 </apex:pageBlockTable>
        </apex:form>
    </apex:pageBlock>
</apex:page>
In the above code, I am hard coding parent values, 

how to equal it to ID ( Unit__c has Look up to property__c)
Raj VakatiRaj Vakati
public class DeleteFlag
{
public List<Unit__c> Records {get; set;}
public DeleteFlag(ApexPages.StandardController controller)
	{
		Records =[Select Name,(Select id ,Name from property__r) from Unit__c WHERE Name='a013B000003i1vZ'];
		
	}
	}
	
	
	<apex:page standardcontroller="Property__c" extensions="DeleteFlag">
<apex:pageBlock mode="details" >    
	<apex:form >
			<apex:repeate value="{!Records}" var="index" >    
				<apex:column value="{!index.Name}"/>
				<apex:column value="{!index.Property_Unit__c}"/>
				<apex:repeate value="{!index.property__r}" var="index1" > 
				<apex:column value="{!index1.Name}"/>
				</apex:repeate/>
				
			 </apex:repeate>
	</apex:form>
</apex:pageBlock>
</apex:page>

 
PawanKumarPawanKumar
you should try query like below then it works.

Select Name from Unit__c WHERE property__c='a013B000003i1vZ'

I am assuming that lookup API name at Unit__c object is property__c.

Please let me know if it helps.

Regards,
Pawan Kumar
DoondiDoondi
@Raj v : when tried your code, it says Columns should be child of PageblockTable or dataTable, after doing changes, saved successfully but there was no records on vf page.

@Pawan Kumar: I don't want to hard code the value of ID, 
PawanKumarPawanKumar
In that case, you can change your query as below. This way you can make it dynamic.

Select Name from Unit__c WHERE property__c=:ApexPages.currentPage().getParameters().get('id')

and when are you calling you VF pass in the URL at end ?id=a013B000003i1vZ

Regards,
Pawan Kumar