Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Introduction: For deleting PO History we can use a BAPI 'BAPI_PO_DELETE_HISTORY'.
EKBE is the PO history table, the entries that are to be changed must be transferred to the BAPI structure BAPIEKBE.
Function module MAP2E_EKBE_TO_BAPIEKBE is available for getting data into BAPI structure BAPIEKBE.
Below is the code that takes list of purchase orders from selection screen to be processed for history deletion.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
*&-----------------------------------------------------------------------*
*& Report          : ZMPMM038
*& DATE            : 28th Jan 2009                                               *
*& AUTHOR          : Krishna Chauhan (Sparta Consulting)        *
*& DEVELOPMENT     : Deleting PO History                               *
*&----------------------------------------------------------------------*
REPORT  ZMPMM038
NO STANDARD PAGE HEADING
LINE-SIZE 140
MESSAGE-ID ZM.
*------Tables--------------------------------------------------------------*
TABLES: EKBE.   "History per Purchasing Document
*******Data Declaration*****************************************************
DATA: IT_RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE,
      IT_PHIST  LIKE BAPIEKBE OCCURS 0 WITH HEADER LINE,
      WA_PHIST  LIKE LINE OF IT_PHIST,
      IT_TREF   LIKE CSL_TR_EXS OCCURS 0 WITH HEADER LINE,
      W_PO      LIKE BAPIEKKO-PO_NUMBER,
      IT_EKBE   LIKE EKBE     OCCURS 0 WITH HEADER LINE,
      WA_EKBE   LIKE LINE OF IT_EKBE.
****Selection screen definition********************************
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS : S_DOCNO FOR EKBE-EBELN OBLIGATORY.
SELECTION-SCREEN END OF BLOCK B1.
******Start of Selection****************************************
START-OF-SELECTION.
*Pick the data and process it for deleteing PO History.
  PERFORM PROCESS_DATA.
*&---------------------------------------------------------------------*
*&      Form  process_data
*&---------------------------------------------------------------------*
FORM PROCESS_DATA .
  CLEAR IT_EKBE.
  REFRESH IT_EKBE.
  SELECT * FROM EKBE
         INTO TABLE IT_EKBE
         WHERE EBELN IN S_DOCNO.
  CHECK SY-SUBRC EQ 0.
  SORT IT_EKBE BY EBELN.
  LOOP AT IT_EKBE INTO WA_EKBE.
    CLEAR: W_PO, WA_PHIST, IT_RETURN, IT_TREF.
    W_PO = WA_EKBE-EBELN.
*---fun mod to get 'bapiekbe' for passing it to BAPI history deetel
    CALL FUNCTION 'MAP2E_EKBE_TO_BAPIEKBE'
      EXPORTING
        EKBE                         = WA_EKBE
      CHANGING
        BAPIEKBE                     = WA_PHIST
      EXCEPTIONS
        ERROR_CONVERTING_CURR_AMOUNT = 1
        OTHERS                       = 2.
    APPEND  WA_PHIST TO IT_PHIST.
    CLEAR IT_PHIST.
    IF SY-SUBRC <> 0.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    AT END OF EBELN.
      CALL FUNCTION 'BAPI_PO_DELETE_HISTORY'
        EXPORTING
          PURCHASEORDER  = W_PO
        TABLES
          RETURN         = IT_RETURN
          POITEMHISTORY  = IT_PHIST
          TOKENREFERENCE = IT_TREF.
      IF SY-SUBRC = 0.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            WAIT = 'X'.
      ENDIF.    " IF sy-subrc = 0.
*---clear it_phist to Transfer Structure for PO History
      REFRESH IT_PHIST.
    ENDAT.
  ENDLOOP.
ENDFORM.                    " process_data



 Output can be checked in ME23N transaction, after execution of the prog it will not show any PO History for the respective PO.  Entries will also be deleted from EKBE table.

output: