【转自http://blog.163.com/arhao_h/blog/static/125169020122266922956/】
批量创建客户
方式一:
REPORT z_test NO STANDARD PAGE HEADING .
DATA:ls_kna1 TYPE kna1,
ls_knb1 TYPE knb1.
DATA:lt_upd_txt TYPE STANDARD TABLE OF fkuntxt.
DATA:lv_kunnr TYPE kunnr .
ls_kna1-kunnr = 'A20100918'.
ls_kna1-land1 = 'CN'.
ls_kna1-name1 = 'Calste'.
ls_kna1-ktokd = 'JD09'.
ls_kna1-anred = '先生'.
ls_kna1-civve = 'X'.
ls_kna1-spras = sy-langu.
*ls_knb1-kunnr = 'A20100918'.
*ls_knb1-bukrs = 'JNPC'.
*ls_knb1-akont = '2211020600'.
*ls_knb1-zuawa = '001'.
CALL FUNCTION 'SD_CUSTOMER_MAINTAIN_ALL'
EXPORTING
i_kna1 = ls_kna1
* i_knb1 = ls_knb1
I_MAINTAIN_ADDRESS_BY_KNA1 = 'X'
pi_postflag = 'X'
EXCEPTIONS
client_error = 1
kna1_incomplete = 2
knb1_incomplete = 3
knb5_incomplete = 4
knvv_incomplete = 5
kunnr_not_unique = 6
sales_area_not_unique = 7
sales_area_not_valid = 8
insert_update_conflict = 9
number_assignment_error = 10
number_not_in_range = 11
number_range_not_extern = 12
number_range_not_intern = 13
account_group_not_valid = 14
parnr_invalid = 15
bank_address_invalid = 16
tax_data_not_valid = 17
no_authority = 18
company_code_not_unique = 19
dunning_data_not_valid = 20
knb1_reference_invalid = 21
cam_error = 22
OTHERS = 23 .
IF sy-subrc <> 0.
ROLLBACK WORK.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
COMMIT WORK.
ENDIF.
方式二:
(class CMD_EI_API (Customer) and VMD_EI_API (Vendor))
REPORT z_create_customer.
DATA:lv_flg.
DATA:ls_main TYPE cmds_ei_main,
ls_mesg TYPE cvis_message,
lt_msg TYPE bapiret2_t,
lwa_msg TYPE bapiret2,
lt_cust TYPE cmds_ei_extern_t,
lwa_cust TYPE cmds_ei_extern,
lt_company TYPE cmds_ei_company_t,
lwa_company TYPE cmds_ei_company.
* 主数据
lwa_cust-header-object_task = 'I'.
lwa_cust-header-object_instance-kunnr = 'A20101241'.
lwa_cust-central_data-central-data-ktokd = 'JD09'.
lwa_cust-central_data-central-data-civve = 'X'.
lwa_cust-central_data-central-datax-ktokd = 'X'.
lwa_cust-central_data-central-datax-civve = 'X'.
* 地址数据
lwa_cust-central_data-address-task = 'I'.
lwa_cust-central_data-address-postal-data-title = '0002'.
lwa_cust-central_data-address-postal-data-name = 'JIARUI.Lv'.
lwa_cust-central_data-address-postal-data-langu = sy-langu.
lwa_cust-central_data-address-postal-data-country = 'CN'.
lwa_cust-central_data-address-postal-datax-title = 'X'.
lwa_cust-central_data-address-postal-datax-name = 'X'.
lwa_cust-central_data-address-postal-datax-langu = 'X'.
lwa_cust-central_data-address-postal-datax-country = 'X'.
* 公司代码数据
*lwa_cust-COMPANY_DATA-CURRENT_STATE = 'X'.
lwa_company-task = 'I'.
lwa_company-data_key-bukrs = 'JNPC'.
lwa_company-data-zuawa = '001'.
lwa_company-data-akont = '2211020600'.
lwa_company-datax-zuawa = 'X'.
lwa_company-datax-akont = 'X'.
APPEND lwa_company TO lt_company.
lwa_cust-company_data-company = lt_company.
APPEND lwa_cust TO lt_cust.
ls_main-customers = lt_cust.
CALL METHOD cmd_ei_api=>maintain
EXPORTING
* iv_test_run = SPACE
is_master_data = ls_main
IMPORTING
es_error = ls_mesg .
lt_msg = ls_mesg-messages.
LOOP AT lt_msg INTO lwa_msg.
WRITE: / lwa_msg-type,lwa_msg-message.
IF lwa_msg-type = 'E' OR lwa_msg-type = 'A'.
lv_flg = 'X'.
ENDIF.
ENDLOOP.
IF lv_flg IS INITIAL.
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.