Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Program to cancel PO?

Former Member
0 Kudos
242

Hi all,

Is there any BAPI or FM to cancel the Purchase orders?

If no please provide the sample BDC program to cancel PO.

Thanks,

Vamshi.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
193

Hi Vamshi,

I also had similar requirement,when client uploaded cut-over data of 6000+ PO and unfortunately all were wrong.

I used BAPI to delete all line items for PO.The following is the BAPI code:

REPORT  ZMM_BAPI_PO_DELETE MESSAGE-ID ZSW LINE-SIZE 255.

DATA : W_EBELN TYPE BAPIMEPOHEADER-PO_NUMBER,
       WA_POHEADER TYPE BAPIMEPOHEADER,
       WA_POHEADERX TYPE BAPIMEPOHEADERX,
       RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2,
       IT_POITEM TYPE TABLE OF BAPIMEPOITEM WITH HEADER LINE,
       IT_POITEMX TYPE TABLE OF BAPIMEPOITEMX WITH HEADER LINE.

DATA: IT_EXCELFILE TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

DATA : BEGIN OF RECORD OCCURS 0,
        EBELN TYPE EKKO-EBELN,
      END OF RECORD.
DATA : BEGIN OF IT_EBELP OCCURS 0,
        EBELP TYPE EKPO-EBELP,
       END OF IT_EBELP.

DATA : BEGIN OF IT_FINAL OCCURS 0,
        EBELN TYPE EKKO-EBELN,
        TY TYPE BAPIRET2-TYPE,
        MESSAGE TYPE BAPIRET2-MESSAGE,
       END OF IT_FINAL.
*----------------------------------------------------------------------*
* Temporary work variables
*----------------------------------------------------------------------*

DATA : WRK_LI TYPE I,
       W_SUC(10) TYPE N,
       W_ERR(10) TYPE N.

FIELD-SYMBOLS : <L_FS> .

*----------------------------------------------------------------------*
*                     Selection-Screen                                 *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS :  P_INFILE  LIKE RLGRAP-FILENAME.
PARAMETERS : P_SKIP(2) TYPE N OBLIGATORY DEFAULT '1'.
SELECTION-SCREEN END OF BLOCK B1 .

*----------------------------------------------------------------------*
*                   AT SELECTION-SCREEN ON VALUE REQUEST               *
*----------------------------------------------------------------------*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE.

  PERFORM OPEN_FILE.

*----------------------------------------------------------------------*
*                  AT Selection-Screen
*----------------------------------------------------------------------*

AT SELECTION-SCREEN.

  IF P_INFILE IS INITIAL.

    MESSAGE E000 WITH 'Please provide filename'.

  ENDIF.

  PERFORM READ_FILE.
*----------------------------------------------------------------------*
*                   START-OF-SELECTION                                 *
*----------------------------------------------------------------------*


START-OF-SELECTION.

  PERFORM MOVE_EXCEL_ITTABLE.

START-OF-SELECTION.

  LOOP AT RECORD.

    CLEAR : WA_POHEADER,WA_POHEADERX,W_EBELN,IT_EBELP,IT_POITEMX,IT_POITEM.
    REFRESH : IT_EBELP,IT_POITEMX,IT_POITEM,RETURN.

    W_EBELN = RECORD-EBELN.
    WA_POHEADER-PO_NUMBER = RECORD-EBELN.

    WA_POHEADERX-PO_NUMBER = 'X'.

    SELECT EBELP INTO TABLE IT_EBELP FROM EKPO WHERE EBELN = RECORD-EBELN.

    IF SY-SUBRC <> 0.

      IT_FINAL-EBELN = RECORD-EBELN.
      IT_FINAL-TY = 'E'.
      IT_FINAL-MESSAGE = 'This PO does not exist/No items found'.

      APPEND IT_FINAL.

      W_ERR = W_ERR + 1.

    ELSE.

      LOOP AT IT_EBELP.

        IT_POITEM-PO_ITEM = IT_EBELP-EBELP.
        IT_POITEM-DELETE_IND = 'L'.

        APPEND IT_POITEM.

        IT_POITEMX-PO_ITEM = IT_EBELP-EBELP.
        IT_POITEMX-DELETE_IND = 'X'.

        APPEND IT_POITEMX.

        CLEAR : IT_POITEMX,IT_POITEM.

      ENDLOOP.

      CALL FUNCTION 'BAPI_PO_CHANGE'
        EXPORTING
          PURCHASEORDER = W_EBELN
          POHEADER      = WA_POHEADER
          POHEADERX     = WA_POHEADERX
        TABLES
          RETURN        = RETURN
          POITEM        = IT_POITEM
          POITEMX       = IT_POITEMX.

      READ TABLE RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.

      IF SY-SUBRC = 0.

        W_ERR = W_ERR + 1.

        LOOP AT RETURN INTO WA_RETURN.

          IT_FINAL-EBELN = RECORD-EBELN.
          IT_FINAL-TY = WA_RETURN-TYPE.
          IT_FINAL-MESSAGE = WA_RETURN-MESSAGE.

          APPEND IT_FINAL.
          CLEAR : WA_RETURN,IT_FINAL.

        ENDLOOP.

      ELSE.

        W_SUC = W_SUC + 1.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            WAIT = 'X'.

        LOOP AT RETURN INTO WA_RETURN.

          IT_FINAL-EBELN = RECORD-EBELN.
          IT_FINAL-TY = WA_RETURN-TYPE.
          IT_FINAL-MESSAGE = WA_RETURN-MESSAGE.

          APPEND IT_FINAL.
          CLEAR : WA_RETURN,IT_FINAL.

        ENDLOOP.

      ENDIF.
    ENDIF.
  ENDLOOP.

