Skip to Content
Author's profile photo Vijay Sharma

Mass Creation of Customer Hierarchy

While data migration, sometimes we come across requirements which need BDC programs to be developed to upload the data in Mass, but sometimes even BDC program or LSMW recording is not feasible like in case of Customer Hierarchy (VDH2N) Transaction.

Oops Problem!!


In such cases we have following options:

  • Look for BAPI or Functional Module and use that BAPI in LSMW.




  • In case of functional module create a custom program using it or create a wrap around BAPI to use in LSMW. Lot of effort and CTS is required to develop and transport the code to different SAP Systems (Testing, Quality, Production).


In case we have a standard BAPI available and is associated with a business object life is easy, otherwise… Sounds Tough Right!!!


In this blog we will discuss a third and easy method to create an upload program for Customer Hierarchy Mass creation without creating a custom program or wrap around BAPI.I used the BAPI “BAPI_CUSTOMER_HIERARCHIE_INS” but differently. Exciting, isn’t it?


Let’s start step by step.

  • Create an LSMW Program with dummy recording for any transaction, just to complete the first step or use any already existing recording. Basically we are not going to use this recording after this step.




  • Create the source structure and the source fields as shown below. Make sure that all the below shown fields are maintained in structure as they are required to create customer hierarchy.




  • Now, inside “Field Mapping and Conversion” step write a code under the processing area “End _Of_Record” using BAPI “BAPI_CUSTOMER_HIERARCHIE_INS” as shown below. Use “End_Of_Record” processing area as it is processed at the end of each input record.




  • Code can be modified as per the output requirement.
  • The input file format need to have the fields as shown below.




  • Specify the input file location in LSMW.
  • Execute the data read step and check if the data is read properly from the file.




  • Now when “Convert Data” step is executed, the code is processed record by record and customer hierarchy got created. I have coded to display the error customers in output. So, if any record is failed due to any reason the corresponding customers will be displayed on screen after execution of all records in file.


We are done with the Customer Hierarchy creation and no further LSMW Steps need to be processed.So, this way we can handle such situations where recording is not possible and don’t have enough time to create custom programs to Mass upload.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      This works well. Great! Thanks for sharing.

      Author's profile photo Lars Kjøller
      Lars Kjøller



      I have trouble when i convert my data.

      I get the errors that the customers used does not exist.

      However the customer number on the given sales org with distribution channel and division, does in fact exist.

      Any pointers on what I could be doing wrong?

      Author's profile photo Former Member
      Former Member

      Thanks for sharing!

      We have modified this code for CHANGING entries of KNVH-table. For this we use BAPI BAPI_CUSTOMER_HIERARCHIE_UPD. My source is a XLSX-download from transaction SE16, converted to a TXT-file.

      First of all I changed the sourcefields similar to table KNVH.

      ZQUELL Quelldatei

      KUNNR C(010) Debitor
      VKORG C(004) Verkaufsorganisation
      VTWEG C(002) Vertriebsweg
      SPART C(002) Sparte
      DATAB C(010) DATAB
      HITYP C(001) HITYP
      HKUNNR C(010) HKUNNR
      HVKORG C(004) HVKORG
      HVTWEG C(002) HVTWEG
      HSPART C(002) HSPART
      DATBI C(010) gültig bis
      GRPNO C(003) Formroutine
      BOKRE C(001) Bonus
      PRFRE C(001) Preisfindung
      HZUOR C(002) Hierarchiezuordnung

      Then I changed the code.

      1 transfer_record.

      2 DATA: it_knvh_alt TYPE TABLE OF bapikna1_knvh_process,
      3 it_knvh_neu TYPE TABLE OF bapikna1_knvh_process,
      4 wa_knvh TYPE bapikna1_knvh_process,
      5 it_msg TYPE TABLE OF bapiret2,
      6 wa_msg TYPE bapiret2.
      7 break-point.
      8 CLEAR: it_knvh_alt[], it_knvh_neu[], wa_knvh.
      9 wa_knvh-customer = zquell-kunnr.
      10 wa_knvh-salesorg = zquell-vkorg.
      11 wa_knvh-distr_chan = zquell-vtweg.
      12 wa_knvh-division = zquell-spart.
      13 concatenate zquell-datab+6(4) zquell-datab+3(2) zquell-datab(2) into
      14 wa_knvh-valid_from.
      15 wa_knvh-custhityp = zquell-hityp.
      16 wa_knvh-parent_customer = zquell-hkunnr.
      17 wa_knvh-parent_sales_org = zquell-hvkorg.
      18 wa_knvh-parent_distr_chan = zquell-hvtweg.
      19 wa_knvh-parent_division = zquell-hspart.
      20 concatenate zquell-datbi+6(4) zquell-datbi+3(2) zquell-datbi(2) into
      21 wa_knvh-valid_to.
      22 wa_knvh-routine = zquell-grpno.
      23 wa_knvh-rebate_rel = zquell-bokre.
      24 wa_knvh-PRIC_REL = zquell-prfre.
      25 wa_knvh-ASSIGN_HI = zquell-hzuor.
      26 APPEND wa_knvh TO it_knvh_alt.
      27 wa_knvh-valid_to = sy-datum.
      28 APPEND wa_knvh TO it_knvh_neu.
      31 TABLES
      32 node_list_before = it_knvh_alt
      33 node_list_after = it_knvh_neu
      34 return = it_msg.
      36 IF it_msg IS NOT INITIAL.
      37 LOOP AT it_msg INTO wa_msg.
      38 WRITE: wa_knvh-customer, wa_msg-type, wa_msg-message, wa_msg-message_v1,
      39 wa_msg-message_v2, wa_msg-message_v3, wa_msg-message_v4.
      40 ENDLOOP.
      41 ELSE.
      43 ENDIF.

      Works perfect!



      Author's profile photo Former Member
      Former Member

      One every important thing I forgot. Please use BAPI BAPI_TRANSACTION_COMMIT with importing parmeter WAIT = 'X'


      43 EXPORTING wait = 'X'.
      44 ENDIF.


      Author's profile photo Vijay Sharma
      Vijay Sharma
      Blog Post Author

      Perfect! Thanks For sharing Stefan.

      Author's profile photo Gerard Not
      Gerard Not

      Hi gurus,


      I'm having trouble to create the customer hierarchy, when i execute the BAPI manually from SE37, don`t have any message return and don`t create the hierarchy.


      When i create the hierarchy from tx. VDH1N, the process is successful.


      This is the data that i use in the BAPI "BAPI_CUSTOMER_HIERARCHIE_INS", that is the same "Source data" in the LSMW of this blog ¿I'm missing something?

      Author's profile photo Vijay Sharma
      Vijay Sharma
      Blog Post Author


      This should work if data is correct.




      Author's profile photo pragya mishra
      pragya mishra

      Hello VIjay,


      Can I create LSMW using BAPI Method and Use "BAPI_CUSTOMER_HIERARCHIE_INS" BAPI for customer Hierarchy Insert. I tried to find out Business Object for this BAPI, but didnot get , Can you please help me ?