Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 1st, 2012  |  syntax: None  |  size: 1.01 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. 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:
  2.  
  3. Controller:
  4. Create a property in the controller that is a map of the parent IDs and Boolean flag.
  5.  
  6. public Map<Id, Boolean> hasChildren {
  7.   get {
  8.     if (this.hasChildren == null) {
  9.       for (Parent__c parent : [select id, (select id from children__r) from parent__c]) {
  10.         if (parent.children__r.size() > 0) {
  11.           this.hasChildren.put(parent.id,true);
  12.         } else {
  13.           this.hasChildren.put(parent.id,false);
  14.         }
  15.       }
  16.     }
  17.     return this.hasChildren;
  18.   }
  19.   set {
  20.     this.hasChildren = value;
  21.   }
  22. }
  23.  
  24. Visualforce Page:
  25. Use dynamic bindings to check if the parent has any children while looping in Visualforce. This example renders the text if there are children.
  26.  
  27. <apex:repeat value="{!parents}" var="parent">
  28.   <apex:outputtext value="-----" rendered="{!hasChildren[parent.id]}" />
  29. </apex:repeat>