其实这个函数就是一个BDC录屏,由三段组成
POSTING_INTERFACE_START
POSTING_INTERFACE_DOCUMENT
POSTING_INTERFACE_END
我写了一个函数来对应这三个函数完成凭证的自动生成
function zctest_03.
*"----------------------------------------------------------------------
*"*"局部接口:
*" IMPORTING
*" VALUE(ZBAPI_F_02_HEADER) LIKE ZBAPI_F_02_HEADER STRUCTURE
*" ZBAPI_F_02_HEADER OPTIONAL
*" EXPORTING
*" VALUE(MESSAGE) TYPE CHAR300
*" TABLES
*" ZBAPI_F_02_POSNR STRUCTURE ZBAPI_F_02_POSNR OPTIONAL
*" IT_RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
data: ls_ftpost type ftpost,
lt_ftpost type table of ftpost,
lt_fttax type table of fttax,
lt_blntab type table of blntab,
lv_subrc type sy-subrc,
lv_msg type bapiret2.
data:lv_count(3) type c.
data:lv_wrbtr(16) type c.
data:me1(30),me2(30),me3(30).
data:git_dfies type standard table of dfies with header line.
call function 'POSTING_INTERFACE_START'
exporting
i_function = 'C' "For Batch Input.
i_group = 'F-02' "Name of the session for creation
i_keep = 'X' "Retain the session
i_user = sy-uname. "User name
define macro.
ls_ftpost-fnam = &1.
ls_ftpost-fval = &2.
append ls_ftpost to lt_ftpost.
end-of-definition.
"header:
ls_ftpost-stype = 'K'. "Header indicator
ls_ftpost-count = '001'. "First item
macro: 'BKPF-BLDAT' zbapi_f_02_header-bldat,
'BKPF-BLART' zbapi_f_02_header-blart,
'BKPF-BUKRS' zbapi_f_02_header-bukrs,
'BKPF-BUDAT' zbapi_f_02_header-budat,
'BKPF-BKTXT' zbapi_f_02_header-bktxt.
"items:
lv_count = '001'.
loop at zbapi_f_02_posnr.
ls_ftpost-stype = 'P'.
ls_ftpost-count = lv_count.
lv_wrbtr = zbapi_f_02_posnr-wrbtr.
macro: 'RF05A-NEWBS' zbapi_f_02_posnr-newbs,
'RF05A-NEWKO' zbapi_f_02_posnr-newko,
'BSEG-WRBTR' lv_wrbtr,
'BSEG-SGTXT' zbapi_f_02_posnr-sgtxt.
if zbapi_f_02_posnr-kostl ne ''.
macro:'COBL-KOSTL' zbapi_f_02_posnr-kostl.
endif.
if zbapi_f_02_posnr-prctr ne ''.
macro:'COBL-PRCTR' zbapi_f_02_posnr-prctr.
endif.
if zbapi_f_02_posnr-projk ne ''.
macro:'COBL-PS_POSID' zbapi_f_02_posnr-projk.
endif.
if zbapi_f_02_posnr-kdauf ne ''.
macro:'COBL-KDAUF' zbapi_f_02_posnr-kdauf.
endif.
if zbapi_f_02_posnr-kdpos ne ''.
macro:'COBL-KDPOS' zbapi_f_02_posnr-kdpos.
endif.
if zbapi_f_02_posnr-fkber ne ''.
macro:'COBL-FKBER' zbapi_f_02_posnr-fkber.
endif.
if zbapi_f_02_posnr-zuonr ne ''.
macro:'BSEG-ZUONR' zbapi_f_02_posnr-zuonr.
endif.
if zbapi_f_02_posnr-newbs+1(1) = '9'.
macro: 'RF05A-NEWUM' zbapi_f_02_posnr-newum.
if zbapi_f_02_posnr-newum = 'X'.
macro: 'BSEG-ZFBDT' zbapi_f_02_posnr-zfbdt. "建议模板增加基准日期字段
endif.
endif.
if zbapi_f_02_posnr-newbs = '70' .
macro: 'RF05A-NEWBW' '100'.
elseif zbapi_f_02_posnr-newbs = '75' .
macro: 'RF05A-NEWBW' '101'.
endif.
if zbapi_f_02_posnr-newko between '0010010000' and '00100299999'.
macro: 'BSEG-RSTGR' zbapi_f_02_posnr-rstgr.
endif.
lv_count = lv_count + 1.
endloop.
call function 'POSTING_INTERFACE_DOCUMENT'
exporting
i_tcode = 'FB01' "Name of t-code for Posting
i_sgfunct = 'C' "Batch input session
importing
e_subrc = lv_subrc
e_msgid = lv_msg-id "Error handling
e_msgty = lv_msg-type "Error handling
e_msgno = lv_msg-number "Error handling
e_msgv1 = lv_msg-message_v1 "Error handling
e_msgv2 = lv_msg-message_v2 "Error handling
e_msgv3 = lv_msg-message_v3 "Error handling
e_msgv4 = lv_msg-message_v4 "Error handling
tables
t_ftpost = lt_ftpost "Internal table populated in Step 2
t_fttax = lt_fttax "Relevant for Tax calculation manually
t_blntab = lt_blntab "Relevant only for Call Trans…
exceptions
others = 1.
call function 'POSTING_INTERFACE_END'.
if sy-subrc = 0 .
if lv_msg-type = 'S'.
concatenate '' lv_msg-message_v1 into message.
elseif lv_msg-type = 'E'.
split lv_msg-message_v1 at '-' into me1 me2.
condense me1 no-gaps.
condense me2 no-gaps.
call function 'DDIF_FIELDINFO_GET'
exporting
tabname = me1
fieldname = me2
langu = sy-langu
tables
dfies_tab = git_dfies[]
exceptions
not_found = 1
internal_error = 2
others = 3.
if git_dfies[] is not initial.
loop at git_dfies from 1.
message = git_dfies-fieldtext.
exit.
endloop.
concatenate message '字段错误!' into message.
else.
message id lv_msg-id type lv_msg-type number lv_msg-number into message.
endif.
endif.
endif.
endfunction.
其中DDIF_FIELDINFO_GET可以获取数据库表的字段描述,方便回传错误信息。