Mass Deletion of Purchase Requisitions

Our SAP system creates multiple MRP generated purchase requisitions on a daily basis and we do not have the manpower to delete each single PR using the ME52N transaction. I checked the MEMASSRQ transaction which is used for mass changes of PRs. However, in MEMASSRQ, there isn't any field mentioned particularly for deletion or blocking of purchase requisition. I found a BAPI, BAPI_REQUISITION_DELETE, but even this BAPI is used to delete the PRs one by one. Not for mass deletion.

Right now, there are around 4000 PRs in our system which are lying and we have to delete them all. So, I want your help in finding out whether there is a transaction/report available for mass deletion of purchase requisitions.

Solution:

The standard SAP system does not provided any mass delete program for PR.

You can used this customized program to mass flag for deletion the PR. 

BAPI_REQUISITION_DELETE Program

*&---------------------------------------------------------------------*
*& Report  ZDELETE_PR_WITH_BAPI
*&---------------------------------------------------------------------*
*& Deletion/closing of purchase requisitions with BAPI
*&---------------------------------------------------------------------*

report  zdelete_pr_with_bapi.

type-pools slis.                       "for ALV

***Tables
tables: eban, mard.

*** DATA
data: ex_numer like bapieban-preq_no,
      tb_item_to_del like bapieband occurs 0 with header line,
      tb_return like bapireturn occurs 0 with header line.

data: begin of i_eban occurs 0,
        banfn like eban-banfn,
        bnfpo like eban-bnfpo,
        bsart like eban-bsart,
        matnr like eban-matnr,
        txz01 like eban-txz01,
        menge like eban-menge,
        meins like eban-meins,
        erdat like eban-erdat,
      end of i_eban.
data  c_i_eban like i_eban occurs 0 with header line.
data: begin of i_msg occurs 0,
        light(1) type c,
        banfn like eban-banfn,
        message like bapireturn-message,
      end of i_msg.
data: index like sy-tabix.
 

** Selection
selection-screen begin of block bl1 with frame title text-001.
select-options: r_eban for eban-banfn,
                r_bsart for eban-bsart,
                r_erdat for eban-erdat.
parameter: p_werks like  mard-werks obligatory memory id wrk.
*          p_sl like mard-lgort memory id lag,
select-options: r_sl for  mard-lgort memory id lag.
parameter: p_statu like eban-statu default 'N' obligatory.

selection-screen end of block bl1.
selection-screen begin of block bl2 with frame title text-002.
parameter: p_del radiobutton group act,
           p_clos radiobutton group act.
selection-screen uline.
parameters x_test as checkbox default 'X'.
selection-screen end of block bl2.

at selection-screen.

  perform authorisation_check using p_werks.

start-of-selection.

  select banfn bnfpo bsart matnr txz01 menge meins erdat
         from eban into table i_eban
         where banfn in r_eban
         and bsart in r_bsart
         and statu = p_statu
         and erdat in r_erdat
         and werks = p_werks
*        and lgort = p_sl
         and lgort in r_sl
         and loekz = space.

  c_i_eban[] = i_eban[].
  sort i_eban by banfn.
  delete adjacent duplicates from i_eban comparing banfn.
  sort c_i_eban by banfn bnfpo.

  check x_test is initial.

  loop at i_eban.
    free: tb_item_to_del, tb_return. clear ex_numer.

    read table c_i_eban with key banfn = i_eban-banfn.
    if sy-subrc = 0.
      index = sy-tabix.
      ex_numer = i_eban-banfn.
      loop at c_i_eban from index.
        if c_i_eban-banfn ne i_eban-banfn.
          exit.
        endif.
        tb_item_to_del-preq_item = c_i_eban-bnfpo.
        tb_item_to_del-delete_ind = p_del.
        tb_item_to_del-closed = p_clos.
        append tb_item_to_del.
      endloop.

      call function 'BAPI_REQUISITION_DELETE'
        exporting
          number                      = ex_numer
        tables
          requisition_items_to_delete = tb_item_to_del
          return                      = tb_return.
      loop at tb_return.
        clear i_msg.
        i_msg-banfn = i_eban-banfn.
        i_msg-message = tb_return-message.
        if tb_return-type = 'E' or tb_return-type = 'A'.
          i_msg-light = '1'.
        elseif tb_return-code = 'W5041'.
          i_msg-light = '3'.
        endif.
        append i_msg.
      endloop.
    endif.
  endloop.

