Skip to Content
Author's profile photo Jhon Jairo Teran Salazar

Create customers using CMD_EI_API with central and sales data

Hello everybody,

Today i would like to share a code snippet to create customers based upon a text file with tab separator, it’s very easy and functional using this api, in 161 lines of code you can create a client with basic central data and sales data.

The form receives the work area with customer data, this create a customer at time, so i call this routine from a loop of a internal table with file data.

form create_customer_api  using p_wa_file type ty_file

                                 value(p_row_id) type sytabix.

  data: l_wa_main type cmds_ei_main,

        l_it_customers type cmds_ei_extern_t,

        l_wa_customers type cmds_ei_extern,

        l_wa_phones type cvis_ei_phone_str,

        l_wa_email type cvis_ei_smtp_str,

        l_wa_sales type cmds_ei_sales,

        l_wa_loading type cmds_ei_loading,

        l_wa_functions type cmds_ei_functions,

        l_update_errors type cvis_message,

        l_it_bdcmsgcoll type table of bdcmsgcoll,

        l_wa_bdcmsgcoll type bdcmsgcoll,

        l_wa_messages type bapiret2,

        l_wa_log_alv type ty_log_alv,

        l_kunnr type kna1kunnr.

  l_wa_customersheaderobject_task = ‘I’.

  l_wa_customerscentral_dataaddresstask = ‘I’.

  case p_wa_fileanred.

   when ‘Sra.’.

    l_wa_customerscentral_dataaddresspostaldatatitle = ‘0001’.

   when ‘Sr.’.

    l_wa_customerscentral_dataaddresspostaldatatitle = ‘0002’.

   when ‘Empresa’.

    l_wa_customerscentral_dataaddresspostaldatatitle = ‘0003’.

   when others.

    l_wa_customerscentral_dataaddresspostaldatatitle = p_wa_fileanred.


  l_wa_customerscentral_dataaddresspostaldataname = p_wa_filename1.

  l_wa_customerscentral_dataaddresspostaldataname_2 = p_wa_filename2.

  l_wa_customerscentral_dataaddresspostaldataname_3 = p_wa_filename3.

  l_wa_customerscentral_dataaddresspostaldataname_4 = p_wa_filename4.

  l_wa_customerscentral_dataaddresspostaldatasort1 = p_wa_filesortl.

  l_wa_customerscentral_dataaddresspostaldatastreet = p_wa_filestreet.

  l_wa_customerscentral_dataaddresspostaldatastr_suppl3 = p_wa_filestr_suppl3.

  l_wa_customerscentral_dataaddresspostaldatapostl_cod1 = p_wa_filepost_code1.

  l_wa_customerscentral_dataaddresspostaldatacity = p_wa_filecity1.

  l_wa_customerscentral_dataaddresspostaldataregion = p_wa_fileregion.

  l_wa_customerscentral_dataaddresspostaldatatranspzone = p_wa_filetranspzone.

  l_wa_customerscentral_dataaddresspostaldataextens_1 = p_wa_fileextension1.

  l_wa_customerscentral_dataaddresspostaldatalangu = ‘S’.

  l_wa_customerscentral_dataaddresspostaldatacountry = ‘CO’.

  l_wa_customerscentral_datacentraldatabrsch = p_wa_filebrsch.

  l_wa_customerscentral_datacentraldatastcd1 = p_wa_filestcd1.

  l_wa_customerscentral_datacentraldatastcdt = p_wa_filestcdt.

  l_wa_customerscentral_datacentraldatafityp = p_wa_filefityp.

  l_wa_customerscentral_datacentraldataniels = p_wa_fileniels.

  l_wa_customerscentral_datacentraldatakukla = p_wa_filekukla.

  l_wa_customerscentral_datacentraldatabran1 = p_wa_filebran1.

  l_wa_customerscentral_datacentraldataktokd = pa_ktokd.

  l_wa_customerscentral_datacentraldatakatr1 = p_wa_filekatr1.

  l_wa_customerscentral_datacentraldatakatr2 = p_wa_filekatr2.

  l_wa_customerscentral_datacentraldatakatr3 = p_wa_filekatr3.

  l_wa_loadingtask = ‘I’.

  l_wa_loadingdata_keyablad = p_wa_fileablad.

  l_wa_loadingdataknfak = p_wa_fileknfak.

  append l_wa_loading to l_wa_customerscentral_dataloadingloading.

  l_wa_phonescontacttask = ‘I’.

  l_wa_phonescontactdatatelephone = p_wa_filetelf1.

  append l_wa_phones to l_wa_customerscentral_dataaddresscommunicationphonephone.

  l_wa_emailcontacttask = ‘I’.

  l_wa_emailcontactdatae_mail = p_wa_filesmtp_addr.

  append l_wa_email to l_wa_customerscentral_dataaddresscommunicationsmtpsmtp.

  l_wa_salestask = ‘I’.

  l_wa_salesdata_keyvkorg = pa_vkorg.

  l_wa_salesdata_keyvtweg = pa_vtweg.

  l_wa_salesdata_keyspart = pa_spart.

  l_wa_salesdatabzirk = p_wa_filebzirk.

  l_wa_salesdatavkbur = p_wa_filevkbur.

  l_wa_salesdatavkgrp = p_wa_filevkgrp.

  l_wa_salesdatakdgrp = p_wa_filekdgrp.

  l_wa_salesdatakonda = p_wa_filekonda.

  l_wa_salesdatakalks = p_wa_filekalks.

  l_wa_salesdatapltyp = p_wa_filepltyp.

  l_wa_salesdataversg = p_wa_fileversg.

  l_wa_salesdatalprio = p_wa_filelprio.

  l_wa_salesdatavsbed = p_wa_filevsbed.

  l_wa_salesdatavwerk = p_wa_filevwerk.

  l_wa_salesdatabokre = p_wa_filebokre.

  l_wa_salesdataprfre = p_wa_fileprfre.

  l_wa_salesdatainco1 = p_wa_fileinco1.

  l_wa_salesdatakkber = p_wa_filekkber.

  l_wa_salesdatakabss = p_wa_filekabss.

  l_wa_functionstask = ‘I’.

  l_wa_functionsdata_keyparvw = ‘WE’.

  l_wa_functionsdatadefpa = ‘X’.

  append l_wa_functions to l_wa_salesfunctionsfunctions.

  clear l_wa_functionsdatadefpa.



      input = p_wa_filekunn2


      output = l_wa_functionsdatapartner.

  l_wa_functionsdata_keyparvw = ‘AG’.

  append l_wa_functions to l_wa_salesfunctionsfunctions.

  l_wa_functionsdata_keyparvw = ‘VE’.

  l_wa_functionsdatapartner = p_wa_filepernr.

  append l_wa_functions to l_wa_salesfunctionsfunctions.

  append l_wa_sales to l_wa_customerssales_datasales.

  cmd_ei_api=>get_number( exporting iv_ktokd = pa_ktokd

                          importing ev_kunnr = l_wa_customersheaderobject_instancekunnr

                                    es_error = l_update_errors ).

  append l_wa_customers to l_wa_maincustomers.

  if l_update_errorsis_error = space.

   clear: l_update_errors.



        iv_test_run    = space

        is_master_data = l_wa_main


        es_error       l_update_errors ).

   if l_update_errorsis_error = space.

    commit work.


    rollback work.



  if l_update_errorsis_error = ‘X’.

    cont_reg_error = cont_reg_error + 1.

    loop at l_update_errorsmessages into l_wa_messages.

     l_wa_log_alvrow_id = p_row_id.

     l_wa_log_alvmsgtyp = l_wa_messagestype.

     call function ‘MESSAGE_TEXT_BUILD’


         msgid               = l_wa_messagesid

         msgnr               = l_wa_messagesnumber

         msgv1               = l_wa_messagesmessage_v1

         msgv2               = l_wa_messagesmessage_v2

         msgv3               = l_wa_messagesmessage_v3

         msgv4               = l_wa_messagesmessage_v4


         message_text_output = l_wa_log_alvmessage.

     append l_wa_log_alv to it_log_alv.



   l_wa_log_alvrow_id = p_row_id.

   l_wa_log_alvmsgtyp = ‘S’.

   l_wa_log_alvkunnr = l_wa_customersheaderobject_instancekunnr.

   l_wa_log_alvmessage = ‘Cliente creado exitosamente’.

   append l_wa_log_alv to it_log_alv.



