Application Development and Automation 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: 
Read only

GR not getting posted through BAPI_GOODSMVT_CREATE-code, but successfully posted by MIGO transaction

Rangan
Explorer
0 Likes
1,727

Hello experts

,GR-error2.jpg

When I create a Goods Receipt with reference to Purchase Order using BAPI_GOODSMVT_CREATE, the following error message appears.

No goods receipt possible for purchase order <PO number> <line item number>.

But the GR is getting created manually using Transaction MIGO for the same PO number.

Here is the code:

SELECT deliverydocument,             "vbeln
             deliverydocumentitem,         "posnr
             plant                         "werks
             FROM i_deliverydocumentitem   "lips
             INTO TABLE @DATA(dlv_item)
             WHERE deliverydocument IN @inb_delv             " OR deliverydocument IN @delv_num )
             AND plant IN @Store.
      IF sy-subrc 0.
        SELECT deliverydocument,          "vbeln
               supplier                   "lifnr
            FROM i_deliverydocument       "likp
            INTO TABLE @DATA(dlv_hdr)
            FOR ALL ENTRIES IN @dlv_item
            WHERE deliverydocument @dlv_item-deliverydocument
            AND deliverydate IN @s_date_tmp
            AND sddocumentcategory '7'.
        IF sy-subrc 0.

          SELECT businesspartner,              "partner
                 bpidentificationtype,         "type
                 bpidentificationnumber        "idnumber
                 FROM i_bupaidentification     "but0id
                 INTO TABLE @DATA(bp_id)
                 FOR ALL ENTRIES IN @dlv_hdr
                 WHERE businesspartner @dlv_hdr-supplier
                 AND   bpidentificationtype 'GLREG'.
          IF sy-subrc 0.

            SELECT low
              FROM ztle_constant
              INTO TABLE @DATA(legacy_vendor)
              WHERE name 'ZS4_LEGACY_VENDOR'.
            IF sy-subrc AND legacy_vendor IS NOT INITIAL.

              LOOP AT bp_id ASSIGNING FIELD-SYMBOL(<lfs_bp_id>).
*                READ TABLE legacy_vendor WITH KEY low = <lfs_bp_id>-bpidentificationnumber TRANSPORTING NO FIELDS.
*                IF sy-subrc = 0.
                IF line_existslegacy_vendor[ low <lfs_bp_id>-bpidentificationnumber ] ).
                  IF NOT dlv_hdr IS INITIAL.
                    DELETE dlv_hdr WHERE supplier <lfs_bp_id>-businesspartner.
                  ELSE.
                    EXIT.
                  ENDIF.
                ENDIF.
              ENDLOOP.
            ENDIF.

            IF NOT dlv_hdr IS INITIAL.
            
  SELECT deliverydocument,                      "vbeln

                     deliverydocumentitem,                  "posnr
                     material,                              "matnr
                     plant,                                 "werks
                     actualdeliveryquantity,                "lfimg
                     baseunit,                              "Meins
                     referencesddocument,                   "vgbel
                     referencesddocumentitem,               "vgpos
                     goodsmovementstatus                    "wbsta
                     FROM i_deliverydocumentitem            "lips
                     INTO TABLE @DATA(dlv_item_final)
                     FOR ALL ENTRIES IN @dlv_hdr
                     WHERE deliverydocument @dlv_hdr-deliverydocument
                     AND   goodsmovementstatus 'A'
                     OR    goodsmovementstatus 'B').
              IF sy-subrc 0.
                SORT dlv_item_final BY referencesddocument ref
erencesddocumentitem.



                SELECT b~purchasingdocument,                      "ebeln
                       b~purchasingdocumentitem,                  "ebelp
                       b~
supplierconfirmationcontrolkey,          "bstae

                       a~purchasingdocumenttype                   "bsart

                       FROM i_purchasingdocumentitem AS b         "ekpo
                       INNER JOIN i_purchasingdocument AS a       "ekko
                       O