end-of-selection.

  if x_test = 'X'.
    perform show_selected_pr tables c_i_eban.
  else.
    perform show_messages tables i_msg.
  endif.

************************************************************************
* Forms
***********************************************************************
*&---------------------------------------------------------------------*
*&      Form  show_selected_pr
*&---------------------------------------------------------------------*
*       Show selected purchase requisitions
*----------------------------------------------------------------------*
*      -->it_eban
*----------------------------------------------------------------------*
form show_selected_pr  tables   it_eban structure i_eban.
  data lt_fieldcat type slis_t_fieldcat_alv.
  data ls_fieldcat type slis_fieldcat_alv.
  data ls_layout type slis_layout_alv.
  data lt_events type slis_t_event.
  data ls_event  type slis_alv_event.

* build fieldcat
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'BANFN'.
  ls_fieldcat-ref_fieldname = 'BANFN'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  ls_fieldcat-key           = 'X'.
  ls_fieldcat-hotspot       = 'X'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'BNFPO'.
  ls_fieldcat-ref_fieldname = 'BNFPO'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  ls_fieldcat-key           = 'X'.
  ls_fieldcat-hotspot       = 'X'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'BSART'.
  ls_fieldcat-ref_fieldname = 'BSART'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'MATNR'.
  ls_fieldcat-ref_fieldname = 'MATNR'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'TXZ01'.
  ls_fieldcat-ref_fieldname = 'TXZ01'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'MENGE'.
  ls_fieldcat-ref_fieldname = 'MENGE'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  ls_fieldcat-qfieldname    = 'MEINS'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'MEINS'.
  ls_fieldcat-ref_fieldname = 'MEINS'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'ERDAT'.
  ls_fieldcat-ref_fieldname = 'ERDAT'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  insert ls_fieldcat into table lt_fieldcat.

* events
  clear ls_event.
  ls_event-name = 'TOP_OF_LIST'.
  ls_event-form = 'TOP_OF_LIST'.
  insert ls_event into table lt_events.

  ls_layout-colwidth_optimize = 'X'.
  ls_layout-f2code = 'ANZ'.

  call function 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
*     I_INTERFACE_CHECK              = ' '
*     I_BYPASSING_BUFFER             =
*     I_BUFFER_ACTIVE                = ' '
      I_CALLBACK_PROGRAM             = sy-repid
*     I_CALLBACK_PF_STATUS_SET       = ' '
      I_CALLBACK_USER_COMMAND        = 'USER_COMMAND'
*     I_STRUCTURE_NAME               =
      IS_LAYOUT                      = ls_layout
      IT_FIELDCAT                    = lt_fieldcat[]
*     IT_EXCLUDING                   =
*     IT_SPECIAL_GROUPS              =
*     IT_SORT                        =
*     IT_FILTER                      =
*     IS_SEL_HIDE                    =
*     I_DEFAULT                      = 'X'
*     I_SAVE                         = ' '
*     IS_VARIANT                     =
      IT_EVENTS                      = lt_events[]
*     IT_EVENT_EXIT                  =
*     IS_PRINT                       =
*     IS_REPREP_ID                   =
*     I_SCREEN_START_COLUMN         = 0
*     I_SCREEN_START_LINE            = 0
*     I_SCREEN_END_COLUMN            = 0
*     I_SCREEN_END_LINE              = 0
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER        =
*     ES_EXIT_CAUSED_BY_USER         =
    tables
      t_outtab                       = it_eban
    EXCEPTIONS
      PROGRAM_ERROR                  = 1
      OTHERS                         = 2.

endform.                    " show_selected_pr
************************************************************************
*    Form TOP_OF_LIST
************************************************************************
form top_of_list.

  write: / text-top.

endform.
*----------------------------------------------------------------------*
*       FORM USERCOMMAND
*----------------------------------------------------------------------*
form user_command using r_ucomm like sy-ucomm
                        rs_selfield type slis_selfield.

  case r_ucomm.
    when 'ANZ'.
*     read table gt_quotations index rs_selfield-tabindex.
      read table c_i_eban index rs_selfield-tabindex.
      if sy-subrc = 0.
        set parameter id 'BAN' field c_i_eban-banfn.
        call transaction 'ME53N' and skip first screen.
      endif.

  endcase.