Kind regards.

Jhon Jairo.

Assigned Tags

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

      Wouldn't it be much less than 161 lines of source code if you had considered using MOVE-CORRESPONDING instead?

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Yes of course, that's right, it can be much less if you use MOVE-CORRESPONDING, but the point is that using this API is much less code if you use other tecnology like BATCH INPUT, even if you are using MOVE-CORRESPONDING or not.

      I appreciate your comments,

      Kind regards.

      Jhon Jairo.

      Author's profile photo ayush sethi
      ayush sethi


      i appreciate your work. I need to provide customer role in business partner. Can i do this by using this code. I will appreciate your reply.

      Ayush Sethi

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Hello Ayush,

      Please could you attach a screenshoot where you include this data in the XD* transaction.

      Best regards.

      Jhon Jairo.

      Author's profile photo ayush sethi
      ayush sethi

      Hello sir,

      i am working on s4 hana server so the concept of transaction XD* is obsolete. So we are using BP(Business Partner ) Transaction to create customer. Here i need to update sales and distribution data for a particular roles (FLCU01 which is CUSTOMER and company code data FLCU00 which is FI customer). But i am not able to update these two data through any bapi. pls help me out

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Ayush, Have you tried using BAPI_BUPA_CREATE_FROM_DATA?, I think that you can't use the same class that I used to create this kind of object. Please check the next link, may be could help you.

      Tell me if this was useful.

      Kind regards.

      Jhon Jairo.

      Author's profile photo ayush sethi
      ayush sethi


      Yes sir i have used this bapi only but this is used to create business partner and give roles my requirement is to update sales and distribution data and company code data in bp transaction.

      Moreover i am facing one tragic thing, the details of sales and distribution can be seen in customer tables (KNVV) but they are not getting reflected on BP transaction.

      Ayush Sethi

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Hello Ayush, I think if your requirement is to update sales and distribution data you could actually  try to use the class cmd_ei_api, because as you say I understand that this information is saved in KNVV table, the same for KNB1 that saves the company data, it is the same when you create or enhance a customer in XD* transaction.


      Jhon Jairo.

      Author's profile photo João Guedes
      João Guedes

      Hi Jhon, I'm trying to populate ICMSTAXPAY (KNA1-ICMSTAXPAY), ICMS Taxpayer into this structure used in CMD_EI_API, but unfortunately I can't find a way to do that.

      Can you help me ?

      Author's profile photo Jhon Jairo Teran Salazar
      Jhon Jairo Teran Salazar
      Blog Post Author

      Hello Joao, sorry for the delay in my response, I was a little bit abscent from the community, did you overcome this issue?

      Jhon Jairo.