A Code for Deletion of Open Sales Orders

How to Mass Delete the Open Sales Order in SAP?

Here is a code for deletion of open sap sales orders. You can use it . The flat file should have only the sales order nos. The program works a bit slow.  Sorry about that,  it is a session BDC.

REPORT ZSODEL  NO STANDARD PAGE HEADING.
TABLES : VBAK,VBFA.
PARAMETERS: SORG LIKE VBAK-VKORG OBLIGATORY,
SOFF LIKE VBAK-VKBUR OBLIGATORY, ERNAME LIKE VBAK-ERNAM OBLIGATORY.
SELECT-OPTIONS POST FOR VBAK-ERDAT OBLIGATORY.
PARAMETERS DOTYP LIKE VBAK-AUART OBLIGATORY.
PARAMETER PSES  LIKE APQI-GROUPID OBLIGATORY.
PARAMETER TEST AS CHECKBOX DEFAULT 'x'.
DATA : DATE LIKE SY-DATUM.
DATA: RCOUNT(10) TYPE C.
DATA: ERR(10) TYPE C.
DATA: RC TYPE I VALUE 0.
DATE = SY-DATUM - 1.
DATA : IBDC LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
DATA :  ITAB1 LIKE VBAK OCCURS 0 WITH HEADER LINE.
DATA : BEGIN OF ITAB OCCURS 0,
       VBELN LIKE  VBAK-VBELN,
        NETWR LIKE VBAK-NETWR,
        FLAG1 TYPE C.
DATA:END OF ITAB.
DATA: FLAG  TYPE C .
DATA: MSG(120) TYPE C.
DATA: KEY LIKE VBAK-VBELN.
*****************************
TOP-OF-PAGE.
  IF ERR NE  0.                        "TO REVERT.
    WRITE :'Mismatch with the flat file and the parametar data.'.
    ULINE.
  ELSE.
    WRITE :RC,'Sales Order Uploaded from WorkStation.'.
  ENDIF.
******************************
*CHECKING VBAK WITH PARAMETER DATA
START-OF-SELECTION.
  SELECT * FROM  VBAK INTO CORRESPONDING FIELDS OF TABLE ITAB1 WHERE
   VKORG = SORG AND ERDAT IN POST AND AUART = DOTYP AND ERNAM = ERNAME
 AND VKBUR = SOFF.
  CLEAR ITAB1.
  IF SY-SUBRC NE '0' .
    MESSAGE E000(ZV) WITH 'No record found for the selection criteria' .
  ENDIF.
  REFRESH ITAB.
  CLEAR ITAB.
  CALL FUNCTION 'UPLOAD'
       EXPORTING
            FILENAME            = 'C:'
            FILETYPE            = 'DAT'
            SILENT              = 'S'
       TABLES
            DATA_TAB            = ITAB
       EXCEPTIONS
            CONVERSION_ERROR    = 1
            INVALID_TABLE_WIDTH = 2
            INVALID_TYPE        = 3
            NO_BATCH            = 4
            UNKNOWN_ERROR       = 5
            OTHERS              = 6.
***********************************************************************
  CLEAR ITAB.
  CLEAR ITAB1.

*  break-point.
* READING FROM TABLE ITAB1 WITH DATAS OF FLAT FILE.
  LOOP AT ITAB.
    FLAG ='S'.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
         EXPORTING
              INPUT  = ITAB-VBELN
         IMPORTING
              OUTPUT = ITAB-VBELN
         EXCEPTIONS
              OTHERS = 1.

    READ TABLE ITAB1 WITH KEY VBELN = ITAB-VBELN BINARY SEARCH.

    IF SY-SUBRC NE '0'.
      FLAG = 'N'.
    ENDIF.
    SELECT *  FROM VBFA WHERE VBELV = ITAB-VBELN.
    ENDSELECT.
IF SY-SUBRC EQ '0'.

ENDIF.
    IF SY-SUBRC EQ '0' OR FLAG ='N'.
*     rcount = rcount + 1.
      MOVE 'X' TO ITAB-FLAG1.
      MOVE ITAB1-NETWR TO ITAB-NETWR.
      MODIFY ITAB.
      ERR = 1.
    ELSE.
      MOVE ITAB1-NETWR TO ITAB-NETWR.
      RC = RC + 1 .
      MODIFY  ITAB.
    ENDIF.
