sd--Bulk Delete Orders

Source: Internet
Author: User

In SAP applications, it is often necessary to delete some erroneous entry documents in bulk, and a small program is developed for this purpose. The program has done a bit of control for security.

1, limit users can only delete their own orders, can not delete the orders entered by others, if necessary to modify the query conditions;

2, the system default is "test Run", convenient for users to check the order before deleting the order, confirm do not mistakenly operation;

The program code is as follows, for everyone reference!

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, "the 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 ' ten ', ' 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, "the Sold-to Party
S_vbeln for Vbak-vbeln. The 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 to 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.
* Handling Error messages: Determine if BAPI is successful by judging the type of message
LOOP at Lt_return.
IF lt_return-type EQ ' E ' OR
Lt_return-type = ' A ' OR
Lt_return = ' X '.
Cp_eind = ' X '. "Failure
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

sd--Bulk Delete Orders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.