*----------------------------------------------------------------------*
*                   END-OF-SELECTION                                   *
*----------------------------------------------------------------------*

END-OF-SELECTION.

  ULINE.

  READ TABLE RECORD.

  WRITE : 'Total Records Processed : ' COLOR 4 ,SY-TFILL COLOR 4.

  WRITE : / 'Success Records : ' COLOR 5 ,W_SUC COLOR 5,
          / 'Error Records   : ' COLOR 6 ,W_ERR COLOR 6.

  ULINE.

  LOOP AT IT_FINAL.

    IF  IT_FINAL-TY <> 'E'.

      WRITE : / IT_FINAL-EBELN,'|',IT_FINAL-TY,'|',IT_FINAL-MESSAGE.

    ELSE.

      WRITE : / IT_FINAL-EBELN COLOR 6,'|',IT_FINAL-TY COLOR 6,'|',IT_FINAL-MESSAGE COLOR 6.

    ENDIF.

  ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  read_file
*&---------------------------------------------------------------------*

FORM READ_FILE .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                = P_INFILE
      I_BEGIN_COL             = 1
      I_BEGIN_ROW             = 1
      I_END_COL               = 100
      I_END_ROW               = 65536
    TABLES
      INTERN                  = IT_EXCELFILE
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 2
      OTHERS                  = 3.

  IF IT_EXCELFILE[] IS INITIAL.
    MESSAGE E000.
  ENDIF.

ENDFORM.                    " read_file

*&---------------------------------------------------------------------*
*&      Form  OPEN_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM OPEN_FILE .

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      DEF_FILENAME     = SPACE
      DEF_PATH         = P_INFILE
      MASK             = ',*.* ,*.*.'
      MODE             = 'O'
      TITLE            = 'Please Select File'
    IMPORTING
      FILENAME         = P_INFILE
    EXCEPTIONS
      INV_WINSYS       = 04
      NO_BATCH         = 08
      SELECTION_CANCEL = 12
      SELECTION_ERROR  = 16.


ENDFORM.                    " open_file

*&---------------------------------------------------------------------*
*&      Form  MOVE_EXCEL_ITTABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM MOVE_EXCEL_ITTABLE .

  REFRESH RECORD.
  CLEAR RECORD.
  DATA : WRK_ROW TYPE I.
  CLEAR: WRK_ROW.
  DATA : WRK_TABIX LIKE SY-TABIX.
  WRK_ROW = P_SKIP + 1.

  READ TABLE IT_EXCELFILE WITH KEY ROW = WRK_ROW.

  IF SY-SUBRC = 0.

    WRK_TABIX = SY-TABIX.

    LOOP AT IT_EXCELFILE FROM WRK_TABIX.

      ASSIGN COMPONENT IT_EXCELFILE-COL
      OF STRUCTURE RECORD TO <L_FS>.

      MOVE IT_EXCELFILE-VALUE TO <L_FS> .
      AT END OF ROW .

        IF RECORD IS NOT INITIAL.
          APPEND RECORD.
          CLEAR RECORD.
        ENDIF.
      ENDAT.
    ENDLOOP .

  ENDIF.

ENDFORM.                    "MOVE_EXCEL_ITTABLE

Thanks & Reagrds,

Rock.

5 REPLIES 5

Former Member
0 Kudos
193

Go to me22n-enter the order no. which has to be cancelled/deleted-select the line item after then click the delete icon and save it. your po will got deleted/cancelled.

if you want BDC then take a recording of above steps and write the program.

AD

Former Member
0 Kudos
193

Hi,

Check this BAPI BAPI_PO_RESET_RELEASE

Or

Check this link..

Former Member
0 Kudos
194

