Sap Technology‎ > ‎ABAP DEVELOPMENT‎ > ‎

ABAP Program to delete mass documents

posted Jun 26, 2013, 3:37 AM by Unknown user   [ updated Jun 26, 2013, 3:46 AM ]

Generally, parked documents can be deleted separately. However, there is no option for deleting several documents at the same time. This ZDELEPARKED program solve this issue.


Step 1. Call transaction SE38 and create the ZDELEPARKED report.
              Title: Mass deletion of parked documents
              Type: Executable program
              Copy the report from the correction instructions then save and activate it.
              
Step 2. Create following text symbols (SE38 - text elements):
              001 General selections
              002 Processing status
              003 Delete parked documents
              004 Do you really want to delete the documents selected?
              005 No documents found
              006 Number of documents:
              007 Documents were already deleted.
              Save and activate the change.
              
Step 3. Create the following selection texts with reference to the dictionary:
              BELNR Document number
              BKTXT Document header text
              BLART Document type
              BLDAT Document date
              BUDAT Posting date
              BUKRS Company code
              GJAHR Fiscal year
              USNAM User
              XBLNR Reference
              XFRGE Released
              XPRFG Document complete
              XWFFR Release required.
              Save and activate the change.
              
Step 4. Call transaction SE41 (Menu Painter) with the following parameters:
              Program: ZDELEPARKED
              Status: DELE
              Choose CREATE.
              Short text: Delete parked documents
              Status type: Dialog status
a) Fill the SAVE function key (diskette) with the 'DELE' function code with the following parameters:

                       Functional type: Empty
                       Function text: Delete
                       Icon name: ICON_DELETE
                       Icon text: Delete
                       Fastpath: space
b) Fill the BACK function key (green arrow) with the 'BACK' function code.
c) Fill the EXIT function key (yellow arrow) with the '%EX' function code.
d) Fill the CANCEL function key (red arrow) with the 'RW' function code.
e) Fill the PRINT function key with the '%PRI' function code.
              
f) Create the LIST entry for the menu bar and assign to it these menu options:
                       %PRI Print
                       %PC Download
                       %EX Exit
g) Create the EDIT entry for the menu bar and assign to it these menu options:
                       DELE Delete
                       %SC  Find string
                       RW   Cancel
h) Create the GOTO entry for the menu bar and assign to it this menu option:
                       BACK Back
              
i) Save and activate the status.
              

Step 5. Activate the ZDELEPARKED report.


Code:
*&---------------------------------------------------------------------*
*& Report  ZDELEPARKED
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZDELEPARKED.

TABLES: vbkpf, t020.
DATA:   mode TYPE c VALUE 'N'.
DATA:   BEGIN OF tbkpf OCCURS 5.
        INCLUDE STRUCTURE vbkpf.
DATA:   END   OF tbkpf.
DATA:   BEGIN OF it020 OCCURS 0.
        INCLUDE STRUCTURE t020.
DATA:   END   OF it020.
DATA:   BEGIN OF bdcdata OCCURS 0.
        INCLUDE STRUCTURE bdcdata.
DATA:   END OF bdcdata.
DATA:   BEGIN OF messtab OCCURS 0.
        INCLUDE STRUCTURE bdcmsgcoll.
DATA:   END OF messtab.
DATA:    char(20)       TYPE c,
         count          TYPE i,
         xdele.

SELECT-OPTIONS:
         bukrs     FOR  vbkpf-bukrs MEMORY ID buk,
         belnr     FOR  vbkpf-belnr,
         gjahr     FOR  vbkpf-gjahr MEMORY ID gjr.


SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF BLOCK 2 WITH FRAME TITLE text-001.
SELECT-OPTIONS:
         budat     FOR  vbkpf-budat,
         bldat     FOR  vbkpf-bldat,
         blart     FOR  vbkpf-blart,
         xblnr     FOR  vbkpf-xblnr,
         bktxt     FOR  vbkpf-bktxt,
         usnam     FOR  vbkpf-usnam DEFAULT sy-uname.
SELECTION-SCREEN END OF BLOCK 2.

