针对大量的历史项目进行“技术性完成”系统状态设置,手工一个个操作显然不现实,那批量操作用程序怎么实现呢?
一开始我想用BDC录屏,简简单单实现了功能就好,代码也很简单
PERFORM bdc_dynpro USING 'SAPLCNPB_M' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE' '=OPEN'.
PERFORM bdc_dynpro USING 'SAPLCNPB_W' '0900'.
PERFORM bdc_field USING 'BDC_OKCODE' '=OK'.
PERFORM bdc_field USING 'CNPB_W_ADD_OBJ_DYN-PROJ_EXT' ls_alv-pspid.
PERFORM bdc_dynpro USING 'SAPLCNPB_M' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE' '=END'.
PERFORM bdc_dynpro USING 'SAPLCNPB_M' '1000'.
PERFORM bdc_field USING 'BDC_OKCODE' '=SAVE'.
但是遇到了问题,好多项目会有订单未完成,会提示红色错误消息,批导就自动结束了
只能去找BAPI,搜了下资料,找到了函数“BAPI_BUS2001_SET_STATUS”。这个函数就是专门用来修改项目定义的系统状态和用户状态的。如果只是用这个函数并不能成功更新项目系统状态,需要结合“BAPI_PS_INITIALIZATION”、“BAPI_PS_PRECOMMIT”等函数,下面是参考代码
FORM frm_save_data .
DATA: lt_alv TYPE TABLE OF ty_alv,
ls_alv TYPE ty_alv,
lv_error(40) TYPE c,
lv_objnr TYPE jest-objnr,
lv_re TYPE bsvx-sttxt,
lt_return2 LIKE TABLE OF bapiret2,
lt_return LIKE TABLE OF bapireturn1,
ls_return2 LIKE bapiret2,
ls_return LIKE bapireturn1,
lt_result LIKE TABLE OF bapi_status_result,
ls_result LIKE bapi_status_result.
lt_alv = gt_alv.
DELETE lt_alv WHERE status <> icon_yellow_light OR zsel = ''.
IF lt_alv IS INITIAL.
"请检查消息字段
MESSAGE s018(zfi) DISPLAY LIKE 'E'.
RETURN.
ENDIF.
LOOP AT lt_alv INTO ls_alv.
CALL FUNCTION 'BAPI_PS_INITIALIZATION'.
CALL FUNCTION 'BAPI_BUS2001_SET_STATUS'
EXPORTING
project_definition = ls_alv-pspid
* UNDO_SYSTEM_STATUS =
* UNDO_USER_STATUS =
set_system_status = 'TECO'
* SET_USER_STATUS =
IMPORTING
return = ls_return
TABLES
e_result = lt_result.
IF ls_return-type CA 'EA'.
LOOP AT lt_result INTO ls_result.
ls_alv-status = icon_red_light.
IF ls_alv-mesg IS INITIAL.
ls_alv-mesg = ls_result-message_text.
ELSE.
CONCATENATE ls_alv-mesg ls_result-message_text INTO ls_alv-mesg SEPARATED BY '/'.
ENDIF.
ENDLOOP.
CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
EXPORTING
i_precommit_ok = 'Y'.
MODIFY gt_alv FROM ls_alv TRANSPORTING status mesg pstus
WHERE zindex = ls_alv-zindex.
CONTINUE.
ENDIF.
CALL FUNCTION 'BAPI_PS_PRECOMMIT'
TABLES
et_return = lt_return2.
LOOP AT lt_return INTO ls_return WHERE type CA 'AE'.
ls_alv-status = icon_red_light.
MESSAGE ID ls_return-id TYPE ls_return-type NUMBER ls_return-number
INTO lv_error
WITH ls_return-message_v1 ls_return-message_v2
ls_return-message_v3 ls_return-message_v4.
IF ls_alv-mesg IS INITIAL.
ls_alv-mesg = lv_error.
ELSE.
CONCATENATE ls_alv-mesg lv_error INTO ls_alv-mesg SEPARATED BY '/'.
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
return = ls_return2.
CALL FUNCTION 'PS_FLAG_INIT_GLOBAL_FLAGS'.
CALL FUNCTION 'PS_FLAG_SET_GLOBAL_FLAGS'
EXPORTING
i_precommit_ok = 'Y'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
IMPORTING
return = ls_return2.
* 项目定义状态更新成功
ls_alv-status = icon_green_light.
"取当前项目状态
lv_objnr = ls_alv-objnr.
CLEAR lv_re.
CALL FUNCTION 'STATUS_TEXT_EDIT'
EXPORTING
objnr = lv_objnr
spras = sy-langu
IMPORTING
line = lv_re
EXCEPTIONS
object_not_found = 1
OTHERS = 2.
ls_alv-pstus = lv_re.
ENDIF.
MODIFY gt_alv FROM ls_alv TRANSPORTING status mesg pstus
WHERE zindex = ls_alv-zindex.
ENDLOOP.
"请检查消息字段
MESSAGE s018(zfi).
ENDFORM. " FRM_SAVE_DATA