Hi Vamshi,

I also had similar requirement,when client uploaded cut-over data of 6000+ PO and unfortunately all were wrong.

I used BAPI to delete all line items for PO.The following is the BAPI code:

REPORT  ZMM_BAPI_PO_DELETE MESSAGE-ID ZSW LINE-SIZE 255.

DATA : W_EBELN TYPE BAPIMEPOHEADER-PO_NUMBER,
       WA_POHEADER TYPE BAPIMEPOHEADER,
       WA_POHEADERX TYPE BAPIMEPOHEADERX,
       RETURN TYPE TABLE OF BAPIRET2,
       WA_RETURN TYPE BAPIRET2,
       IT_POITEM TYPE TABLE OF BAPIMEPOITEM WITH HEADER LINE,
       IT_POITEMX TYPE TABLE OF BAPIMEPOITEMX WITH HEADER LINE.

DATA: IT_EXCELFILE TYPE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.

DATA : BEGIN OF RECORD OCCURS 0,
        EBELN TYPE EKKO-EBELN,
      END OF RECORD.
DATA : BEGIN OF IT_EBELP OCCURS 0,
        EBELP TYPE EKPO-EBELP,
       END OF IT_EBELP.

DATA : BEGIN OF IT_FINAL OCCURS 0,
        EBELN TYPE EKKO-EBELN,
        TY TYPE BAPIRET2-TYPE,
        MESSAGE TYPE BAPIRET2-MESSAGE,
       END OF IT_FINAL.
*----------------------------------------------------------------------*
* Temporary work variables
*----------------------------------------------------------------------*

DATA : WRK_LI TYPE I,
       W_SUC(10) TYPE N,
       W_ERR(10) TYPE N.

FIELD-SYMBOLS : <L_FS> .

*----------------------------------------------------------------------*
*                     Selection-Screen                                 *
*----------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS :  P_INFILE  LIKE RLGRAP-FILENAME.
PARAMETERS : P_SKIP(2) TYPE N OBLIGATORY DEFAULT '1'.
SELECTION-SCREEN END OF BLOCK B1 .

*----------------------------------------------------------------------*
*                   AT SELECTION-SCREEN ON VALUE REQUEST               *
*----------------------------------------------------------------------*

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE.

  PERFORM OPEN_FILE.

*----------------------------------------------------------------------*
*                  AT Selection-Screen
*----------------------------------------------------------------------*

AT SELECTION-SCREEN.

  IF P_INFILE IS INITIAL.

    MESSAGE E000 WITH 'Please provide filename'.

  ENDIF.

  PERFORM READ_FILE.
*----------------------------------------------------------------------*
*                   START-OF-SELECTION                                 *
*----------------------------------------------------------------------*


START-OF-SELECTION.

  PERFORM MOVE_EXCEL_ITTABLE.

START-OF-SELECTION.

  LOOP AT RECORD.

    CLEAR : WA_POHEADER,WA_POHEADERX,W_EBELN,IT_EBELP,IT_POITEMX,IT_POITEM.
    REFRESH : IT_EBELP,IT_POITEMX,IT_POITEM,RETURN.

    W_EBELN = RECORD-EBELN.
    WA_POHEADER-PO_NUMBER = RECORD-EBELN.

    WA_POHEADERX-PO_NUMBER = 'X'.

    SELECT EBELP INTO TABLE IT_EBELP FROM EKPO WHERE EBELN = RECORD-EBELN.

    IF SY-SUBRC <> 0.

      IT_FINAL-EBELN = RECORD-EBELN.
      IT_FINAL-TY = 'E'.
      IT_FINAL-MESSAGE = 'This PO does not exist/No items found'.

      APPEND IT_FINAL.

      W_ERR = W_ERR + 1.

    ELSE.

      LOOP AT IT_EBELP.

        IT_POITEM-PO_ITEM = IT_EBELP-EBELP.
        IT_POITEM-DELETE_IND = 'L'.

        APPEND IT_POITEM.

        IT_POITEMX-PO_ITEM = IT_EBELP-EBELP.
        IT_POITEMX-DELETE_IND = 'X'.

        APPEND IT_POITEMX.

        CLEAR : IT_POITEMX,IT_POITEM.

      ENDLOOP.

      CALL FUNCTION 'BAPI_PO_CHANGE'
        EXPORTING
          PURCHASEORDER = W_EBELN
          POHEADER      = WA_POHEADER
          POHEADERX     = WA_POHEADERX
        TABLES
          RETURN        = RETURN
          POITEM        = IT_POITEM
          POITEMX       = IT_POITEMX.

      READ TABLE RETURN INTO WA_RETURN WITH KEY TYPE = 'E'.

      IF SY-SUBRC = 0.

        W_ERR = W_ERR + 1.

        LOOP AT RETURN INTO WA_RETURN.

          IT_FINAL-EBELN = RECORD-EBELN.
          IT_FINAL-TY = WA_RETURN-TYPE.
          IT_FINAL-MESSAGE = WA_RETURN-MESSAGE.

          APPEND IT_FINAL.
          CLEAR : WA_RETURN,IT_FINAL.

        ENDLOOP.

      ELSE.

        W_SUC = W_SUC + 1.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            WAIT = 'X'.

        LOOP AT RETURN INTO WA_RETURN.

          IT_FINAL-EBELN = RECORD-EBELN.
          IT_FINAL-TY = WA_RETURN-TYPE.
          IT_FINAL-MESSAGE = WA_RETURN-MESSAGE.

          APPEND IT_FINAL.
          CLEAR : WA_RETURN,IT_FINAL.

        ENDLOOP.

      ENDIF.
    ENDIF.
  ENDLOOP.

