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 sy–tabix.
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 kna1–kunnr.
l_wa_customers–header–object_task = ‘I’.
l_wa_customers–central_data–address–task = ‘I’.
case p_wa_file–anred.
when ‘Sra.’.
l_wa_customers–central_data–address–postal–data–title = ‘0001’.
when ‘Sr.’.
l_wa_customers–central_data–address–postal–data–title = ‘0002’.
when ‘Empresa’.
l_wa_customers–central_data–address–postal–data–title = ‘0003’.
when others.
l_wa_customers–central_data–address–postal–data–title = p_wa_file–anred.
endcase.
l_wa_customers–central_data–address–postal–data–name = p_wa_file–name1.
l_wa_customers–central_data–address–postal–data–name_2 = p_wa_file–name2.
l_wa_customers–central_data–address–postal–data–name_3 = p_wa_file–name3.
l_wa_customers–central_data–address–postal–data–name_4 = p_wa_file–name4.
l_wa_customers–central_data–address–postal–data–sort1 = p_wa_file–sortl.
l_wa_customers–central_data–address–postal–data–street = p_wa_file–street.
l_wa_customers–central_data–address–postal–data–str_suppl3 = p_wa_file–str_suppl3.
l_wa_customers–central_data–address–postal–data–postl_cod1 = p_wa_file–post_code1.
l_wa_customers–central_data–address–postal–data–city = p_wa_file–city1.
l_wa_customers–central_data–address–postal–data–region = p_wa_file–region.
l_wa_customers–central_data–address–postal–data–transpzone = p_wa_file–transpzone.
l_wa_customers–central_data–address–postal–data–extens_1 = p_wa_file–extension1.
l_wa_customers–central_data–address–postal–data–langu = ‘S’.
l_wa_customers–central_data–address–postal–data–country = ‘CO’.
l_wa_customers–central_data–central–data–brsch = p_wa_file–brsch.
l_wa_customers–central_data–central–data–stcd1 = p_wa_file–stcd1.
l_wa_customers–central_data–central–data–stcdt = p_wa_file–stcdt.
l_wa_customers–central_data–central–data–fityp = p_wa_file–fityp.
l_wa_customers–central_data–central–data–niels = p_wa_file–niels.
l_wa_customers–central_data–central–data–kukla = p_wa_file–kukla.
l_wa_customers–central_data–central–data–bran1 = p_wa_file–bran1.
l_wa_customers–central_data–central–data–ktokd = pa_ktokd.
l_wa_customers–central_data–central–data–katr1 = p_wa_file–katr1.
l_wa_customers–central_data–central–data–katr2 = p_wa_file–katr2.
l_wa_customers–central_data–central–data–katr3 = p_wa_file–katr3.
l_wa_loading–task = ‘I’.
l_wa_loading–data_key–ablad = p_wa_file–ablad.
l_wa_loading–data–knfak = p_wa_file–knfak.
append l_wa_loading to l_wa_customers–central_data–loading–loading.
l_wa_phones–contact–task = ‘I’.
l_wa_phones–contact–data–telephone = p_wa_file–telf1.
append l_wa_phones to l_wa_customers–central_data–address–communication–phone–phone.
l_wa_email–contact–task = ‘I’.
l_wa_email–contact–data–e_mail = p_wa_file–smtp_addr.
append l_wa_email to l_wa_customers–central_data–address–communication–smtp–smtp.
l_wa_sales–task = ‘I’.
l_wa_sales–data_key–vkorg = pa_vkorg.
l_wa_sales–data_key–vtweg = pa_vtweg.
l_wa_sales–data_key–spart = pa_spart.
l_wa_sales–data–bzirk = p_wa_file–bzirk.
l_wa_sales–data–vkbur = p_wa_file–vkbur.
l_wa_sales–data–vkgrp = p_wa_file–vkgrp.
l_wa_sales–data–kdgrp = p_wa_file–kdgrp.
l_wa_sales–data–konda = p_wa_file–konda.
l_wa_sales–data–kalks = p_wa_file–kalks.
l_wa_sales–data–pltyp = p_wa_file–pltyp.
l_wa_sales–data–versg = p_wa_file–versg.
l_wa_sales–data–lprio = p_wa_file–lprio.
l_wa_sales–data–vsbed = p_wa_file–vsbed.
l_wa_sales–data–vwerk = p_wa_file–vwerk.
l_wa_sales–data–bokre = p_wa_file–bokre.
l_wa_sales–data–prfre = p_wa_file–prfre.
l_wa_sales–data–inco1 = p_wa_file–inco1.
l_wa_sales–data–kkber = p_wa_file–kkber.
l_wa_sales–data–kabss = p_wa_file–kabss.
l_wa_functions–task = ‘I’.
l_wa_functions–data_key–parvw = ‘WE’.
l_wa_functions–data–defpa = ‘X’.
append l_wa_functions to l_wa_sales–functions–functions.
clear l_wa_functions–data–defpa.
call function ‘CONVERSION_EXIT_ALPHA_INPUT’
exporting
input = p_wa_file–kunn2
importing
output = l_wa_functions–data–partner.
l_wa_functions–data_key–parvw = ‘AG’.
append l_wa_functions to l_wa_sales–functions–functions.
l_wa_functions–data_key–parvw = ‘VE’.
l_wa_functions–data–partner = p_wa_file–pernr.
append l_wa_functions to l_wa_sales–functions–functions.
append l_wa_sales to l_wa_customers–sales_data–sales.
cmd_ei_api=>get_number( exporting iv_ktokd = pa_ktokd
importing ev_kunnr = l_wa_customers–header–object_instance–kunnr
es_error = l_update_errors ).
append l_wa_customers to l_wa_main–customers.
if l_update_errors–is_error = space.
clear: l_update_errors.
cmd_ei_api=>maintain(
exporting
iv_test_run = space
is_master_data = l_wa_main
importing
es_error = l_update_errors ).
if l_update_errors–is_error = space.
commit work.
else.
rollback work.
endif.
endif.
if l_update_errors–is_error = ‘X’.
cont_reg_error = cont_reg_error + 1.
loop at l_update_errors–messages into l_wa_messages.
l_wa_log_alv–row_id = p_row_id.
l_wa_log_alv–msgtyp = l_wa_messages–type.
call function ‘MESSAGE_TEXT_BUILD’
exporting
msgid = l_wa_messages–id
msgnr = l_wa_messages–number
msgv1 = l_wa_messages–message_v1
msgv2 = l_wa_messages–message_v2
msgv3 = l_wa_messages–message_v3
msgv4 = l_wa_messages–message_v4
importing
message_text_output = l_wa_log_alv–message.
append l_wa_log_alv to it_log_alv.
endloop.
else.
l_wa_log_alv–row_id = p_row_id.
l_wa_log_alv–msgtyp = ‘S’.
l_wa_log_alv–kunnr = l_wa_customers–header–object_instance–kunnr.
l_wa_log_alv–message = ‘Cliente creado exitosamente’.
append l_wa_log_alv to it_log_alv.
endif.
endform.
Kind regards.
Jhon Jairo.
Wouldn't it be much less than 161 lines of source code if you had considered using MOVE-CORRESPONDING instead?
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.
Sir,
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.
Regards
Ayush Sethi
Hello Ayush,
Please could you attach a screenshoot where you include this data in the XD* transaction.
Best regards.
Jhon Jairo.
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
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.
https://archive.sap.com/discussions/thread/642223
Tell me if this was useful.
Kind regards.
Jhon Jairo.
hi
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.
regards
Ayush Sethi
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.
Regars.
Jhon Jairo.
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 ?
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.