Friday, April 17, 2020

ABAP/4 : BAPI:BAPI_GOODSMVT_CANCEL Reversed MM DOC (MBST)

ABAP/4 : BAPI:BAPI_GOODSMVT_CANCEL Reversed MM DOC (MBST) 

 

IMPORT 

 MATERIALDOCUMENT : MM Doc
MATDOCUMENTYEAR : MM Doc Year
GOODSMVT_PSTNG_DATE : Posting Date
GOODSMVT_PR_UNAME : User ID

EXPORT
GOODSMVT_HEADRET : MM Doc

TABLES
RETURN :  Return Message
GOODSMVT_MATDOCITEM : By Item 

The Sample code is divided into 3 parts:


Input Screen :

*&---------------------------------------------------------------------*
*& Report  ZMM_MATERIAL_DOC_CANCEL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMM_MATERIAL_DOC_CANCEL.
INCLUDE zmm_material_doc_top.
INCLUDE ZMM_MATERIAL_DOC_F01.
INITIALIZATION.
P_LGORT 'XX01'.
START-OF-SELECTION.
PERFORM GET_DATA_DISPLAY.


------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
*    TOP Code
-------------------------------------------------------------------------------------------------------------
*&---------------------------------------------------------------------*
*&  Include           ZMM_MATERIAL_DOC_TOP
*&---------------------------------------------------------------------*
TABLES MSEG.
 DATABEGIN OF it_result OCCURS 0,
        MBLNR TYPE MSEG-MBLNR,
        text  TYPE char100,
      END OF it_result.
      data :   w_result  LIKE it_result.

    DATA:
      l_materialdocument    TYPE bapi2017_gm_head_02-mat_doc,
      l_matdocumentyear     TYPE bapi2017_gm_head_02-doc_year,
      l_goodsmvt_pstng_date TYPE bapi2017_gm_head_02-pstng_date,
      l_goodsmvt_pr_uname   TYPE bapi2017_gm_head_01-pr_uname.

    DATAl_goodsmvt_headret    LIKE bapi2017_gm_head_ret.
    DATAlt_goodsmvt_matdocitem LIKE STANDARD TABLE OF bapi2017_gm_item_04 WITH HEADER LINE.
    DATA it_return TYPE STANDARD TABLE OF bapiret2,
           w_return  TYPE                   bapiret2.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001 .
SELECT-OPTIONS s_mblnr FOR mseg-mblnr OBLIGATORY.
PARAMETERS     p_mjahr type mseg-mjahr  OBLIGATORY.
PARAMETERS     P_BUDAT TYPE BUDAT OBLIGATORY.
PARAMETERS     P_LGORT TYPE LGORT_D .
SELECTION-SCREEN :END OF BLOCK b1.

AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.

    IF SCREEN-NAME 'P_LGORT'.
      SCREEN-INPUT 0.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
                  Query F01 Code
-----------------------------------------------------------------------------------------------------------

*&---------------------------------------------------------------------*
*&  Include           ZMM_MATERIAL_DOC_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  GET_DATA_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_data_display .
  SELECT mblnr,
         mjahr,
         zeile,
         werks,
         lgort
         FROM mseg INTO  TABLE @DATA(it_mseg)
         WHERE  mblnr IN @s_mblnr
          AND mjahr @p_mjahr
          AND lgort @p_lgort.
  IF it_mseg[] IS NOT INITIAL.


    LOOP AT it_mseg INTO DATA(wa_mseg).
      l_materialdocument    wa_mseg-mblnr.
      l_matdocumentyear     p_mjahr.
      l_goodsmvt_pstng_date p_budat.
      l_goodsmvt_pr_uname   sy-uname.

      lt_goodsmvt_matdocitem-matdoc_item wa_mseg-zeile.
      APPEND lt_goodsmvt_matdocitemCLEAR lt_goodsmvt_matdocitem.

      CALL FUNCTION 'BAPI_GOODSMVT_CANCEL'
        EXPORTING
          materialdocument    l_materialdocument
          matdocumentyear     l_matdocumentyear
          goodsmvt_pstng_date l_goodsmvt_pstng_date
          goodsmvt_pr_uname   l_goodsmvt_pr_uname
        IMPORTING
          goodsmvt_headret    l_goodsmvt_headret
        TABLES
          return              IT_return
          goodsmvt_matdocitem lt_goodsmvt_matdocitem.




       IF it_return IS NOT INITIAL.
          READ TABLE it_return INTO w_return WITH KEY type 'E'.
          IF sy-subrc 0.
            ROLLBACK WORK.
            w_result-MBLNR l_materialdocument.
            w_result-text  'Not updated successfully'.
            APPEND w_result TO it_result.

          endif. "As this bapi only fill return table when there is error
            CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              EXPORTING
                wait abap_true.
            w_result-MBLNR l_materialdocument.
            w_result-text  'Updated successfully'.
            APPEND w_result TO it_result.
            WAIT UP TO SECONDS.
          CLEAR w_result.
        ENDIF.
        REFRESH:LT_GOODSMVT_MATDOCITEM,it_return.
        CLEAR l_materialdocument,l_matdocumentyear,l_goodsmvt_pstng_date,
                l_goodsmvt_pr_uname,l_goodsmvt_headret,WA_MSEG.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait 'X'
*  IMPORTING
*         RETURN        =
        .

    ENDLOOP.

endif.
 LOOP AT it_result INTO w_result.
    WRITE:/1 sy-vline,
          (10)w_result-mblnrsy-vline,
          (50)w_result-textsy-vline.
  ENDLOOP.
ENDFORM.