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

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 sysubrc 0.
        SELECT deliverydocument,          “vbeln
               supplier                   “lifnr
            FROM i_deliverydocument       “likp
            INTO TABLE @DATA(dlv_hdr)
            FOR ALL ENTRIES IN @dlv_item
            WHERE deliverydocument @dlv_itemdeliverydocument
            AND deliverydate IN @s_date_tmp
            AND sddocumentcategory ‘7’.
        IF sysubrc 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_hdrsupplier
                 AND   bpidentificationtype ‘GLREG’.
          IF sysubrc 0.

            SELECT low
              FROM ztle_constant
              INTO TABLE @DATA(legacy_vendor)
              WHERE name ‘ZS4_LEGACY_VENDOR’.
            IF sysubrc AND legacy_vendor IS NOT INITIAL.

              LOOP AT bp_id ASSIGNING FIELDSYMBOL(<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_hdrdeliverydocument
                     AND   goodsmovementstatus ‘A’
                     OR    goodsmovementstatus ‘B’).
              IF sysubrc 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_finalreferencesddocument
                       AND   b~purchasingdocumentitem @dlv_item_finalreferencesddocumentitem+1(5)
                       AND   b~supplierconfirmationcontrolkey IN ‘Z002’ ‘Z003’ )
                       AND   a~purchasingdocumenttype ‘ZDNB’
                       INTO TABLE @DATA(dsp_po).
                IF sysubrc 0.
                  SORT dsp_po BY pu
rchasingdocument purchasingdocumentitem.


                  LOOP AT dlv_item_final ASSIGNING FIELDSYMBOL(<lfs_item>).
                    DATA(indexsytabix.
*                    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 FIELDSYMBOL(<lfs_dlv_hdr>).
                READ TABLE dlv_item_final ASSIGNING FIELDSYMBOL(<lfs_dlv_item>WITH KEY deliverydocument <lfs_dlv_hdr>deliverydocument
                                                                                                             BINARY SEARCH.
                IF sysubrc 0.
                  DATA(item_indxsytabix.

                  goodsmvt_headerpstng_date sydatum.
                  goodsmvt_headerref_doc_no <lfs_dlv_item>referencesddocument.
                  goodsmvt_codegm_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 sysubrc 0.
                      CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.
                    ELSE.
                      CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
                        EXPORTING
                          wait abap_true.
                    ENDIF.

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

Scroll to Top