Generic hierarchies in SAP BW
At first, I thought about starting my SDN blogging career with a stupid “hello world” post. But then I decided to write something (in my opinion) more valuable.
In several customer projects, I had the requirement to build non-standard SAP hierarchies based on SAP BW data. I had to generate special organization/customer hierarchies, date hierarchies and hierarchies based on 3th party data which are stored in DSOs. All these different hierarchies had to be created dynamically because of the base data being changed frequently (daily, monthly etc.).
SAP NetWeaver BW offers several possibilities to build hierarchies:
- Hierarchies can be maintained manually. Cons: not maintainable for large, frequently changing hierarchies.
- Use the SAP business content for standard hierarchies … but we will build our own hierarchies.
- Generate flat files to load them into the hierarchy InfoObject; but then is a solid structure required: NODEID, IOBJNM, NODENAME, LINK, PARENTID. Cons: You have to care about the correct ID assignment and there are unnecessary overhead for the file creation.
Wouldn’t it be great to have a comfortable hierarchy framework with simple functions like “create node”, “add node” or “add child” to easily build a hierarchy and not having to care about the complex ID assignment and flat file creation? I think it would.
What do we need? We need two objects:
- Hierarchy object:
- knows about the corresponding hierarchy InfoObject, technical name and hierarchy description
- nodes can be added (type “node object”)
- generates and activates a SAP hierarchy
- Node object:
- knows about the corresponding node InfoObject and its nodename
- can be added to a hierarchy (of type mentioned above “Hierarchy object”) or to another node as child
Both objects take care of the ID assignment for PARENTID, CHILDID, and NEXTID and additionally assign each node to the correct level for a node. That sounds good.
There are two classes for the objects
- Hierarchy object: ZCL_GENERIC_HIERARCHY
- Node object: ZCL_HIERARCHY_NODE
The hierarchy class ZCL_GENERIC_HIERARCHY is defined as an abstract class and cannot be instantiated. Common behaviour like ADD_NODE() is implemented in the abstract class while the BUILD_HIERARCHY() method, however, must be implemented in an inherited class with its own coding.
A coding example for a dynamic hierarchy on 0CALMONTH is described in the following post: example-for-a-generic-date-hierarchy-on-0calmonth