*   clear itab.
*   clear itab1.
  ENDLOOP.
  IF ERR NE 0 .
    WRITE: RCOUNT,' SALES ORDER CAN NOT BE DELETED.'.
     ULINE.
.
    WRITE: 8'SO Number'.
    LOOP AT ITAB WHERE FLAG1 EQ 'X'.
      WRITE : /10 ITAB-VBELN .
    ENDLOOP.
  ENDIF.
  IF ERR EQ 0 AND TEST = '' .
    CALL FUNCTION 'BDC_OPEN_GROUP'
       EXPORTING
             CLIENT              = SY-MANDT
*         DEST                = FILLER8
             GROUP               = PSES
             HOLDDATE            = DATE
             KEEP                =  'X'
             USER                = SY-UNAME
*         RECORD              = FILLER1
*    IMPORTING
*         QID                 =
         EXCEPTIONS
              CLIENT_INVALID      = 1
              DESTINATION_INVALID = 2
              GROUP_INVALID       = 3
              GROUP_IS_LOCKED     = 4
              HOLDDATE_INVALID    = 5
              INTERNAL_ERROR      = 6
              QUEUE_ERROR         = 7
              RUNNING             = 8
              SYSTEM_LOCK_ERROR   = 9
              USER_INVALID        = 10
              OTHERS              = 11.



    RCOUNT = 0.
    LOOP AT ITAB.
       REFRESH IBDC.
           CLEAR IBDC.
      RCOUNT = RCOUNT + 1 .
       PERFORM FILL_SCREEN.

***********************************************************************
      CALL FUNCTION 'BDC_INSERT'
           EXPORTING
                TCODE            = 'VA02'
           TABLES
                DYNPROTAB        = IBDC
           EXCEPTIONS
                INTERNAL_ERROR   = 1
                NOT_OPEN         = 2
                QUEUE_ERROR      = 3
                TCODE_INVALID    = 4
                PRINTING_INVALID = 5
                POSTING_INVALID  = 6
                OTHERS           = 7.
    ENDLOOP.

    CALL FUNCTION 'BDC_CLOSE_GROUP'
         EXCEPTIONS
              NOT_OPEN    = 1
              QUEUE_ERROR = 2
              OTHERS      = 3.
   IF SY-SUBRC EQ 0.
    CONCATENATE 'Sesson ' PSES ' Generated Succesfully.' RCOUNT
    'Records Found'  INTO  MSG.
    MESSAGE I000(ZV) WITH MSG.
   ELSE.
     MESSAGE E000(ZV) WITH 'Error in BDC Session.'.
   ENDIF.

  ELSEIF ERR EQ 0 AND TEST = 'X' .
    WRITE : 10'SO NUMBER'  .
    ULINE.
    LOOP AT ITAB.
      WRITE :/10 ITAB-VBELN .
    ENDLOOP.
  ENDIF.
  EXIT.
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM FILL_SCREEN.
  IBDC-PROGRAM = 'SAPMV45A'.
  IBDC-DYNPRO = 0102.
  IBDC-DYNBEGIN = 'X'.
  APPEND IBDC.
  CLEAR IBDC.
  IBDC-FNAM = 'VBAK-VBELN'.
  IBDC-FVAL = ITAB-VBELN.
  APPEND IBDC.
  CLEAR IBDC.
  IBDC-FNAM = 'BDC_OKCODE'.
  IBDC-FVAL = '=ENT2'.
  APPEND IBDC.
  CLEAR IBDC.
  IBDC-PROGRAM = 'SAPMV45A'.
  IBDC-DYNPRO = 4001.
  IBDC-DYNBEGIN = 'X'.
  APPEND IBDC.
  CLEAR IBDC.
  IBDC-FNAM = 'BDC_OKCODE'.
  IBDC-FVAL = '/ELOES'.
  APPEND IBDC.
  CLEAR IBDC.
ENDFORM.
Tips by: Monosij

Back to ABAP Menu:
ABAP Example Hints and Tips

Return to :-
SAP Hints and Tips on Configuration and ABAP/4 Programming

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.