SELECTION-SCREEN BEGIN OF BLOCK 3 WITH FRAME TITLE text-002.
SELECT-OPTIONS:
         xwffr     FOR  vbkpf-xwffr,
         xprfg     FOR  vbkpf-xprfg,
         xfrge     FOR  vbkpf-xfrge.
SELECTION-SCREEN END OF BLOCK 3.


AT SELECTION-SCREEN.

START-OF-SELECTION.

  SET PF-STATUS 'DELE'.

  CALL FUNCTION 'AUTHORITY_CHECK_TCODE'
    EXPORTING
      tcode  = 'FBV0'
    EXCEPTIONS
      ok     = 1
      OTHERS = 4.
  IF sy-subrc = 4.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    EXIT.
  ENDIF.


  SELECT * FROM vbkpf INTO tbkpf
                      WHERE   ausbk IN bukrs
                        AND   belnr IN belnr
                        AND   gjahr IN gjahr
                        AND   budat IN budat
                        AND   bldat IN bldat
                        AND   blart IN blart
                        AND   bktxt IN bktxt
                        AND   xblnr IN xblnr
                        AND   usnam IN usnam
                        AND   bstat EQ 'V'
                        AND   xwffr IN xwffr
                        AND   xfrge IN xfrge
                        AND   xprfg IN xprfg.

    APPEND tbkpf.
    count = count + 1.
  ENDSELECT.

  SORT tbkpf BY ausbk belnr gjahr.

  IF count = 0.
    WRITE: /, text-005.
  ELSE.
    WRITE: /, text-006, count, /.
    ULINE.
    LOOP AT tbkpf.
      WRITE: /, tbkpf-ausbk, tbkpf-bukrs, tbkpf-belnr, tbkpf-gjahr.
    ENDLOOP.
  ENDIF.

END-OF-SELECTION.

AT USER-COMMAND.
  CLEAR: char.
  IF count > 0.
    CASE sy-ucomm.
      WHEN 'DELE'.
        IF xdele = space.
          CALL FUNCTION 'POPUP_TO_CONFIRM_LOSS_OF_DATA'
            EXPORTING
              titel     = text-003
              textline1 = text-004
              textline2 = space
            IMPORTING
              answer    = char(1).
          IF char(1) = 'J'.
            PERFORM fbv0_dele.
            xdele = 'X'.
          ELSE.
            EXIT.
          ENDIF.
        ELSE.
          MESSAGE i899(f5) WITH text-007.
        ENDIF.
      WHEN 'BACK'.
      WHEN OTHERS.
        EXIT.
    ENDCASE.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FBV0_DELE
*&---------------------------------------------------------------------*                                                                    *
*&      Find out, how document was parked (classically or by ENJOY)
*&---------------------------------------------------------------------*                                                                    *

FORM fbv0_dele.
  REFRESH messtab.
  LOOP AT tbkpf.
    CLEAR t020.
    SELECT SINGLE * FROM t020 WHERE tcode = tbkpf-tcode.
    IF t020-gener = space.              "parked by classical transaction
      PERFORM fbv0_dele1 USING tbkpf-gjahr
                                  tbkpf-belnr
                                  tbkpf-bukrs.
    ELSE.                               "parked by ENJOY
      PERFORM fbv0_dele2 USING tbkpf-gjahr
                            tbkpf-belnr
                            tbkpf-bukrs.
    ENDIF.
  ENDLOOP.
  CHECK sy-subrc EQ 0.
  LOOP AT messtab.
    WRITE: / messtab.
  ENDLOOP.
ENDFORM.                                                    "FBV0_DELE

*&---------------------------------------------------------------------*
*&      Form  FBV0_DELE1
*&---------------------------------------------------------------------*
*&      Classically parked documents
*&---------------------------------------------------------------------*