*----------------------------------------------------------------------*
*                   END-OF-SELECTION                                   *
*----------------------------------------------------------------------*

END-OF-SELECTION.

  ULINE.

  READ TABLE RECORD.

  WRITE : 'Total Records Processed : ' COLOR 4 ,SY-TFILL COLOR 4.

  WRITE : / 'Success Records : ' COLOR 5 ,W_SUC COLOR 5,
          / 'Error Records   : ' COLOR 6 ,W_ERR COLOR 6.

  ULINE.

  LOOP AT IT_FINAL.

    IF  IT_FINAL-TY <> 'E'.

      WRITE : / IT_FINAL-EBELN,'|',IT_FINAL-TY,'|',IT_FINAL-MESSAGE.

    ELSE.

      WRITE : / IT_FINAL-EBELN COLOR 6,'|',IT_FINAL-TY COLOR 6,'|',IT_FINAL-MESSAGE COLOR 6.

    ENDIF.

  ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  read_file
*&---------------------------------------------------------------------*

FORM READ_FILE .

  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      FILENAME                = P_INFILE
      I_BEGIN_COL             = 1
      I_BEGIN_ROW             = 1
      I_END_COL               = 100
      I_END_ROW               = 65536
    TABLES
      INTERN                  = IT_EXCELFILE
    EXCEPTIONS
      INCONSISTENT_PARAMETERS = 1
      UPLOAD_OLE              = 2
      OTHERS                  = 3.

  IF IT_EXCELFILE[] IS INITIAL.
    MESSAGE E000.
  ENDIF.

ENDFORM.                    " read_file

*&---------------------------------------------------------------------*
*&      Form  OPEN_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM OPEN_FILE .

  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      DEF_FILENAME     = SPACE
      DEF_PATH         = P_INFILE
      MASK             = ',*.* ,*.*.'
      MODE             = 'O'
      TITLE            = 'Please Select File'
    IMPORTING
      FILENAME         = P_INFILE
    EXCEPTIONS
      INV_WINSYS       = 04
      NO_BATCH         = 08
      SELECTION_CANCEL = 12
      SELECTION_ERROR  = 16.


ENDFORM.                    " open_file

*&---------------------------------------------------------------------*
*&      Form  MOVE_EXCEL_ITTABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM MOVE_EXCEL_ITTABLE .

  REFRESH RECORD.
  CLEAR RECORD.
  DATA : WRK_ROW TYPE I.
  CLEAR: WRK_ROW.
  DATA : WRK_TABIX LIKE SY-TABIX.
  WRK_ROW = P_SKIP + 1.

  READ TABLE IT_EXCELFILE WITH KEY ROW = WRK_ROW.

  IF SY-SUBRC = 0.

    WRK_TABIX = SY-TABIX.

    LOOP AT IT_EXCELFILE FROM WRK_TABIX.

      ASSIGN COMPONENT IT_EXCELFILE-COL
      OF STRUCTURE RECORD TO <L_FS>.

      MOVE IT_EXCELFILE-VALUE TO <L_FS> .
      AT END OF ROW .

        IF RECORD IS NOT INITIAL.
          APPEND RECORD.
          CLEAR RECORD.
        ENDIF.
      ENDAT.
    ENDLOOP .

  ENDIF.

ENDFORM.                    "MOVE_EXCEL_ITTABLE

Thanks & Reagrds,

Rock.

0 Kudos
193

Hi all,

Thanks for the reply.

Hi Rock,

Problem solved.

Thanks,

Vamshi

Former Member
0 Kudos
193

Hello Vamsi,

There is a BAPI to cancel a Release of PO --> BAPI_PO_RESET_RELEASE.

Please check the same.

-Syed.