+ Start a Discussion

Filtering a display of related objects in a VF page

Have the sample code, but I am having issues in modifying it in 2 ways -

- I want to display a custom object, but am having trouble replacing the contacts

- I want to only display records with a certain field value (Product = United)

any help with ficing the output and filtering based on field value much appreciated


<apex:page standardController="Account" >
<apex:pageBlock >

<apex:form >

<apex:pageBlockTable value="{!account.Contracts}" var="Contract">

<apex:column headervalue="Contract Number">
<apex:outputLink value="https://na2.salesforce.com/{!Contract.id}"  target="_top" id="theLink">{!Contract.contractNumber}</apex:outputLink>

<apex:column value="{!Contract.Contract_Type__c}"/>

<apex:column value="{!Contract.status}"/>
<apex:column value="{!Contract.ownerid}"/>    
<apex:column value="{!Contract.ActivatedDate}"/>










Presumably the custom object is a related list on the account object?  That being the case you should be able to use the __r notation.  If your custom object has an API of My_Custom__c, then the related list default name will be My_Customs__r.


You should then be able to apply the filter to the rendered attribute of the apex:column.


So something like the following should do the trick:


<apex:page standardController="Account" >
<apex:pageBlock >

<apex:form >

<apex:pageBlockTable value="{!account.My_Customs__r}" var="custom">

<apex:column headervalue="Custom Number" rendered="{!custom.Product__c=='United'}">
<apex:outputLink value="https://na2.salesforce.com/{!custom.id}"  target="_top" id="theLink">{!Contract.contractNumber}</apex:outputLink>

... more columns here ...
</apex:pageBlockTable> </apex:form> </apex:pageBlock> </apex:page>

 If the product__c field is a picklist, you'll need to use the TEXT function to be able to compare it to a literal value.


I am getting an error 

<apex:page standardController="Account" ><apex:pageBlock >
<apex:form >
<apex:pageBlockTable value="{!account.Custom_ObjectName__r}" var="Ecv">
<apex:column headervalue="Custom_ObjectName__c Number"><apex:outputLink value="https://na2.salesforce.com/{!Custom_ObjectName__r.id}"  target="_top" id="theLink">{!Custom_ObjectName__r.Name}</apex:outputLink></apex:column>


Error: Invalid field Custom_ObjectName__r for SObject Account


You have to replace that with the relationship api name for your object model.


Yes of course, I had the real object name in my code, just redacted it for the forum. 


That means that you haven't used the correct name - does that relationship exist?


Yes, whoops, error in the plural nature of the related objects.

Can you assist with filtering so it returns objects of only type based on a field value?


Whoops just missed the filtering via render in the code above.

got it to save but it's not filtering




<apex:pageBlockTable value="{!account.Ecommerce_Visits__r}" var="ECV">

<apex:column headervalue="ECV Number" rendered="{!ECV.Department__c=='Ecommerce'}" >
<apex:outputLink value="https://na2.salesforce.com/{!ECV.id}"  target="_top" id="theLink">{!ECV.name}</apex:outputLink>
<apex:column value="{!ECV.name}"/><apex:column value="{!ECV.Visit_Date__c}"/>
<apex:column value="{!ECV.ownerid}"/>  (etc)

 still renders info where the department equals things other than the example.




You'd need to have the rendered condition on each of the columns.  However, if the first element in the list doesn't match the filter condition, you won't get any headings either I've just found out.  


I'm sure I've got something like this working before, but I think I may have been using vanilla HTML tables and this had far more control over the layout.


Thanks for you help so far. The filtering of the list is key to the whole point of the VF page.

If anyone else has some ideas or can point me to instances it would be much appreciated.