Skip to Content
author's profile photo Former Member
Former Member

bapi_costelementgrp_create and hierarchy nodes

Hi to all,

I need an algorythm in order to fill the internal table hierarchynodes (def. like bapiset_hier ) to pass to the bapi_costelementgrp_create.

The hierarchy nodes will be uploaded from a file with two fields: "father node" and "child node" like the following:

TESTJR TESTJR2

TESTJR2 TESTJR3

TESTJR TESTJR4

in the internal table the hierlevel shoul be like the following schema:

TESTJR is level 0

TESTJR2 have to assigned to TESTJR (hierlevel 1)

TESTJR3 have to assigned to TESTJR2 (hierlevel 2)

TESTJR4 have to assigned to TESTJR (hierlevel 1)

Any ideas and suggestions?

Thanks in advance.

Andrew

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Apr 14, 2011 at 11:49 PM

    Hi,

    you have to options. Go to SE37 and read documentaiton for BAPI_COSTELEMENTGRP_CREATE where it's described how to fill these tables. If you are lazy to read then you can create a sample hierarchy in standard SAP transaction and use BAPI BAPI_COSTELEMENTGRP_GETDETAIL to get example how you need to pass data. You can see that both BAPIs have similar interface.

    Cheers

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Apr 15, 2011 at 05:07 AM

    Hello Andrew,

    Try the code below. However, you should test it thoroughly. WARNING: Make sure first record in internal table pertains to the root node. The internal table lt_hier is the internal table that you assign parameter "hierarchynodes" when calling BAPI_COSTELEMENTGRP_CREATE.

    Kind Regards,

    Rae Ellen Woytowiez

    *
    * Types, internal table, and structures for input file
    TYPES:  BEGIN OF input,
              groupname1 TYPE bapiset_groupname,
              groupname2 TYPE bapiset_groupname,
            END OF input.
    TYPES:  input_tt TYPE TABLE OF input.
    
    DATA:  lt_input TYPE input_tt,
           ls_input TYPE input.
    
    TYPES:  BEGIN OF input_numbered,
              groupname1 TYPE bapiset_groupname,
              groupname2 TYPE bapiset_groupname,
              rrn TYPE i,
            END OF input_numbered.
    TYPES:  input_numbered_tt TYPE TABLE OF input_numbered.
    DATA:  lt_input_numbered TYPE input_numbered_tt,
           ls_input_numbered TYPE input_numbered.
    *
    * Types, internal tables, structures used to help
    * determine hierarchy level.
    TYPES:  BEGIN OF group_level_numbered,
              level TYPE bapiset_level,
              rrn TYPE i,
              groupname TYPE bapiset_groupname,
            END OF group_level_numbered.
    TYPES:  group_level_numbered_tt TYPE TABLE OF group_level_numbered.
    DATA:  lt_group_level_numbered TYPE group_level_numbered_tt,
           ls_group_level_numbered TYPE group_level_numbered.
    *
    * Types, internal table, structures for function module.
    TYPES:  bapiset_hier_tt TYPE TABLE OF bapiset_hier,
            bapi1113_values_tt TYPE TABLE OF bapi1113_values.
    DATA:  lt_hier TYPE bapiset_hier_tt,
           lt_values TYPE bapi1113_values_tt,
           ls_hier TYPE bapiset_hier,
           ls_values TYPE bapi1113_values.
    
    DATA:  count TYPE i.
    DATA:  level TYPE i.
    
    START-OF-SELECTION.
    
      count = 1.
    
      LOOP AT lt_input INTO ls_input.
        CLEAR:  ls_input_numbered.
        ls_input_numbered-groupname1 = ls_input-groupname1.
        ls_input_numbered-groupname2 = ls_input-groupname2.
        ls_input_numbered-rrn = count.
        APPEND ls_input_numbered TO lt_input_numbered.
        count = count + 1.
      ENDLOOP.
    
    *
    * Build hierarchy
      SORT lt_input_numbered BY groupname1 rrn.
      level = 0.
      LOOP AT lt_input_numbered INTO ls_input_numbered.
        PERFORM process_group
                    USING
                       ls_input_numbered
                    CHANGING
                       level.
      ENDLOOP.
    *
    * derive table to be used in FM
      SORT lt_group_level_numbered BY rrn.
    
      LOOP AT lt_group_level_numbered INTO ls_group_level_numbered.
        ls_hier-groupname = ls_group_level_numbered-groupname.
        ls_hier-hierlevel = ls_group_level_numbered-level.
        ls_hier-DESCRIPT = ls_group_level_numbered-groupname.
        APPEND ls_hier TO lt_hier.
      ENDLOOP.
    FORM process_group USING value(ps_input_numbered) TYPE input_numbered
                       CHANGING value(pv_level) TYPE bapiset_level.
    
    
      DATA:  groupname1_subrc TYPE sysubrc,
             groupname1_tabix TYPE sytabix.
      DATA:  groupname2_subrc TYPE sysubrc,
             groupname2_tabix TYPE sytabix.
    
    *
    * Check and if necessary add groupname1.
      CLEAR ls_group_level_numbered.
      READ TABLE lt_group_level_numbered INTO ls_group_level_numbered
                 WITH KEY groupname = ps_input_numbered-groupname1
                 BINARY SEARCH.
      groupname1_subrc = sy-subrc.
      groupname1_tabix = sy-tabix.
    
      IF groupname1_subrc <> 0.
        ls_group_level_numbered-groupname = ps_input_numbered-groupname1.
        ls_group_level_numbered-level = pv_level.
        ls_group_level_numbered-rrn = ps_input_numbered-rrn.
        INSERT ls_group_level_numbered INTO lt_group_level_numbered
               INDEX groupname1_tabix.
      ELSE.
        pv_level = ls_group_level_numbered-level.
      ENDIF.
    
    *
    * Check and if necessary add groupname2.
      CLEAR ls_group_level_numbered.
      READ TABLE lt_group_level_numbered INTO ls_group_level_numbered
                 WITH KEY groupname = ps_input_numbered-groupname2
                 BINARY SEARCH.
      groupname2_subrc = sy-subrc.
      groupname2_tabix = sy-tabix.
      IF groupname2_subrc <> 0.
        ls_group_level_numbered-groupname = ps_input_numbered-groupname2.
        ls_group_level_numbered-level = pv_level + 1.
        ls_group_level_numbered-rrn = ps_input_numbered-rrn.
        INSERT ls_group_level_numbered INTO lt_group_level_numbered
               INDEX groupname2_tabix.
      ENDIF.
    
    
    *
    * Check for childs for groupname2.
      pv_level = pv_level + 1.
      LOOP AT lt_input_numbered INTO ls_input_numbered
                         WHERE groupname1 = ps_input_numbered-groupname2.
    
        PERFORM process_group
                    USING
                       ls_input_numbered
                    CHANGING
                       level.
    
      ENDLOOP.
    ENDFORM.
    
    

    Edited by: Rae Ellen Woytowiez on Apr 15, 2011 7:24 AM

    Edited by: Rae Ellen Woytowiez on Apr 15, 2011 7:26 AM

    Edited by: Rae Ellen Woytowiez on Apr 15, 2011 7:26 AM

    Add a comment
    10|10000 characters needed characters exceeded

    • Hello Andrew,

      You are very welcome.

      Apparently the less than sign, greater than sign version of the not equal sign does not copy properly to this forum; it is used as a formatting option for text. Therefore, I needed to change the not equal sign from less than sign, greater than sign to "ne" format. That's what caused the confusion we were experiencing.

      Have a nice day!

      Rae Ellen Woytowie

      Edited by: Rae Ellen Woytowiez on Apr 15, 2011 3:44 PM

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.