
Untitled
By: a guest on
Jun 1st, 2012 | syntax:
None | size: 1.01 KB | hits: 11 | expires: Never
When looping through a set of Salesforce.com SObjects from a VisualForce page, you may want to test whether the SObject has any children without wrapping the SObjects in a wrapper class. Here's how:
Controller:
Create a property in the controller that is a map of the parent IDs and Boolean flag.
public Map<Id, Boolean> hasChildren {
get {
if (this.hasChildren == null) {
for (Parent__c parent : [select id, (select id from children__r) from parent__c]) {
if (parent.children__r.size() > 0) {
this.hasChildren.put(parent.id,true);
} else {
this.hasChildren.put(parent.id,false);
}
}
}
return this.hasChildren;
}
set {
this.hasChildren = value;
}
}
Visualforce Page:
Use dynamic bindings to check if the parent has any children while looping in Visualforce. This example renders the text if there are children.
<apex:repeat value="{!parents}" var="parent">
<apex:outputtext value="-----" rendered="{!hasChildren[parent.id]}" />
</apex:repeat>