endform.
*----------------------------------------------------------------------*
*       FORM USERCOMMAND2
*----------------------------------------------------------------------*
form user_command2 using r_ucomm like sy-ucomm
                        rs_selfield type slis_selfield.

  case r_ucomm.
    when 'ANZ'.
*     read table gt_quotations index rs_selfield-tabindex.
      read table i_msg index rs_selfield-tabindex.
      if sy-subrc = 0.
        set parameter id 'BAN' field i_msg-banfn.
        call transaction 'ME53N' and skip first screen.
      endif.

  endcase.
endform.
*&---------------------------------------------------------------------*
*&      Form  show_messages
*&---------------------------------------------------------------------*
*       Show messages from BAPI
*----------------------------------------------------------------------*
*      -->it_msg
*----------------------------------------------------------------------*
form show_messages  tables   it_msg structure i_msg.
  data lt_fieldcat type slis_t_fieldcat_alv.
  data ls_fieldcat type slis_fieldcat_alv.
  data ls_layout type slis_layout_alv.
  data lt_events type slis_t_event.
  data ls_event  type slis_alv_event.

* build fieldcat
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'BANFN'.
  ls_fieldcat-ref_fieldname = 'BANFN'.
  ls_fieldcat-ref_tabname   = 'EBAN'.
  ls_fieldcat-key           = 'X'.
  ls_fieldcat-hotspot       = 'X'.
  insert ls_fieldcat into table lt_fieldcat.
  clear ls_fieldcat.
  ls_fieldcat-fieldname     = 'MESSAGE'.
  ls_fieldcat-ref_fieldname = 'MESSAGE'.
  ls_fieldcat-ref_tabname   = 'BAPIRETURN'.
  insert ls_fieldcat into table lt_fieldcat.

  ls_layout-colwidth_optimize = 'X'.
  ls_layout-f2code = 'ANZ'.
  ls_layout-lights_fieldname = 'LIGHT'.

  call function 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
*     I_INTERFACE_CHECK              = ' '
*     I_BYPASSING_BUFFER             =
*     I_BUFFER_ACTIVE                = ' '
      I_CALLBACK_PROGRAM             = sy-repid
*     I_CALLBACK_PF_STATUS_SET       = ' '
      I_CALLBACK_USER_COMMAND        = 'USER_COMMAND2'
*     I_STRUCTURE_NAME               =
      IS_LAYOUT                      = ls_layout
      IT_FIELDCAT                    = lt_fieldcat[]
*     IT_EXCLUDING                   =
*     IT_SPECIAL_GROUPS              =
*     IT_SORT                        =
*     IT_FILTER                      =
*     IS_SEL_HIDE                    =
*     I_DEFAULT                      = 'X'
*     I_SAVE                         = ' '
*     IS_VARIANT                     =
*     IT_EVENTS                      = lt_events[]
*     IT_EVENT_EXIT                  =
*     IS_PRINT                       =
*     IS_REPREP_ID                   =
*     I_SCREEN_START_COLUMN          = 0
*     I_SCREEN_START_LINE            = 0
*     I_SCREEN_END_COLUMN            = 0
*     I_SCREEN_END_LINE              = 0
*   IMPORTING
*     E_EXIT_CAUSED_BY_CALLER        =
*     ES_EXIT_CAUSED_BY_USER         =
    tables
      t_outtab                       = it_msg
    EXCEPTIONS
      PROGRAM_ERROR                  = 1
      OTHERS                         = 2.

endform.                    " show_messages
*&---------------------------------------------------------------------*
*&      Form  authorisation_check
*&---------------------------------------------------------------------*
*       Check authorisation object M_BANF_WRK
*----------------------------------------------------------------------*
*      -->iv_werks
*----------------------------------------------------------------------*
form authorisation_check  using    iv_werks like mard-werks.
  data lv_text type string.

  authority-check object 'M_BANF_WRK'
           id 'ACTVT' field '02'                "change
           id 'WERKS' field iv_werks.
  if sy-subrc <> 0.
    lv_text = text-e01.
    replace '&' with iv_werks into lv_text.
    message e000(zz) with lv_text.
  endif.

endform.                    " authorisation_check

*-- End of Program

ABAP Tips

See Also
Changed Service Order Status Upon Service Confirmation

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.