本文主要是介绍SAP BAPI-创建供应商客户主数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 批量创建供应商主数据
使用BAPI批量创建供应商主数据
先创建供应商一般主数据,再根据一般主数据扩展到公司代码层级
CLASS LCL_VMD_BAPI DEFINITION.
PUBLIC SECTION.
CLASS-DATA:
GR_MAIN TYPE VMDS_EI_MAIN,
LS_MAIN1 TYPE VMDS_EI_MAIN,
LS_MAIN2 TYPE VMDS_EI_MAIN,
LS_MESG TYPE CVIS_MESSAGE,
LS_MESG1 TYPE CVIS_MESSAGE,CGT_VEND TYPE VMDS_EI_EXTERN_T,
CGR_VEND TYPE VMDS_EI_EXTERN,CGR_COMPANY TYPE VMDS_EI_COMPANY,
CGT_COMPANY TYPE VMDS_EI_COMPANY_T.CLASS-METHODS:
MAIN_LFA1 IMPORTING" I_KTOKD TYPE KTOKD
I_LIFNR TYPE LIFNR
I_NAME TYPE BAPIAD1VL-NAME
I_LANGU TYPE BAPIAD1VL-LANGU
I_COUNTRY TYPE BAPIAD1VL-COUNTRY
I_CITY TYPE BAPIAD1VL-PO_BOX_CIT
I_SORT1 TYPE BAPIAD1VL-SORT1
EXPORTING E_LIFNR TYPE LIFNR
E_ERR_MSG TYPE CLIKE,MAIN_LFB1 IMPORTING I_LIFNR TYPE LIFNR
I_BUKRS TYPE BUKRS
EXPORTING E_MESSAGE TYPE CLIKE.PRIVATE SECTION.
CLASS-METHODS:
DATAX,
SET_X IMPORTING I_DATANAME TYPE CLIKE,
CLEAR.
ENDCLASS.CLASS lcl_vmd_bapi IMPLEMENTATION.
METHOD main_lfa1.
CLEAR e_err_msg.* 主数据
cgr_vend-header-object_task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_vend-header-object_instance-lifnr = i_lifnr."供应商编号
cgr_vend-central_data-central-data-ktokk = 'Z300'. "供应商帐户组* 地址数据
cgr_vend-central_data-address-task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_vend-central_data-address-postal-data-title = '0004'. "女士和先生
cgr_vend-central_data-address-postal-data-name = i_name. "供应商名称
cgr_vend-central_data-address-postal-data-langu = i_langu."SY-LANGU
cgr_vend-central_data-address-postal-data-country = i_country."'CN' 国家
cgr_vend-central_data-address-postal-data-po_box_cit = i_city."城市
cgr_vend-central_data-address-postal-data-sort1 = i_sort1."排序字段* X 字段设值
datax( ).APPEND cgr_vend TO cgt_vend.
gr_main-vendors = cgt_vend. "供应商 (Tab.) 的复杂外部接口* Create custmer
CALL METHOD vmd_ei_api=>maintain_bapi
EXPORTING
* IV_TEST_RUN = P_TEST
iv_collect_messages = 'X'
is_master_data = gr_main
IMPORTING
es_master_data_correct = ls_main1
es_message_correct = ls_mesg1
es_master_data_defective = ls_main2
es_message_defective = ls_mesg.IF ls_mesg IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.
* IMPORTING
* RETURN = LT_RETURN.* 手动更新
UPDATE lfa1
SET zz_type = '10' "供应商类型2:供应商
WHERE lifnr = i_lifnr.READ TABLE ls_main1-vendors
INTO cgr_vend
INDEX 1.
IF sy-subrc = 0.
e_lifnr = cgr_vend-header-object_instance-lifnr."供应商编号
ELSE.ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.READ TABLE ls_mesg-messages
INTO DATA(lr_message)
INDEX 1.
IF sy-subrc = 0.
e_err_msg = lr_message-message.
ELSE.ENDIF.
ENDIF.clear( ).
ENDMETHOD.METHOD main_lfb1.
CLEAR e_message.* 主数据
cgr_vend-header-object_task = 'U'. "I 创建、U 更改、D 删除、M 更改
cgr_vend-header-object_instance-lifnr = i_lifnr."供应商编码cgr_company-task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_company-data_key-bukrs = i_bukrs. "公司代码
cgr_company-data-akont = '2202060100'. "统御科目* X 字段设值
datax( ).APPEND cgr_company TO cgt_company.
cgr_vend-company_data-company = cgt_company.APPEND cgr_vend TO cgt_vend.
gr_main-vendors = cgt_vend."客户 (Tab.) 的复杂外部接口* Create custmer
CALL METHOD vmd_ei_api=>maintain_bapi
EXPORTING
* IV_TEST_RUN = P_TEST
iv_collect_messages = 'X'
is_master_data = gr_main
IMPORTING
es_master_data_correct = ls_main1
es_message_correct = ls_mesg1
es_master_data_defective = ls_main2
es_message_defective = ls_mesg.IF ls_mesg IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.READ TABLE ls_main1-vendors
INTO cgr_vend
INDEX 1.
IF sy-subrc = 0.ELSE.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
READ TABLE ls_mesg-messages
INTO DATA(lr_message)
INDEX 1.
IF sy-subrc = 0.
e_message = lr_message-message.
ELSE.ENDIF.
ENDIF.clear( ).
ENDMETHOD.METHOD datax.
set_x( 'CGR_VEND-CENTRAL_DATA-CENTRAL-DATA' ).
set_x( 'CGR_VEND-CENTRAL_DATA-ADDRESS-POSTAL-DATA' ).
ENDMETHOD.METHOD set_x.
DATA: l_ref_descr TYPE REF TO cl_abap_structdescr,
lt_components TYPE abap_compdescr_tab,
l_dataxname TYPE char200.FIELD-SYMBOLS:
<l_data> TYPE any,
<l_datax> TYPE any,
<l_datafield> TYPE any,
<l_dataxfield> TYPE any.l_dataxname = i_dataname && 'X'.
ASSIGN (i_dataname) TO <l_data>.
ASSIGN (l_dataxname) TO <l_datax>.
IF sy-subrc = 0.
l_ref_descr ?= cl_abap_typedescr=>describe_by_data( <l_data> ).
lt_components = l_ref_descr->components[].
ENDIF.LOOP AT lt_components INTO DATA(lr_component).
ASSIGN COMPONENT lr_component-name OF STRUCTURE <l_data> TO <l_datafield>.
IF <l_datafield> IS NOT INITIAL.
ASSIGN COMPONENT lr_component-name OF STRUCTURE <l_datax> TO <l_dataxfield>.
IF sy-subrc = 0.
<l_dataxfield> = abap_true.
ELSE.ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD.METHOD clear.
CLEAR:
gr_main ,
ls_main1 ,
ls_main2 ,
ls_mesg ,
ls_mesg1 ,
cgt_vend ,
cgr_vend ,
cgr_company,
cgt_company.
ENDMETHOD.
ENDCLASS.
2. 批量创建客户主数据
使用BAPI批量创建客户主数据,
先创建客户的一般主数据,再根据一般主数据扩展到公司代码层级。
CLASS lcl_cmd_bapi DEFINITION.
PUBLIC SECTION.
CLASS-DATA:
gr_main TYPE cmds_ei_main,
ls_main1 TYPE cmds_ei_main,
ls_main2 TYPE cmds_ei_main,
ls_mesg TYPE cvis_message,
ls_mesg1 TYPE cvis_message,
cgt_cust TYPE cmds_ei_extern_t,
cgr_cust TYPE cmds_ei_extern,
cgr_company TYPE cmds_ei_company,
cgt_company TYPE cmds_ei_company_t.
CLASS-METHODS:
main_kna1 IMPORTING i_ktokd TYPE ktokd
i_name TYPE name1
i_langu TYPE langu
i_country TYPE land1
i_city TYPE ort01
i_sort1 TYPE clike "AD_SORT1
i_bankl TYPE bankk OPTIONAL
i_zbank TYPE ze_zbank OPTIONAL
i_stceg TYPE stceg
i_vbund TYPE vbund
EXPORTING e_kunnr TYPE kunnr
e_message TYPE clike,
* CHANGING CR_ALV TYPE ZTFI0352,
main_knb1 IMPORTING i_kunnr TYPE kunnr
i_bukrs TYPE bukrs
EXPORTING e_message TYPE clike.
PRIVATE SECTION.
CLASS-METHODS:
datax,
set_x IMPORTING i_dataname TYPE clike,
clear.
ENDCLASS.
CLASS lcl_cmd_bapi IMPLEMENTATION.
METHOD main_kna1.
DATA: ls_banks TYPE cvis_ei_cvi_bankdetail, "银行详细数据
lt_banks TYPE cvis_ei_bankdetail_t.
* 主数据
cgr_cust-header-object_task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_cust-central_data-central-data-ktokd = i_ktokd."'0001' 客户帐户组
cgr_cust-central_data-central-data-stceg = i_stceg."'增值税登记号 "ADD C-XIAOX06 16.02.2022 10:54:22
cgr_cust-central_data-central-data-vbund = i_vbund."'增值税登记号 "ADD C-XIAOX06 16.02.2022 10:54:22
* 银行详细数据
IF i_zbank IS NOT INITIAL.
ls_banks-task = 'I'. " 删除
* LS_BANKS-DATA-BKONT = ''. " 银行账号类型
* LS_BANKS-DATAX-BKONT = 'X'.
ls_banks-data-koinh = i_name. "账户持有人
ls_banks-datax-koinh = 'X'.
ls_banks-data_key-bankn = i_zbank.
ls_banks-data_key-bankl = i_bankl.
ls_banks-data_key-banks = i_country.
IF i_zbank+18(20) IS NOT INITIAL.
ls_banks-data-bkref = i_zbank+18(20).
ls_banks-datax-bkref = 'X'.
ENDIF.
APPEND ls_banks TO lt_banks.
cgr_cust-central_data-bankdetail-bankdetails = lt_banks.
ENDIF.
* 地址数据
cgr_cust-central_data-address-task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_cust-central_data-address-postal-data-title = '0003'. "关键字 '3'公司
cgr_cust-central_data-address-postal-data-name = i_name. "客户名称
cgr_cust-central_data-address-postal-data-langu = i_langu."SY-LANGU
cgr_cust-central_data-address-postal-data-country = i_country."'CN' 国家
cgr_cust-central_data-address-postal-data-city = i_city."城市
cgr_cust-central_data-address-postal-data-sort1 = i_sort1."I_EHSLX && CR_ALV-EHSBM. "排序字段
* X 字段设值
datax( ).
APPEND cgr_cust TO cgt_cust.
gr_main-customers = cgt_cust. "客户 (Tab.) 的复杂外部接口
* Create custmer
CALL METHOD cmd_ei_api=>maintain_bapi
EXPORTING
* IV_TEST_RUN = P_TEST
iv_collect_messages = 'X'
is_master_data = gr_main
IMPORTING
es_master_data_correct = ls_main1
es_message_correct = ls_mesg1
es_master_data_defective = ls_main2
es_message_defective = ls_mesg.
IF ls_mesg IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.
* IMPORTING
* RETURN =
.
READ TABLE ls_main1-customers
INTO cgr_cust
INDEX 1.
IF sy-subrc = 0.
e_kunnr = cgr_cust-header-object_instance-kunnr."客户编号
ELSE.
ENDIF.
"更新KNBK银行账号增强字段
IF i_zbank IS NOT INITIAL.
UPDATE knbk
SET zbank = i_zbank
WHERE kunnr = e_kunnr
AND bankl = i_bankl
AND banks = i_country
AND bankn = i_zbank+0(18).
IF sy-subrc = 0.
COMMIT WORK.
ENDIF.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
READ TABLE ls_mesg-messages
INTO DATA(lr_message)
INDEX 1.
IF sy-subrc = 0.
e_message = lr_message-message.
ELSE.
ENDIF.
ENDIF.
clear( ).
ENDMETHOD.
METHOD main_knb1.
* 主数据
cgr_cust-header-object_task = 'U'. "I 创建、U 更改、D 删除、M 更改
cgr_cust-header-object_instance-kunnr = i_kunnr."客户编码
cgr_company-task = 'I'. "I 创建、U 更改、D 删除、M 更改
cgr_company-data_key-bukrs = i_bukrs. "公司代码
cgr_company-data-akont = '1122010901'. "统御科目
* X 字段设值
datax( ).
APPEND cgr_company TO cgt_company.
cgr_cust-company_data-company = cgt_company.
APPEND cgr_cust TO cgt_cust.
gr_main-customers = cgt_cust."客户 (Tab.) 的复杂外部接口
* Create custmer
CALL METHOD cmd_ei_api=>maintain_bapi
EXPORTING
* IV_TEST_RUN = P_TEST
iv_collect_messages = 'X'
is_master_data = gr_main
IMPORTING
es_master_data_correct = ls_main1
es_message_correct = ls_mesg1
es_master_data_defective = ls_main2
es_message_defective = ls_mesg.
IF ls_mesg IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.
READ TABLE ls_main1-customers
INTO cgr_cust
INDEX 1.
IF sy-subrc = 0.
ELSE.
ENDIF.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
READ TABLE ls_mesg-messages
INTO DATA(lr_message)
INDEX 1.
IF sy-subrc = 0.
e_message = lr_message-message.
ELSE.
ENDIF.
ENDIF.
clear( ).
ENDMETHOD.
METHOD datax.
set_x( 'CGR_CUST-CENTRAL_DATA-CENTRAL-DATA' ).
set_x( 'CGR_CUST-CENTRAL_DATA-ADDRESS-POSTAL-DATA' ).
ENDMETHOD.
METHOD set_x.
DATA: l_ref_descr TYPE REF TO cl_abap_structdescr,
lt_components TYPE abap_compdescr_tab,
l_dataxname TYPE char200.
FIELD-SYMBOLS:
<l_data> TYPE any,
<l_datax> TYPE any,
<l_datafield> TYPE any,
<l_dataxfield> TYPE any.
l_dataxname = i_dataname && 'X'.
ASSIGN (i_dataname) TO <l_data>.
ASSIGN (l_dataxname) TO <l_datax>.
IF sy-subrc = 0.
l_ref_descr ?= cl_abap_typedescr=>describe_by_data( <l_data> ).
lt_components = l_ref_descr->components[].
ENDIF.
LOOP AT lt_components INTO DATA(lr_component).
ASSIGN COMPONENT lr_component-name OF STRUCTURE <l_data> TO <l_datafield>.
IF <l_datafield> IS NOT INITIAL.
ASSIGN COMPONENT lr_component-name OF STRUCTURE <l_datax> TO <l_dataxfield>.
IF sy-subrc = 0.
<l_dataxfield> = abap_true.
ELSE.
ENDIF.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD clear.
CLEAR:
gr_main ,
ls_main1 ,
ls_main2 ,
ls_mesg ,
ls_mesg1 ,
cgt_cust ,
cgr_cust ,
cgr_company,
cgt_company.
ENDMETHOD.
ENDCLASS.
这篇关于SAP BAPI-创建供应商客户主数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!