Skip to Content

SAP CDS how to add Hierarchy annotations sap:hierarchy-node-for sap:hierarchy-level-for

Through a CDS view how can we add the annotations for:

  • sap:hierarchy-node-for
  • sap:hierarchy-level-for
  • sap:hierarchy-parent-node-for
  • sap:hierarchy-drill-state-for

I do not see these specific annotations:

I need to generate the metadata to look like

<EntityType Name="Node">
	<Key>
	     <PropertyRef Name="NodeID" />
	</Key>
	<Property Name="NodeID" Type="Edm.Int32" Nullable="false" edmanno:StoreGeneratedPattern="Identity" sap:hierarchy-node-for="NodeID"/>
	<Property Name="HierarchyLevel" Type="Edm.Int32" Nullable="false" sap:hierarchy-level-for="NodeID"/>
	<Property Name="WBSDescription" Type="Edm.String" MaxLength="40" sap:label="WBS Element Name" sap:quickinfo="PS: Short description (1st text line)"/>
	<Property Name="ParentNodeID" Type="Edm.Int32" Nullable="true" sap:hierarchy-parent-node-for="NodeID"/>
	<Property Name="DrillState" Type="Edm.String" Nullable="false" MaxLength="10" Unicode="true" FixedLength="false" sap:hierarchy-drill-state-for="NodeID" />
</EntityType>



I tried the following annotations but it is not working:

@Hierarchy.parentChild: { name: 'WBSHierarchy',
                            recurse : { 
                                parent: [ 'ParentNodeID'],
                                child: ['NodeID'] }

My fields in the CDS are defined as:

    //NodeID
    key concat( _Project.Project, _WBSElementHierarchy.HierarchyNode) as NodeID,


    //HierarchyLevel - Need to zero sequence the Hierarchy
    _WBS.WBSElementHierarchyLevel - 1 as Hierarchylevel,
    
    //ParentNodeID
    case 
        when (_WBS.WBSElementHierarchyLevel > 1)
            then concat( _Project.Project, _WBSElementHierarchy.ParentNode)
        else
            '' 
    end as ParentNodeID,
    _WBSParent,


    //DrillState
    cast( 'expanded' as abap.sstring(10) ) as DrillState,

Is there a way to add those exact annotations to the Node, Hierarchy Level, Parent and Drillstate to the individual fields?

Thanks,

Jay

Add a comment
10|10000 characters needed characters exceeded

  • I added this to the XML view which gets around the annotation issue.:

    <TreeTable id="table" selectionMode="Single" enableColumnReordering="false" rows="{ path : '/ZRTR_PROJ_WBS_HIERARCHY', parameters : { countMode: 'Inline', numberOfExpandedLevels: 3, treeAnnotationProperties : { hierarchyLevelFor : 'HierarchyLevel', hierarchyNodeFor : 'Counter', hierarchyParentNodeFor : 'ParentCounter', hierarchyDrillStateFor : 'Drillstate' } } }">

    There should be way to do this with simple annotations.

    Thanks,

    Jay

  • The blog post you mentioned was for a specific usage in Fiori Elements Tree table. I do not think the metadata annotations you are lloking for in the resultant metadata are generated by @Hierarchy.parentChild annotations. Where are you going to consume this OData - Fiori apps or any Analytics client.?

Related questions

2 Answers

  • Best Answer
    Posted on Dec 07, 2020 at 11:03 AM

    Hi Jay,

    Below wiki might be helpful.

    https://wiki.scn.sap.com/wiki/display/BI/A+very+simple+hierarchy+without+hierarchy+directory

    First, you need a hierarchy view and a dimension view consuming the hierarchy view.

    Next you will associate the dimension view with a consumption view.

    Then the consumption view will get the the annotations you mentioned.

    I followed the same wiki and it worked.

    Regards,

    Mio

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 19, 2020 at 07:00 PM

    Sumit Kumar Kundu - I just posted this question. Is there a way to generate those required metadata field and values for the hierarchy without using an MPC class? Referring to your post - https://blogs.sap.com/2019/04/01/fiori-elements-tree-table-with-sap-annotations/

    Jocelyn Dart - Was also referring to your comment on https://blogs.sap.com/2018/01/11/fiori-elements-list-report-sorting-grouping-and-table-types/

    Basically, I am trying to render a tree view, the data I have is good but I am unable to generate the annotations from the CDS:

    Here is the JSON that works for the display:

    [
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000355')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000355')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000355",
        "HierarchyLevel": 0,
        "ParentNodeID": "",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600010000000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "Digestive health-endoscopy"
      },
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000357')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000357')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000357",
        "HierarchyLevel": 1,
        "ParentNodeID": "I0000660000000000000000355",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600010100000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "Endoscopy-Advanced technology"
      },
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000358')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000358')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000358",
        "HierarchyLevel": 1,
        "ParentNodeID": "I0000660000000000000000355",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600010200000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "Endoscopy-Traditional technology"
      },
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000356')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000356')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000356",
        "HierarchyLevel": 0,
        "ParentNodeID": "",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600020000000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "Digestive health-CT Scan"
      },
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000359')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000359')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000359",
        "HierarchyLevel": 1,
        "ParentNodeID": "I0000660000000000000000356",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600020100000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "CT Scan - Advanced"
      },
      {
        "__metadata": {
          "id": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000360')",
          "uri": "http://frdev:8001/sap/opu/odata/sap/ZRTR_PROJECT_WBS_CDS/ZRTR_PROJECT_WBS('I0000660000000000000000360')",
          "type": "ZRTR_PROJECT_WBS_CDS.ZRTR_PROJECT_WBSType"
        },
        "NodeID": "I0000660000000000000000360",
        "HierarchyLevel": 2,
        "ParentNodeID": "I0000660000000000000000359",
        "DrillState": "expanded",
        "Project": "I-000066",
        "WBSElement": "I00006600020101000",
        "ProjectDescription": "Digestive Health",
        "WBSDescription": "CT Scan - Old technology"
      }
    ]
    

    Your help is appreciated in advance.

    Thanks,

    Jay

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.