Traverse Tree Hierarchy Example

The following is an example to transverse a tree hierarchy and write the information to a file.

Local ApiObject &TreeObj, &RootNodeObj; 
Local number &Res; 
Local string &sSetId, &sSetCntrlValue, &sTreeName, &sBranchName; 
Local File &MyFile; 
 
Function ProcessNodeChildren(&ParentNodeObj As ApiObject) 
    
   Local ApiObject &ChildNodeObj, &ChildLeafObj; 
   Local boolean &First; 
    
   If &ParentNodeObj.HasChildren Then 
       
      &ChildLeafObj = &ParentNodeObj.FirstChildLeaf; 
      While All(&ChildLeafObj) 
         &MyFile.WriteLine("Process Leaf Range From : " | &ChildLeafObj.RangeFrom); 
         &ChildLeafObj = &ChildLeafObj.NextSib; 
      End-While; 
       
      If &ParentNodeObj.HasChildNodes Then 
         &First = True; 
         &ChildNodeObj = &ParentNodeObj.FirstChildNode; 
         While &First Or 
               &ChildNodeObj.HasNextSib 
             
            If &First Then 
               &First = False; 
            Else 
               &ChildNodeObj = &ChildNodeObj.NextSib; 
            End-If; 
            &MyFile.WriteLine("Process Node : " | &ChildNodeObj.Name); 
            ProcessNodeChildren(&ChildNodeObj); 
             
         End-While; 
      End-If; 
   End-If; 
End-Function; 
 
&TreeObj = %Session.GetTree(); 

If None(&TreeObj) then
   Error("Cannot get a tree");
End-if;

&sSetId = ""; 
&sSetCntrlValue = ""; 
&sTreeName = "QE_JOBCODES"; 
&TreeEffDt = Date("1999-01-01"); 
&sBranchName = ""; 
 
&Res = &TreeObj.OPEN(&sSetId, &sSetCntrlValue, &sTreeName, &TreeEffDt, &sBranchName, False); 
If All(&Res) Then 
   Error("Cannot open a tree");
End-If; 
 
&MyFile = GetFile("TreeDump.txt", "A"); 
&RootNodeObj = &TreeObj.FindRoot(); 

&MyFile.WriteLine("Process Root Node : " | &RootNodeObj.Name);

ProcessNodeChildren(&RootNodeObj); 
&MyFile.Close();