KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Code Snippet: Identifying if a List Item is a Parent Item with Children
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: March 23, 2012

Here is a utility method that can be used to identify whether or not a list item has children elements:

// $1 = list reference
// $2 = item reference
// $0 = true if list item has children / false if no children

C_LONGINT($1;$2;$a;$vl_ItemRef;$vl_ListItems;$vl_ParentRef)
C_BOOLEAN($0)
C_TEXT($vt_ItemText)
$0:=False
If (Count parameters=2)
 If (Is a list($1))
  $vl_ListItems:=Count list items($1)
  For ($a;1;$vl_ListItems)
   GET LIST ITEM($1;$a;$vl_ItemRef;$vt_ItemText)
   $vl_ParentRef:=List item parent($1;$vl_ItemRef)
   If ($vl_ParentRef=$2)
    $0:=True
   End if
  End for
 End if
End if


The method loops over all list items in $1 and then for each list item check LIST ITEM PARENT to see if it matches the same reference as $2 and if it does it returns true in $0 otherwise it returns false in $0.

If the project method is saved as UTIL_ListItemHasChildren then it could be used like this:

if(UTIL_ListItemHasChildren(ListRef;ItemRef))
    // list item has children
else
    // list item does not have children
end if