bapi:CO_XT_COMPONENT_ADD
部分参考代码:
DATA: LS_STORAGE_LOCATION TYPE COXT_S_STORAGE_LOCATION,
LS_STORAGE_LOCATIONX TYPE COXT_S_STORAGE_LOCATIONX,
LS_REQU_QUAN TYPE COXT_S_QUANTITY,
LV_OPERATION TYPE CO_APLZL,
LV_BATCH TYPE COXT_BATCH,
LV_BATCHX TYPE COXT_BATCHX,
LV_POSTP TYPE POSTP,
LV_SEQUENCE TYPE PLNFOLGE,
LV_MATERIAL TYPE MATNR,
LV_POSITIONNO TYPE POSITIONNO,
LV_NUMC TYPE NUMC4.
DATA:LV_UNIT TYPE T006-ISOCODE.
DATA:LV_TABIX TYPE SY-TABIX .
DATA:LS_RETURN TYPE COXT_BAPIRETURN.
DATA:LV_ERROR TYPE C.
TYPES: BEGIN OF TY_RESB_BT.
INCLUDE TYPE RESBB.
TYPES: INDOLD TYPE SYST_TABIX.
TYPES: NO_REQ_UPD TYPE SYST_DATAR.
TYPES: END OF TY_RESB_BT.
TYPES TT_RESB_BT TYPE TABLE OF TY_RESB_BT.
FIELD-SYMBOLS: <FT_RESB_BT> TYPE TT_RESB_BT,
<FS_RESB_BT> TYPE TY_RESB_BT.
SORT PT_ORD BY WERKS PROJN ZPROJPERS AUART AUFNR ZDELFLAG PLNBEZ MATNR.
DATA:LV_INDEX TYPE SY-TABIX.
DATA:LV_QUANTITY LIKE AFKO-GAMNG.
LV_QUANTITY = 0.
LOOP AT PT_ORD INTO DATA(LS_ORD).
LV_INDEX = SY-TABIX.
LV_QUANTITY = LV_QUANTITY + LS_ORD-BDMNG.
AT END OF MATNR.
READ TABLE PT_ORD INTO LS_ORD INDEX LV_INDEX.
LS_STORAGE_LOCATION-WERKS = LS_ORD-WERKS.
LS_STORAGE_LOCATIONX-WERKS = 'X'.
LS_REQU_QUAN-QUANTITY = LV_QUANTITY." LS_ORD-BDMNG.
LS_REQU_QUAN-UOM = LS_ORD-MEINS.
LV_POSITIONNO = '0010'.
LV_POSTP = 'L'.
LV_MATERIAL = LS_ORD-MATNR.
SELECT SINGLE AUFNR, AUFPL INTO @DATA(LS_AFKO)
FROM AFKO
WHERE AUFNR = @PV_AUFNR.
IF SY-SUBRC EQ 0.
* Fetch operation to which it has to be assigned
SELECT SINGLE AUFPL, APLZL, PLNFL INTO @DATA(LS_AFVC)
FROM AFVC
WHERE AUFPL = @LS_AFKO-AUFPL.
IF SY-SUBRC EQ 0.
LV_OPERATION = LS_AFVC-APLZL.
LV_SEQUENCE = LS_AFVC-PLNFL.
ENDIF.
ENDIF.
CALL FUNCTION 'CO_XT_COMPONENT_ADD'
EXPORTING
IS_ORDER_KEY = PV_AUFNR
I_MATERIAL = LV_MATERIAL
IS_REQU_QUAN = LS_REQU_QUAN
I_OPERATION = LV_OPERATION
I_SEQUENCE = LV_SEQUENCE
IS_STORAGE_LOCATION = LS_STORAGE_LOCATION
IS_STORAGE_LOCATIONX = LS_STORAGE_LOCATIONX
I_BATCH = LV_BATCH
I_BATCHX = LV_BATCHX
I_POSTP = LV_POSTP
I_POSNO = LV_POSITIONNO
IMPORTING
ES_BAPIRETURN = LS_RETURN
E_ERROR_OCCURRED = LV_ERROR.
IF LV_ERROR = SPACE.
CLEAR: LV_NUMC,
LS_RETURN.
* Modify POSNR via ASSIGN before DB update to correct the blank
* item number in Components due to incompatible types of I_POSNO
* (type CIF_R3RES-POSITIONNO) and RESB-POSNR
ASSIGN ('(SAPLCOBC)RESB_BT[]') TO <FT_RESB_BT>.
LOOP AT <FT_RESB_BT> ASSIGNING <FS_RESB_BT>.
LV_NUMC = SY-TABIX * 10.
<FS_RESB_BT>-POSNR = LV_NUMC.
CLEAR LV_NUMC.
ENDLOOP.
IF <FT_RESB_BT> IS NOT INITIAL.
<FS_RESB_BT>-SANKA = LS_ORD-SANKA."最后一行赋值到RESB刚好是需要修改的这条数据-成本核算相关性
<FS_RESB_BT>-SCHGT = LS_ORD-SCHGT."最后一行赋值到RESB刚好是需要修改的这条数据-散装物料
<FS_RESB_BT>-BEIKZ = LS_ORD-BEIKZ."最后一行赋值到RESB刚好是需要修改的这条数据-物料供应标识符
LS_ORD-RSNUM = <FS_RESB_BT>-RSNUM. "刚好取到最后一行赋值到RESB中
LS_ORD-RSPOS = <FS_RESB_BT>-RSPOS.
ENDIF.
* Commit transaction
CALL FUNCTION 'CO_XT_ORDER_PREPARE_COMMIT'
IMPORTING
ES_BAPIRETURN = LS_RETURN
E_ERROR_OCCURRED = LV_ERROR.
IF ( LS_RETURN-TYPE = 'S' OR
LS_RETURN-TYPE = 'W' OR
LS_RETURN-TYPE = 'I' ) OR
LS_RETURN IS INITIAL.
* Commit data
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
CALL FUNCTION 'CO_XT_ORDER_INITIALIZE'.
WAIT UP TO '0.1' SECONDS.
LS_ORD-STATUS = 'S'.
LS_ORD-MESSAGE = '组件添加成功:' && LS_ORD-MATNR.
ELSE.
* Data Rollback
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LS_ORD-STATUS = 'E'.
LS_ORD-MESSAGE = '组件添加失败:' && LS_ORD-MATNR && LS_RETURN-MESSAGE.
CLEAR: LV_ERROR,
LS_RETURN.
ENDIF.
ELSE.
* Data Rollback
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
LS_ORD-STATUS = 'E'.
LS_ORD-MESSAGE = '组件添加失败:' && LS_ORD-MATNR && LS_RETURN-MESSAGE.
CLEAR: LV_ERROR,
LS_RETURN.
ENDIF.
MODIFY PT_ORD FROM LS_ORD.
CLEAR LS_ORD.
LV_QUANTITY = 0.
ENDAT.
ENDLOOP.
PV_STATUS = LS_ORD-STATUS.
IF PV_STATUS = 'S'.
PV_MSG = '组件添加完成'.
ELSE.
PV_MSG = '组件添加失败'.
ENDIF.