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.