automationvbscripthp-uftsap-gui

Expanding a SAP Tree


I'm using UFT (vbscript) to automate a process between SAP and a Web page. Using SAP transaction S_ALR_87013534, I have a piece of code in UFT that will expand the tree completely and extract the value associated with one of the order numbers:

set tree = SAPGuiSession("Session").SAPGuiWindow("Execute Drilldown Report").SAPGuiTree("TableTreeControl")
tree.OpenItemContextMenu "PRJ "&projNum,"PRJ "&projNum
tree.SelectMenuItemById "&EXPAND"
tree.SelectNode "PRJ "&projNum
colKey_plan1 = getColNameFromTitle(tree, "Plan 2--Overall")
rowContainingOrdNum = findBudget(tree, ordNum)
plannedProjectBudget = tree.Object.GetItemText(tree.Object.GetAllNodeKeys(rowContainingOrdNum(0)), colKey_plan1)

Function getColNameFromTitle(tree, title)
    set colNames = tree.Object.GetColumnNames
    For i = 1 To (colNames.length-1)
        selectedColTitle = tree.Object.GetColumnTitleFromName(colNames(i))
        If selectedColTitle = title Then
            getColNameFromTitle = colNames(i)
            Exit For
        End If
    Next
End Function

Function findBudget(tree, ordNum)
    rowContainingOrdNum = Array()
    Set columnNames = tree.Object.GetColumnNames()
    set columnKeys = tree.Object.GetColumnCol(columnNames(0))
    For i = 1 To (columnKeys.length-1)
        If InStr(columnKeys(i), ordNum)>0 Then
            AddItem rowContainingOrdNum, i
            Exit For
        End If
    Next
    findBudget = rowContainingOrdNum
End Function

Function AddItem(arr, val)
    ReDim Preserve arr(UBound(arr) + 1)
    arr(UBound(arr)) = val
    AddItem = arr
End Function

This works perfectly, but when I use a different report, S_ALR_87013543, it's still recognised as a tree but the above code doesn't work as there is no EXPAND option at the Object heading. I'm not very familiar with SAP and all their trees and how to use automation with it, so any guidance or tips are appreciated.

Left is the report I need to expand, and right is the report the code works with:

enter image description here enter image description here


Solution

  • Solution:

       Set TreeObj = SAPGuiSession("Session").SAPGuiWindow("Actual/Plan/Variance").SAPGuiTree("TableTreeControl").Object
        Set AllValues = TreeObj.GetAllNodeKeys
        Count = AllValues.Count
        Found = 0
        For i = 0 to Count-1
            NodeText = TreeObj.GetNodeTextByKey(AllValues(i))
            If NodeText = WBSelement Then
                Found = 1
                Exit For
            End if
        Next
        If Found = 1 Then
             SAPGuiSession("Session").SAPGuiWindow("Actual/Plan/Variance").SAPGuiTree("TableTreeControl").SelectNode WBS
        End If