ABAP CDS recursive association using hierarchy

Introduction
In this article, I will show a practical example of self-association in CDS. You will learn how to define a simple hierarchy in your CDS and how to consume it in your ABAP code.
The problem
Recently I have faced a quite interesting problem. There is a concept of something like ‘frame contract’. It means that out of this document many others of the same type (or not necessarily) can be created. Let’s take a look at VBFA table (Sales Document Flow). I am interested only in subsequent Sales Orders, therefore I have filtered out only documents of category ‘C’:
What we can see is a ‘chain’ of documents, or even to be more specific – a tree. Let’s take a look at document 24. More friendly and eye-catching representation of the relationship between those documents would be:
Root parent of the above tree would be document 24 – our frame contract. Below it you will see it’s ‘children’ – subsequent documents. Now imagine that you would like to extract all documents related to your frame contract. Lot’s of twisted ABAP! Maybe there is an easier way of solving this riddle?
Gentle solutions
Yes! Fortunately, there is – we can avoid looping, reading, assigning and dumping.
CDS
We can define a CDS view which would have an association with itself. Let’s take a look:
I have defined a view Z_VBFA based on VBFA and added an association to Z_VBFA. As you can see preceding documents will be treated as parents and subsequent ones as children. The cardinality of the above association is [0..1] as there cannot be any multiple parents – one contract can only have one preceding document, however it can have multiple children. In order to allow consumption of such CDS annotation needs to be added:
It defines parent-child hierarchy and points at our self-association. There is also a second way of defining a hierarchy:
I believe that the first one is much nicer. Remember that in that approach you should not use association.
Hierarchy
You can also define a hierarchy object Z_VBFA_HIERARCHY which would look like this:
– Source – Z_VBFA – previously defined view with self-association
– Child to parent association – points at recursion association in the source
– Siblings order – order of sibling nodes
– Orphans root – orphans will be treated as roots. In our case contract 24 would be such node. Multiple parents – this syntax is not used here as multiple parents are not allowed in my case
– I have also listed out all $node fields. These are hierarchy-specific fields describing a hierarchy. We will take a look at them later. After running the hierarchy you will see a standard table view:
Gentle consumption
Now let’s take advantage of the work we have done here and let’s consume our objects in ABAP. There are few useful HIERARCHY functions – you can read more about them here https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.01/en-US/fc59f81a5c494f399cc2ff70b9c3b4c9.html.
We will Focus on three of them:
- HIERARCHY – generates a hierarchy
- HIERARCHY_DESCENDANTS – Returns all descendants of a set of start nodes in a hierarchy.
- HIERARCHY_ANCESTORS – Returns all ancestors of a set of start nodes in a hierarchy.
Let’s implement it in ABAP.
HIERARCHY
A hierarchy from CDS view with defined hierarchy will be generated.
Selected fields are hierarchy nodes that are always available and describe the structure. You need to select them explicitly and add an alias. Let’s take a look at the SOURCE:
- CHILD TO PARENT ASSOCIATION – points at recursive association in the view
- START WHERE – points at a starting node in the hierarchy
Result:
INDEX | PARENT | NODE | LEVEL | RANK | TREE_SIZE |
1 | 10,0000000024;6,000000 | 10,0000000025;6,000000 | 1 | 1 | 11 |
2 | 10,0000000025;6,000000 | 10,0000000067;6,000000 | 2 | 2 | 1 |
3 | 10,0000000025;6,000000 | 10,0000000061;6,000000 | 2 | 3 | 1 |
4 | 10,0000000025;6,000000 | 10,0000000060;6,000000 | 2 | 4 | 1 |
5 | 10,0000000025;6,000000 | 10,0000000063;6,000000 | 2 | 5 | 1 |
6 | 10,0000000025;6,000000 | 10,0000000064;6,000000 | 2 | 6 | 1 |
7 | 10,0000000025;6,000000 | 10,0000000065;6,000000 | 2 | 7 | 1 |
8 | 10,0000000025;6,000000 | 10,0000000066;6,000000 | 2 | 8 | 1 |
9 | 10,0000000025;6,000000 | 10,0000000068;6,000000 | 2 | 9 | 3 |
10 | 10,0000000068;6,000000 | 10,0000000069;6,000000 | 3 | 10 | 1 |
11 | 10,0000000068;6,000000 | 10,0000000070;6,000000 | 3 | 11 | 1 |
We get all relationships between nodes, starting from contract 24. What’s great all children are listed out in the node column, so our goal has been achieved. Let’s dig deeper into it however and take a look at hierarchy specific fields which may help us with traversing a hierarchy. Below pictures explain two of them:
HIERARCHY_DESCENDANTS
In this example however we are going to consume our CDS hierarchy directly.
Result:
INDEX | PRECEDINGDOCUMENT | PRECEDINGITEM | SUBSEQUENTDOCUMENT | SUBSEQUENTITEM | PARENTNODE | CHILDNODE | HIERARCHYISORPHAN | HIERARCHYLEVEL | HIERARCHYRANK | HIERARCHYPARENTRANK | HIERARCHYTREESIZE |
1 | 0000000024 | 0 | 0000000025 | 0 | 10,0000000024;6,000000 | 10,0000000025;6,000000 | 1 | 1 | 2 | 0 | 11 |
2 | 0000000025 | 0 | 0000000067 | 0 | 10,0000000025;6,000000 | 10,0000000067;6,000000 | 1 | 2 | 3 | 2 | 1 |
3 | 0000000025 | 0 | 0000000061 | 0 | 10,0000000025;6,000000 | 10,0000000061;6,000000 | 1 | 2 | 4 | 2 | 1 |
4 | 0000000025 | 0 | 0000000060 | 0 | 10,0000000025;6,000000 | 10,0000000060;6,000000 | 1 | 2 | 5 | 2 | 1 |
5 | 0000000025 | 0 | 0000000063 | 0 | 10,0000000025;6,000000 | 10,0000000063;6,000000 | 1 | 2 | 6 | 2 | 1 |
6 | 0000000025 | 0 | 0000000064 | 0 | 10,0000000025;6,000000 | 10,0000000064;6,000000 | 1 | 2 | 7 | 2 | 1 |
7 | 0000000025 | 0 | 0000000065 | 0 | 10,0000000025;6,000000 | 10,0000000065;6,000000 | 1 | 2 | 8 | 2 | 1 |
8 | 0000000025 | 0 | 0000000066 | 0 | 10,0000000025;6,000000 | 10,0000000066;6,000000 | 1 | 2 | 9 | 2 | 1 |
9 | 0000000025 | 0 | 0000000068 | 0 | 10,0000000025;6,000000 | 10,0000000068;6,000000 | 1 | 2 | 10 | 2 | 3 |
10 | 0000000068 | 0 | 0000000069 | 0 | 10,0000000068;6,000000 | 10,0000000069;6,000000 | 1 | 3 | 11 | 10 | 1 |
11 | 0000000068 | 0 | 0000000070 | 0 | 10,0000000068;6,000000 | 10,0000000070;6,000000 | 1 | 3 | 12 | 10 | 1 |
I used * sign in select, therefore, all fields from defined earlier CDS hierarchy were selected. Notice that in this case Z_VBFA_HIERARCHY is consumed, not Z_VBFA.
HIERARCHY_ANCESTORS
This function will return the ancestors of a given node.
Result:
INDEX | PRECEDINGDOCUMENT | PRECEDINGITEM | SUBSEQUENTDOCUMENT | SUBSEQUENTITEM | PARENTNODE | CHILDNODE | HIERARCHYISORPHAN | HIERARCHYLEVEL | HIERARCHYRANK | HIERARCHYPARENTRANK | HIERARCHYTREESIZE |
1 | 0000000024 | 0 | 0000000025 | 0 | 10,0000000024;6,000000 | 10,0000000025;6,000000 | 1 | 1 | 2 | 0 | 11 |
I have added orphans root annotation therefore one relationship in the hierarchy is displayed.
Alternative syntax
You can combine HIERARCHY and HIERARCHY_DESCENDANTS/HIERARCHY_ANCESTORS:
It will dynamically return hierarchy and apply HIERARCHY_DESCENDANTS on the result. Notice that we are not using Z_VBFA_HIERARCHY here but Z_VBFA. The result will be exactly the same as in HIERARCHY_DESCENDANTS.
Summary
I hope you can see the advantage of such solution. With few annotations and one select, we can extract the whole tree. With additional parameters like hierarchy_rank etc. it is incredibly easy to traverse through the whole structure. You can even select all other business data and have them all with one simple select. When possible avoid writing complex code!
And if you want to learn more about this missing link of your SAP S/4HANA testing strategy, make sure to visit the Int4’s course called “Avoid SAP S/4HANA Project Delays with Third-Party System Service Virtualization” available here:
Popular tags
ABAP int4 INT4 IFTT Int4Interview S/4HANA SAP AIF SAP CPI sap integration