SAP Virtual/Onsite Trainings

Document Categories:

What's New?

Contribute?

Sample Specs

What's Hot?

Web Dynpro for ABAP Tutorials 


Join the Mailing List
Enter name and email address below:
Name:
Email:
Subscribe  Unsubscribe 

GR Posting for each line item of Purchase order using a BAPI

By Prasanna, Infosys

The BAPI 'BAPI_GOODSMVT_CREATE’ is used here to post Goods Receipt. Create a program with the following code:

* Tables
TABLES: ekko.

* Selection Screen
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

* Types declaration for EKKO table
TYPES: BEGIN OF ty_ekko,
       ebeln TYPE ebeln,
       END OF ty_ekko.

* Types declaration for EKPO table
TYPES: BEGIN OF ty_ekpo,
       ebeln TYPE ebeln,
       ebelp TYPE ebelp,
       matnr TYPE matnr,
       werks TYPE ewerk,
       lgort TYPE lgort_d,
       menge TYPE bstmg,
       meins TYPE bstme,
       END OF ty_ekpo.

* Types declaration for MAKTX table
TYPES: BEGIN OF ty_maktx,
       matnr TYPE matnr,
       maktx TYPE maktx,
       END OF ty_maktx.

*Types declaration for Output data structure

TYPES: BEGIN OF ty_det,
       ebeln TYPE ebeln,
       mblnr TYPE mblnr,
       mjahr TYPE mjahr,
       END OF ty_det.  

* Internal Tables Declaration
DATA: IT_EKKO  TYPE STANDARD TABLE OF TY_EKKO,
      IT_EKPO  TYPE STANDARD TABLE OF TY_EKPO,
      IT_TEMP  TYPE STANDARD TABLE OF TY_EKPO,
      IT_MAKTX TYPE STANDARD TABLE OF TY_MAKTX,

     IT_DET       TYPE STANDARD TABLE OF TY_DET.

* Work area Declarations
DATA: wa_ekpo  TYPE ty_ekpo,
      wa_maktx TYPE ty_maktx,

      wa_det TYPE ty_det.

* BAPI Structure Declaration
DATA: GMVT_HEADER       TYPE BAPI2017_GM_HEAD_01,
      WA_GMVT_ITEM      TYPE BAPI2017_GM_ITEM_CREATE,
      GMVT_HEADRET      TYPE BAPI2017_GM_HEAD_RET,
      RT_BAPIRET             TYPE STANDARD TABLE OF BAPIRET2,
      GMVT_ITEM         TYPE STANDARD TABLE OF BAPI2017_GM_ITEM_CREATE.

START-OF-SELECTION.

*Retrieve PO documents from Header (EKKO) table
SELECT EBELN FROM EKKO
INTO TABLE IT_EKKO
WHERE EBELN IN S_EBELN.

IF sy-subrc = AND it_ekko[] IS NOT INITIAL.

*Retrieve PO document details from Item (EKPO) table
  SELECT EBELN EBELP MATNR WERKS LGORT MENGE MEINS
  FROM EKPO
  INTO TABLE IT_EKPO
 FOR ALL ENTRIES IN IT_EKKO
 WHERE EBELN = IT_EKKO-EBELN.

  IF SY-SUBRC = AND IT_EKPO[] IS NOT INITIAL.
    IT_TEMP[] = IT_EKPO[].
    SORT IT_TEMP BY MATNR.
    DELETE ADJACENT DUPLICATES FROM IT_TEMP COMPARING MATNR.  

    IF IT_TEMP[] IS NOT INITIAL.
*     Retrieve the Material Description from MAKT table
      SELECT MATNR MAKTX
      FROM MAKT
      INTO TABLE IT_MAKTX
      FOR ALL ENTRIES IN IT_TEMP
      WHERE MATNR = IT_TEMP-MATNR.
      SORT IT_EKPO BY EBELN EBELP.
    ENDIF.
  ENDIF.
ENDIF.

* Looping the PO details.
LOOP AT it_ekpo INTO wa_ekpo .
  READ TABLE IT_MAKTX INTO WA_MAKTX WITH KEY MATNR = WA_EKPO-MATNR.
  IF SY-SUBRC = 0.
    WA_GMVT_ITEM-ITEM_TEXT = WA_MAKTX-MAKTX.
  ENDIF.

* Fill the bapi Header structure details
  GMVT_HEADER-PSTNG_DATE = SY-DATUM.
  GMVT_HEADER-DOC_DATE = SY-DATUM.
  GMVT_HEADER-PR_UNAME = SY-UNAME.
  GMVT_HEADER-REF_DOC_NO = WA_EKPO-EBELN.

* FILL THE BAPI ITEM STRUCTURE DETAILS
  WA_GMVT_ITEM-MATERIAL = WA_EKPO-MATNR.
  WA_GMVT_ITEM-PLANT = WA_EKPO-WERKS.
  WA_GMVT_ITEM-STGE_LOC = WA_EKPO-LGORT.
  WA_GMVT_ITEM-MOVE_TYPE = '101'.
  WA_GMVT_ITEM-PO_NUMBER = WA_EKPO-EBELN.
  WA_GMVT_ITEM-PO_ITEM = WA_EKPO-EBELP.
  WA_GMVT_ITEM-ENTRY_QNT = WA_EKPO-MENGE.
  WA_GMVT_ITEM-ENTRY_UOM = WA_EKPO-MEINS.
  WA_GMVT_ITEM-NO_MORE_GR = 'X'.
  WA_GMVT_ITEM-REF_DOC = WA_EKPO-EBELN.
  WA_GMVT_ITEM-PROD_DATE = SY-DATUM.
  WA_GMVT_ITEM-MVT_IND = 'B'.

  APPEND WA_GMVT_ITEM TO GMVT_ITEM.
  CLEAR WA_GMVT_ITEM.

* Call the BAPI FM for GR posting
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      GOODSMVT_HEADER  = GMVT_HEADER
      GOODSMVT_CODE    = '01'
    IMPORTING
      GOODSMVT_HEADRET = GMVT_HEADRET
    TABLES
      GOODSMVT_ITEM    = GMVT_ITEM[]
      RETURN           = RT_BAPIRET[].

  IF sy-subrc = 0.
* For commit the changes use BAPI_TRANSACTION_COMMIT FM.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait 'X'.
      MOVE: GMVT_HEADRET-MAT_DOC TO WA_DET-MBLNR,
            GMVT_HEADRET-DOC_YEAR TO WA_DET-MJAHR,
            GMVT_HEADER-REF_DOC_NO TO WA_DET-EBELN.
      APPEND WA_DET TO IT_DET.
      CLEAR WA_DET.
    ENDIF.
  ENDLOOP.

  WRITE:1(15)'PO document', 18(20)'Material Documant', 40(5)'Year'.
  LOOP AT IT_DET INTO WA_DET.
  WRITE:/1(15) WA_DET-EBELN,
         18(20) WA_DET-MBLNR,
         40(5) WA_DET-MJAHR.
  ENDLOOP.

Execute the program.

The selection screen will be displayed.

Enter the Purchase order number “0007654321”

Choose Execute button.

It will create a material document number for posting of movement type 101.

The output will be displayed as below

To check the details of the material document created for given movement type,

Go to MB03 transaction

Choose enter; the details of the material document will be displayed.

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image