在sap应用中常常会需要批量删除一些错误录入的单据,为此开发了一个小程序。该程序为了安全,程序做了一下控制
1、限制用户只能删除自己的订单,不能删除别人输入的订单,如果需要修改一下查询条件;
2、系统默认为“测试运行”,方便用户在删除订单前要核实一下订单,确认不要误操作;
程序代码如下,供大家参考!
REPORT zsde0099.
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
* data Declarations
*----------------------------------------------------------------------*
TABLES: vbak. " Sales Document: Header Data
*----------------------------------------------------------------------*
* table control Output Declarations
*----------------------------------------------------------------------*
TYPES: BEGIN OF tc_0100,
sel(1), " Check box
vbeln LIKE vbak-vbeln, " Sales Order Number
erdat LIKE vbak-erdat, " Sales order creation date
ernam LIKE vbak-ernam,
kunnr LIKE likp-kunag, " Sold-to Party
name1 LIKE kna1-name1, " Sold-to Company Name
text(200),
END OF tc_0100.
DATA: wa_tc_0100 TYPE tc_0100,
it_tc_0100 TYPE tc_0100 OCCURS 0 WITH HEADER LINE.
DATA g_fieldcat TYPE slis_t_fieldcat_alv.
*----------------------------------------------------------------------*
* Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK params1 WITH FRAME TITLE text-001.
PARAMETER: s_vkorg LIKE vbak-vkorg DEFAULT ‘6001‘ OBLIGATORY MODIF ID s1. " " Sales Org.
SELECT-OPTIONS:
s_vtweg FOR vbak-vtweg OBLIGATORY, " Distribution Channel
s_spart FOR vbak-spart OBLIGATORY DEFAULT ‘10‘, " Division
s_auart FOR vbak-auart OBLIGATORY, " Order type
s_erdat FOR vbak-erdat OBLIGATORY, " Sales order creation date
* s_ERNAM FOR vbak-ERNAM OBLIGATORY , " Sales order creation date
s_kunnr FOR vbak-kunnr , " Sold-to party
s_vbeln FOR vbak-vbeln . " Sales Order
PARAMETER p_test AS CHECKBOX DEFAULT ‘X‘.
SELECTION-SCREEN END OF BLOCK params1.
***********************************************************************
*INITIALIZATION.
***********************************************************************
INITIALIZATION.
s_auart-sign = ‘I‘.
s_auart-option = ‘EQ‘.
s_auart-low = ‘ZC01‘.
APPEND s_auart.
s_auart-low = ‘ZC03‘.
APPEND s_auart.
s_auart-low = ‘ZC06‘.
APPEND s_auart.
s_auart-low = ‘ZC20‘.
APPEND s_auart.
s_vtweg-sign = ‘I‘.
s_vtweg-option = ‘EQ‘.
s_vtweg-low = ‘10‘.
APPEND s_vtweg.
s_vtweg-low = ‘20‘.
APPEND s_vtweg.
s_vtweg-low = ‘30‘.
APPEND s_vtweg.
s_vtweg-low = ‘40‘.
APPEND s_vtweg.
*----------------------------------------------------------------------*
* Selection Events Processing
*----------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT.
PERFORM screen_check.
AT SELECTION-SCREEN.
CLEAR: it_tc_0100, wa_tc_0100.
REFRESH: it_tc_0100.
START-OF-SELECTION.
PERFORM extract_data.
END-OF-SELECTION.
PERFORM change_sales_orders.
PERFORM display_mes.
*&---------------------------------------------------------------------*
*& Form SCREEN_CHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM screen_check.
DATA: l_line TYPE i .
DESCRIBE TABLE s_erdat LINES l_line.
IF l_line EQ 0.
s_erdat-sign = ‘I‘.
s_erdat-option = ‘BT‘.
s_erdat-high = sy-datum.
s_erdat-low = sy-datum.
APPEND s_erdat.
ENDIF.
DESCRIBE TABLE s_vtweg LINES l_line.
IF l_line EQ 0.
ENDIF.
ENDFORM. "SCREEN_CHECK
*&---------------------------------------------------------------------*
*& Form extract_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM extract_data.
DATA: lt_it1 LIKE it_tc_0100 OCCURS 0 WITH HEADER LINE,
l_chk(1). " for exclude incomplete SO
SELECT a~vbeln a~erdat a~ernam a~kunnr INTO CORRESPONDING FIELDS OF TABLE lt_it1
FROM vbak AS a
WHERE a~vbeln IN s_vbeln
AND a~auart IN s_auart
AND a~vkorg EQ s_vkorg
AND a~vtweg IN s_vtweg
AND a~spart IN s_spart
AND a~erdat IN s_erdat
AND a~ernam = sy-uname
AND a~kunnr IN s_kunnr.
LOOP AT lt_it1.
* get sold-to party company name
PERFORM get_customer_company_name USING lt_it1-kunnr CHANGING lt_it1-name1.
MODIFY lt_it1.
ENDLOOP.
it_tc_0100[] = lt_it1[].
ENDFORM. "extract_data
*&---------------------------------------------------------------------*
*& Form GET_CUSTOMER_COMPANY_NAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->F_KUNNR text
* -->F_NAME text
*----------------------------------------------------------------------*
FORM get_customer_company_name USING f_kunnr CHANGING f_name.
CLEAR f_name.
SELECT SINGLE name1 INTO f_name
FROM kna1
WHERE kunnr = f_kunnr.
ENDFORM. "GET_CUSTOMER_COMPANY_NAME
*&---------------------------------------------------------------------*
*& Form change_sales_orders
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM change_sales_orders.
DATA: lt_order_header_in LIKE bapisdh1,
lt_order_header_inx LIKE bapisdh1x,
lt_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
lt_order_item_in LIKE bapisditm OCCURS 0 WITH HEADER LINE,
lt_order_item_inx LIKE bapisditmx OCCURS 0 WITH HEADER LINE,
cp_eind(1).
SORT it_tc_0100 BY vbeln.
DELETE ADJACENT DUPLICATES FROM it_tc_0100.
LOOP AT it_tc_0100.
CLEAR: lt_order_header_inx,lt_order_item_in, lt_order_item_inx, lt_return,cp_eind.
REFRESH: lt_order_item_in, lt_order_item_inx, lt_return.
lt_order_header_inx-updateflag = ‘D‘.
IF p_test NE ‘X‘.
CALL FUNCTION ‘BAPI_SALESORDER_CHANGE‘
EXPORTING
salesdocument = it_tc_0100-vbeln
* ORDER_HEADER_IN = LT_ORDER_HEADER_IN
order_header_inx = lt_order_header_inx
TABLES
return = lt_return.
* 处理错误消息:通过判断消息的类型,来判断BAPI是否成功
LOOP AT lt_return .
IF lt_return-type EQ ‘E‘ OR
lt_return-type = ‘A‘ OR
lt_return = ‘X‘.
cp_eind = ‘X‘. "失败
it_tc_0100-text = lt_return-message.
ENDIF.
ENDLOOP.
IF cp_eind NE ‘X‘.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT‘
EXPORTING
wait = ‘X‘.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK‘.
ENDIF.
ENDIF.
IF cp_eind NE ‘X‘ AND sy-subrc = 0.
IF p_test = ‘X‘.
it_tc_0100-text = ‘Test successfully.‘.
ELSE.
it_tc_0100-text = ‘Delete successfully.‘.
ENDIF.
ELSE.
CONCATENATE it_tc_0100-text ‘---‘ ‘Delete error.‘ into it_tc_0100-text.
ENDIF.
MODIFY it_tc_0100.
ENDLOOP.
ENDFORM. "change_sales_orders
*&---------------------------------------------------------------------*
*& Form init_fieldedi
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_P_FIELDTAB text
*----------------------------------------------------------------------*
FORM init_fieldedi USING p_p_fieldtab TYPE slis_t_fieldcat_alv.
DATA: l_fieldcat TYPE slis_fieldcat_alv.
REFRESH p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘VBELN‘.
l_fieldcat-seltext_m = ‘ORDER NUMBER‘.
l_fieldcat-outputlen = 10.
APPEND l_fieldcat TO p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘ERDAT‘.
l_fieldcat-seltext_m = ‘Create Date‘.
l_fieldcat-ref_fieldname = ‘ERDAT‘.
l_fieldcat-ref_tabname = ‘VBAK‘.
l_fieldcat-outputlen = 10.
APPEND l_fieldcat TO p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘ERNAM‘.
l_fieldcat-seltext_m = ‘Input Person‘.
l_fieldcat-ref_fieldname = ‘ERDAT‘.
l_fieldcat-ref_tabname = ‘VBAK‘.
l_fieldcat-outputlen = 10.
APPEND l_fieldcat TO p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘KUNNR‘.
l_fieldcat-seltext_m = ‘Sold Code‘.
l_fieldcat-ref_fieldname = ‘KUNNR‘.
l_fieldcat-ref_tabname = ‘VBAK‘.
l_fieldcat-outputlen = 10.
APPEND l_fieldcat TO p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘NAME1‘.
l_fieldcat-seltext_m = ‘Sold Name1‘.
l_fieldcat-outputlen = 35.
APPEND l_fieldcat TO p_p_fieldtab.
CLEAR l_fieldcat.
l_fieldcat-fieldname = ‘TEXT‘.
l_fieldcat-seltext_m = ‘Message‘.
l_fieldcat-outputlen = 120.
APPEND l_fieldcat TO p_p_fieldtab.
ENDFORM. " INITIALIZE_FIELDEDI
*&---------------------------------------------------------------------*
*& Form display_mes
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM display_mes .
PERFORM init_fieldedi USING g_fieldcat[].
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY‘
EXPORTING
i_callback_program = sy-repid
it_fieldcat = g_fieldcat[]
i_save = ‘A‘
i_default = ‘A‘
TABLES
t_outtab = it_tc_0100.
ENDFORM. " display_mes