b~purchasingdocument a~purchasingdocument

                       FOR ALL ENTRIES IN @dlv_item_final
                       WHERE b~purchasingdocument @dlv_item_final-referencesddocument
                       AND   b~purchasingdocumentitem @dlv_item_final-referencesddocumentitem+1(5)
                       AND   b~supplierconfirmationcontrolkey IN 'Z002' 'Z003' )
                       AND   a~purchasingdocumenttype 'ZDNB'
                       INTO TABLE @DATA(dsp_po).
                IF sy-subrc 0.
                  SORT dsp_po BY pu
rchasingdocument purchasingdocumentitem.


                  LOOP AT dlv_item_final ASSIGNING FIELD-SYMBOL(<lfs_item>).
                    DATA(indexsy-tabix.
*                    READ TABLE dsp_po WITH KEY purchasingdocument = <lfs_item>-referencesddocument
*                                               purchasingdocumentitem = <lfs_item>-referencesddocumentitem+1(5)
*                                               BINARY SEARCH
*                                
               TRANSPORTING NO FIELDS.

*                    IF sy-subrc NE 0.
                    IF NOT line_existsdsp_po[ purchasingdocument <lfs_item>-referencesddocument
                                                purchasingdocumentitem <lfs_item>-referencesddocumentitem+1(5).
                      DELETE dlv_item_final INDEX index.
                    ENDIF.
                  ENDLOOP.
                ENDIF.
              ELSE.
                MESSAGE 'GR is already done for this delivery'(007TYPE 'I'.

                LEAVE LIST-PROCESSING.
              ENDIF.

              CLEARgoodsmvt_item.
              SORT dlv_hdr BY deliverydocument.

              SORT dlv_item_final BY  deliverydocument deliverydocumentitem.


              LOOP AT dlv_hdr ASSIGNING FIELD-SYMBOL(<lfs_dlv_hdr>).
                READ TABLE dlv_item_final ASSIGNING FIELD-SYMBOL(<lfs_dlv_item>WITH KEY deliverydocument <lfs_dlv_hdr>-deliverydocument
                                                                                                             BINARY SEARCH.
                IF sy-subrc 0.
                  DATA(item_indxsy-tabix.

                  goodsmvt_header-pstng_date sy-datum.
                  goodsmvt_header-ref_doc_no <lfs_dlv_item>-referencesddocument.
                  goodsmvt_code-gm_code      gm_code_gr.

                  LOOP AT dlv_item_final ASSIGNING <lfs_item> FROM item_indx.
                    IF <lfs_item>-deliverydocument NE <lfs_dlv_hdr>-deliverydocument.
                      EXIT.
                    ENDIF.
                    APPEND VALUE #(            material             =  <lfs_item>-material
                                               plant                =  <lfs_item>-plant
                                               no_more_gr           =  abap_true
                                               mvt_ind              =  movement_indicator
                                               move_type            =  movement_type_gr
                                               entry_qnt            =  <lfs_item>-actualdeliveryquantity
                                               entry_uom            =  <lfs_item>-baseunit
                                               po_number            =  <lfs_item>-referencesddocument
                                               po_item              =  <lfs_item>-referencesddocumentitem+1(5)
                                               ref_doc              =  <lfs_item>-referencesddocument
                                               ref_doc_it           =  <lfs_item>-referencesddocumentitem+1(5)
                                               deliv_numb           =  <lfs_item>-deliverydocument
                                               deliv_item           =  <lfs_item>-deliverydocumentitem  TO goodsmvt_item.
                  ENDLOOP.

                  IF goodsmvt_item IS NOT INITIAL.
                    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
                      EXPORTING
                        goodsmvt_header  goodsmvt_header
                        goodsmvt_code    goodsmvt_code
                      IMPORTING
                        materialdocument materialdocument
                        matdocumentyear  matdocumentyear
                      TABLES
                        goodsmvt_item    goodsmvt_item
                        return           return.

                    READ TABLE return WITH KEY type 'E' TRANSPORTING NO FIELDS.
                    IF sy-subrc 0.
                      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
                    ELSE.
                      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                        EXPORTING
                          wait abap_true.
                    ENDIF.

ENDIF.
                ENDIF.
              ENDLOOP.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.

Hello experts

,GR-error2.jpg

When I create a Goods Receipt with reference to Purchase Order using BAPI_GOODSMVT_CREATE, the following error message appears.

No goods receipt possible for purchase order <PO number> <line item number>.

But the GR is getting created manually using Transaction MIGO for the same PO number.

Here is the code:

SELECT deliverydocument,             "vbeln
             deliverydocumentitem,         "posnr
             plant                         "werks
             FROM i_deliverydocumentitem   "lips
             INTO TABLE @DATA(dlv_item)
             WHERE deliverydocument IN @inb_delv             " OR deliverydocument IN @delv_num )
             AND plant IN @Store.
      IF sy-subrc 0.
        SELECT deliverydocument,          "vbeln
               supplier                   "lifnr
            FROM i_deliverydocument       "likp
            INTO TABLE @DATA(dlv_hdr)
            FOR ALL ENTRIES IN @dlv_item
            WHERE deliverydocument @dlv_item-deliverydocument
            AND deliverydate IN @s_date_tmp
            AND sddocumentcategory '7'.
        IF sy-subrc 0.

          SELECT businesspartner,              "partner
                 bpidentificationtype,         "type
                 bpidentificationnumber        "idnumber
                 FROM i_bupaidentification     "but0id
                 INTO TABLE @DATA(bp_id)
                 FOR ALL ENTRIES IN @dlv_hdr
                 WHERE businesspartner @dlv_hdr-supplier
                 AND   bpidentificationtype 'GLREG'.
          IF sy-subrc 0.

            SELECT low
              FROM ztle_constant
              INTO TABLE @DATA(legacy_vendor)
              WHERE name 'ZS4_LEGACY_VENDOR'.
            IF sy-subrc AND legacy_vendor IS NOT INITIAL.

              LOOP AT bp_id ASSIGNING FIELD-SYMBOL(<lfs_bp_id>).
*                READ TABLE legacy_vendor WITH KEY low = <lfs_bp_id>-bpidentificationnumber TRANSPORTING NO FIELDS.
*                IF sy-subrc = 0.
                IF line_existslegacy_vendor[ low <lfs_bp_id>-bpidentificationnumber ] ).
                  IF NOT dlv_hdr IS INITIAL.
                    DELETE dlv_hdr WHERE supplier <lfs_bp_id>-businesspartner.
                  ELSE.
                    EXIT.
                  ENDIF.
                ENDIF.
              ENDLOOP.
            ENDIF.

            IF NOT dlv_hdr IS INITIAL.
            
  SELECT deliverydocument,                      "vbeln

                     deliverydocumentitem,                  "posnr
                     material,                              "matnr
                     plant,                                 "werks
                     actualdeliveryquantity,                "lfimg
                     baseunit,                              "Meins
                     referencesddocument,                   "vgbel
                     referencesddocumentitem,               "vgpos
                     goodsmovementstatus                    "wbsta
                     FROM i_deliverydocumentitem            "lips
                     INTO TABLE @DATA(dlv_item_final)
                     FOR ALL ENTRIES IN @dlv_hdr
                     WHERE deliverydocument @dlv_hdr-deliverydocument
                     AND   goodsmovementstatus 'A'
                     OR    goodsmovementstatus 'B').
              IF sy-subrc 0.
                SORT dlv_item_final BY referencesddocument ref
erencesddocumentitem.



                SELECT b~purchasingdocument,                      "ebeln
                       b~purchasingdocumentitem,                  "ebelp
                       b~
supplierconfirmationcontrolkey,          "bstae

                       a~purchasingdocumenttype                   "bsart

                       FROM i_purchasingdocumentitem AS b         "ekpo
                       INNER JOIN i_purchasingdocument AS a       "ekko
                       O
b~purchasingdocument a~purchasingdocument

                       FOR ALL ENTRIES IN @dlv_item_final
                       WHERE b~purchasingdocument @dlv_item_final-referencesddocument
                       AND   b~purchasingdocumentitem @dlv_item_final-referencesddocumentitem+1(5)
                       AND   b~supplierconfirmationcontrolkey IN 'Z002' 'Z003' )
                       AND   a~purchasingdocumenttype 'ZDNB'
                       INTO TABLE @DATA(dsp_po).
                IF sy-subrc 0.
                  SORT dsp_po BY pu
rchasingdocument purchasingdocumentitem.


                  LOOP AT dlv_item_final ASSIGNING FIELD-SYMBOL(<lfs_item>).
                    DATA(indexsy-tabix.
*                    READ TABLE dsp_po WITH KEY purchasingdocument = <lfs_item>-referencesddocument
*                                               purchasingdocumentitem = <lfs_item>-referencesddocumentitem+1(5)
*                                               BINARY SEARCH
*                                
               TRANSPORTING NO FIELDS.

*                    IF sy-subrc NE 0.
                    IF NOT line_existsdsp_po[ purchasingdocument <lfs_item>-referencesddocument
                                                purchasingdocumentitem <lfs_item>-referencesddocumentitem+1(5).
                      DELETE dlv_item_final INDEX index.
                    ENDIF.
                  ENDLOOP.
                ENDIF.
              ELSE.
                MESSAGE 'GR is already done for this delivery'(007TYPE 'I'.

                LEAVE LIST-PROCESSING.
              ENDIF.

              CLEARgoodsmvt_item.
              SORT dlv_hdr BY deliverydocument.

              SORT dlv_item_final BY  deliverydocument deliverydocumentitem.


              LOOP AT dlv_hdr ASSIGNING FIELD-SYMBOL(<lfs_dlv_hdr>).
                READ TABLE dlv_item_final ASSIGNING FIELD-SYMBOL(<lfs_dlv_item>WITH KEY deliverydocument <lfs_dlv_hdr>-deliverydocument
                                                                                                             BINARY SEARCH.
                IF sy-subrc 0.
                  DATA(item_indxsy-tabix.

                  goodsmvt_header-pstng_date sy-datum.
                  goodsmvt_header-ref_doc_no <lfs_dlv_item>-referencesddocument.
                  goodsmvt_code-gm_code      gm_code_gr.

                  LOOP AT dlv_item_final ASSIGNING <lfs_item> FROM item_indx.
                    IF <lfs_item>-deliverydocument NE <lfs_dlv_hdr>-deliverydocument.
                      EXIT.
                    ENDIF.
                    APPEND VALUE #(            material             =  <lfs_item>-material
                                               plant                =  <lfs_item>-plant
                                               no_more_gr           =  abap_true
                                               mvt_ind              =  movement_indicator
                                               move_type            =  movement_type_gr
                                               entry_qnt            =  <lfs_item>-actualdeliveryquantity
                                               entry_uom            =  <lfs_item>-baseunit
                                               po_number            =  <lfs_item>-referencesddocument
                                               po_item              =  <lfs_item>-referencesddocumentitem+1(5)
                                               ref_doc              =  <lfs_item>-referencesddocument
                                               ref_doc_it           =  <lfs_item>-referencesddocumentitem+1(5)
                                               deliv_numb           =  <lfs_item>-deliverydocument
                                               deliv_item           =  <lfs_item>-deliverydocumentitem  TO goodsmvt_item.
                  ENDLOOP.

                  IF goodsmvt_item IS NOT INITIAL.
                    CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
                      EXPORTING
                        goodsmvt_header  goodsmvt_header
                        goodsmvt_code    goodsmvt_code
                      IMPORTING
                        materialdocument materialdocument
                        matdocumentyear  matdocumentyear
                      TABLES
                        goodsmvt_item    goodsmvt_item
                        return           return.

                    READ TABLE return WITH KEY type 'E' TRANSPORTING NO FIELDS.
                    IF sy-subrc 0.
                      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
                    ELSE.
                      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                        EXPORTING
                          wait abap_true.
                    ENDIF.

ENDIF.
                ENDIF.
              ENDLOOP.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,696

Get the answer to your question here (asked and answered many times): https://www.google.com/search?q=M7036+BAPI_GOODSMVT_CREATE%20site:sap.com 

Read only

0 Likes
1,684

Hi Sandra,

Already checked it, can't find any solution, this error is coming in output screen

 

GR-error1.jpg

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,635

You probably missed one, so what did you check exactly?

Read only

0 Likes
1,633

Just commented out this line:

 ref_doc              =  <lfs_item>-referencesddocument,

now it works, probably i was passing PO two times inside FM

 

Rangan_0-1728330612730.png