详细看这个:
http://note.youdao.com/share/?id=d626d08f8e6923074d43ed8e1b36ffbe&type=note
http://blog.csdn.net/feijibin/article/details/13279077
其中 最后的代码例子需要更改一下,
代码例子为:
method IF_EX_ACC_DOCUMENT~CHANGE. DATA: wa_extension TYPE bapiparex,
ext_value(960) TYPE c,
wa_accit TYPE accit,
l_ref TYPE REF TO data.
FIELD-SYMBOLS: <l_struc> TYPE ANY,
<l_field> TYPE ANY.
SORT c_extension2 BY structure.
LOOP AT c_extension2 INTO wa_extension.
AT NEW structure.
CREATE DATA l_ref TYPE (wa_extension-structure).
ASSIGN l_ref->* TO <l_struc>.
ENDAT.
CONCATENATE wa_extension-valuepart1 wa_extension-valuepart2
wa_extension-valuepart3 wa_extension-valuepart4
INTO ext_value.
MOVE ext_value TO <l_struc>.
ASSIGN COMPONENT 'POSNR' OF STRUCTURE <l_struc> TO <l_field>.
READ TABLE c_accit WITH KEY posnr = <l_field>
INTO wa_accit.
IF sy-subrc IS INITIAL.
MOVE-CORRESPONDING <l_struc> TO wa_accit.
MODIFY c_accit FROM wa_accit INDEX sy-tabix.
ENDIF.
ENDLOOP.
endmethod.
然后就是在程序中添加代码:
程序中的代码为:
DATA:lv_budat LIKE bkpf-budat.
DATA:lv_tabix LIKE sy-tabix.
DATA:LV_STRLEN TYPE char02.
DATA:wa_exten LIKE zextension.
DATA:lv_belnr LIKE bkpf-belnr.
DATA:lv_message TYPE string.
DATA:BEGIN OF lt_zzrcb OCCURS 0,
number TYPE c,
zzrcb LIKE zco010-zzrcb,
END OF lt_zzrcb.
DATA:lw_documentheader LIKE bapiache09,
lw_accountgl LIKE bapiacgl09,
lt_accountgl LIKE TABLE OF bapiacgl09,
lw_currencyamount LIKE bapiaccr09,
lt_currencyamount LIKE TABLE OF bapiaccr09,
lt_extension2 LIKE TABLE OF bapiparex WITH HEADER LINE,
lt_return LIKE TABLE OF bapiret2 WITH HEADER LINE.
CLEAR: lv_budat,lw_documentheader,lw_accountgl,lw_currencyamount,lv_tabix.
REFRESH:lt_accountgl,lt_currencyamount,lt_extension2,lt_return,lt_zzrcb.
CONCATENATE p_gjahr p_monat INTO lv_budat.
CONDENSE lv_budat NO-GAPS.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = lv_budat
IMPORTING
last_day_of_month = lw_documentheader-doc_date
EXCEPTIONS
day_in_no_date = 1
OTHERS = 2.
lw_documentheader-pstng_date = lw_documentheader-doc_date.
lw_documentheader-comp_code = p_bukrs.
lw_documentheader-username = sy-uname.
lw_documentheader-fisc_year = p_gjahr.
lw_documentheader-fis_period = p_monat.
lw_documentheader-doc_type = 'SA'.
LOOP AT gt_display INTO gw_display.
lv_strlen = strlen( gw_display-zkwbm ).
IF lv_strlen = '10'.
lv_tabix = lv_tabix + 1.
lw_accountgl-itemno_acc = lv_tabix.
lw_accountgl-gl_account = gw_display-zkwbm.
lw_accountgl-comp_code = p_bukrs.
lw_accountgl-plant = p_werks.
lw_accountgl-fis_period = p_monat.
lw_accountgl-fisc_year = p_gjahr.
lw_accountgl-orderid = p_aufnr.
APPEND lw_accountgl TO lt_accountgl.
CLEAR lw_accountgl.
lw_currencyamount-itemno_acc = lv_tabix.
lw_currencyamount-amt_doccur = gw_display-zzrcb.
lw_currencyamount-currency = 'CNY'.
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR lw_currencyamount.
wa_exten-posnr = lv_tabix.
wa_exten-bschl = '40'.
lt_extension2-structure = 'ZEXTENSION'.
lt_extension2-valuepart1 = wa_exten.
APPEND lt_extension2.CLEAR lt_extension2.
lt_zzrcb-number = '1'.
lt_zzrcb-zzrcb = gw_display-zzrcb.
COLLECT lt_zzrcb.CLEAR lt_zzrcb.
ENDIF.
ENDLOOP.
lv_tabix = lv_tabix + 1.
lw_accountgl-itemno_acc = lv_tabix.
lw_accountgl-gl_account = '0099999906'.
lw_accountgl-comp_code = p_bukrs.
lw_accountgl-plant = p_werks.
lw_accountgl-fis_period = p_monat.
lw_accountgl-fisc_year = p_gjahr.
APPEND lw_accountgl TO lt_accountgl.
CLEAR lw_accountgl.
READ TABLE lt_zzrcb INDEX 1.
IF sy-subrc = 0.
lw_currencyamount-itemno_acc = lv_tabix.
lw_currencyamount-amt_doccur = 0 - lt_zzrcb-zzrcb.
lw_currencyamount-currency = 'CNY'.
APPEND lw_currencyamount TO lt_currencyamount.
CLEAR lw_currencyamount.
ENDIF.
wa_exten-posnr = lv_tabix.
wa_exten-bschl = '50'.
lt_extension2-structure = 'ZEXTENSION'.
lt_extension2-valuepart1 = wa_exten.
APPEND lt_extension2.CLEAR lt_extension2.
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
documentheader = lw_documentheader
* CUSTOMERCPD =
* CONTRACTHEADER =
* IMPORTING
* OBJ_TYPE =
* OBJ_KEY =
* OBJ_SYS =
TABLES
accountgl = lt_accountgl
* ACCOUNTRECEIVABLE =
* ACCOUNTPAYABLE =
* ACCOUNTTAX =
currencyamount = lt_currencyamount
* CRITERIA =
* VALUEFIELD =
* EXTENSION1 =
return = lt_return
* PAYMENTCARD =
* CONTRACTITEM =
extension2 = lt_extension2.
* REALESTATE =
* ACCOUNTWT =
COMMIT WORK.
IF sy-subrc = 0.
READ TABLE lt_return INDEX 1.
IF lt_return-type = 'S'.
lv_belnr = lt_return-message_v2+0(10).
CONCATENATE lv_belnr '创建成功!' INTO lv_message.
MESSAGE lv_message TYPE 'S'.
ELSE.
MESSAGE lt_return-message TYPE 'E'.
ENDIF.
ELSE.
MESSAGE lt_return-message TYPE 'E'.
ENDIF.
其中ZEXTENSION还需要在SE11中定义,就是一个简单的结构posnr是必须有的,他是会计凭证行项目编号,另一个就是自己的需要添加的字段。