FORM fbv0_dele1 USING i_gjahr LIKE bkpf-gjahr
                     i_belnr LIKE bkpf-belnr
                     i_bukrs LIKE bkpf-bukrs.

  DATA: xdate(10) TYPE c.
  REFRESH bdcdata.
  PERFORM bdc_dynpro      USING 'SAPMF05V'
                                '0100'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '/00'.
  PERFORM bdc_field       USING 'BDC_CURSOR'
                                'RF05V-GJAHR'.
  PERFORM bdc_field       USING 'RF05V-BUKRS'
                                i_bukrs.
  PERFORM bdc_field       USING 'RF05V-BELNR'
                                i_belnr.
  PERFORM bdc_field       USING 'RF05V-GJAHR'
                                i_gjahr.
  PERFORM bdc_dynpro      USING 'SAPLF040'
                                '0700'.
  PERFORM bdc_field       USING 'BDC_CURSOR'
                                'BKPF-XBLNR'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                'BL'.
  PERFORM bdc_dynpro      USING 'SAPLSPO1'
                                '0200'.
  PERFORM bdc_field       USING 'BDC_CURSOR'
                                'SPOP-OPTION1'.              "Button YES
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                'ENTER'.

  CALL TRANSACTION 'FBV0' USING  bdcdata
                          MODE   mode
                          UPDATE 'S'.

  CALL FUNCTION 'MESSAGE_TEXT_BUILD'
    EXPORTING
      msgid               = sy-msgid
      msgnr               = sy-msgno
      msgv1               = sy-msgv1
      msgv2               = sy-msgv2
      msgv3               = sy-msgv3
      msgv4               = sy-msgv4
    IMPORTING
      message_text_output = messtab
    EXCEPTIONS
      OTHERS              = 4.
  APPEND messtab.
ENDFORM.                                                    "FBV0_DELE2

*&---------------------------------------------------------------------*
*&      Form  fbv0_dele2
*&---------------------------------------------------------------------*
*&     Documents parked by ENJOY
*&---------------------------------------------------------------------*

FORM fbv0_dele2  USING    i_gjahr
                          i_belnr
                          i_bukrs.
  DATA: xdate(10) TYPE c.
  REFRESH bdcdata.
  PERFORM bdc_dynpro      USING 'SAPMF05V'
                                '0100'.
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                '/00'.
  PERFORM bdc_field       USING 'BDC_CURSOR'
                                'RF05V-GJAHR'.
  PERFORM bdc_field       USING 'RF05V-BUKRS'
                                i_bukrs.
  PERFORM bdc_field       USING 'RF05V-BELNR'
                                i_belnr.
  PERFORM bdc_field       USING 'RF05V-GJAHR'
                                i_gjahr.

  IF t020-koart = 'D'.
    PERFORM bdc_dynpro      USING 'SAPMF05A'
                                   '1200'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'INVFO-ACCNT'.
  ELSEIF t020-koart = 'K'.
    PERFORM bdc_dynpro      USING 'SAPMF05A'
                                   '1100'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                   'INVFO-ACCNT'.
  ELSEIF t020-koart = 'S'.
    PERFORM bdc_dynpro      USING 'SAPMF05A'
                                   '1001'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'ACGL_HEAD-BLDAT'.
  ENDIF.

  PERFORM bdc_field       USING 'BDC_OKCODE'
                                 '=9-PD'.

  PERFORM bdc_dynpro      USING 'SAPLSPO1'
                                 '0200'.
  PERFORM bdc_field       USING 'BDC_CURSOR'
                                'SPOP-OPTION1'.              "Button YES
  PERFORM bdc_field       USING 'BDC_OKCODE'
                                'ENTER'.

  CALL TRANSACTION 'FBV0' USING  bdcdata
                          MODE   mode
                          UPDATE 'S'.

  CALL FUNCTION 'MESSAGE_TEXT_BUILD'
    EXPORTING
      msgid               = sy-msgid
      msgnr               = sy-msgno
      msgv1               = sy-msgv1
      msgv2               = sy-msgv2
      msgv3               = sy-msgv3
      msgv4               = sy-msgv4
    IMPORTING
      message_text_output = messtab
    EXCEPTIONS
      OTHERS              = 4.

  APPEND messtab.

ENDFORM.                    " fbv0_dele2

*&---------------------------------------------------------------------*
*&      Form  BDC_DYNPRO
*&---------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    "BDC_DYNPRO

*&---------------------------------------------------------------------*
*&      Form  BDC_FIELD
*&---------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  APPEND bdcdata.
ENDFORM.                    "BDC_FIELD
Comments