# SAP BAPI_GOODSMVT_CREATE ABAP Code Generator

> Source: https://netray.co/tools/sap-bapi-goodsmvt-create-abap-code-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in plant, material, quantity and movement type to get a working ABAP skeleton for posting a goods movement through the standard BAPI.

## Category

sap

## Erp

SAP ECC / S/4HANA

## Audience

- SAP ABAP developer
- SAP MM consultant
- SAP integration developer

## Tool

**Kind:** template

**Output:** code

**Language:** abap

**Fields:**

#### Report name

**Key:** program

**Type:** text

**Default:** ZR_POST_GOODS_MOVEMENT

#### Goods movement code

**Key:** gm_code

**Type:** select

**Options:**

- 01
- 02
- 03
- 04
- 05
- 06
- 07

**Default:** 05

#### Movement type

**Key:** move_type

**Type:** text

**Default:** 501

#### Material number

**Key:** material

**Type:** text

**Default:** FG-100045

#### Plant

**Key:** plant

**Type:** text

**Default:** 1010

#### Storage location

**Key:** stge_loc

**Type:** text

**Default:** 0001

#### Quantity

**Key:** qty

**Type:** number

**Default:** 25

#### Unit of measure

**Key:** uom

**Type:** text

**Default:** EA

**Blocks:**

#### Goods movement posting

**Body:** REPORT {{program}}.

DATA: ls_header  TYPE bapi2017_gm_head_01,
      ls_code    TYPE bapi2017_gm_code,
      ls_headret TYPE bapi2017_gm_head_ret,
      lt_item    TYPE STANDARD TABLE OF bapi2017_gm_item_create,
      ls_item    TYPE bapi2017_gm_item_create,
      lt_return  TYPE STANDARD TABLE OF bapiret2,
      ls_return  TYPE bapiret2,
      lv_error   TYPE abap_bool.

ls_header-pstng_date = sy-datum.
ls_header-doc_date   = sy-datum.
ls_header-header_txt = 'Posted by {{program}}'.
ls_code-gm_code      = '{{gm_code}}'.

ls_item-material  = '{{material}}'.
ls_item-plant     = '{{plant}}'.
ls_item-stge_loc  = '{{stge_loc}}'.
ls_item-move_type = '{{move_type}}'.
ls_item-entry_qnt = {{qty}}.
ls_item-entry_uom = '{{uom}}'.
APPEND ls_item TO lt_item.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
  EXPORTING
    goodsmvt_header  = ls_header
    goodsmvt_code    = ls_code
  IMPORTING
    goodsmvt_headret = ls_headret
  TABLES
    goodsmvt_item    = lt_item
    return           = lt_return.

LOOP AT lt_return INTO ls_return WHERE type CA 'EAX'.
  lv_error = abap_true.
  WRITE: / ls_return-type, ls_return-message.
ENDLOOP.

IF lv_error = abap_true.
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait = abap_true.
  WRITE: / 'Material document:', ls_headret-mat_doc, ls_headret-doc_year.
ENDIF.

#### Goods movement codes

**Body:** 01 goods receipt for purchase order, 02 goods receipt for production order, 03 goods issue, 04 transfer posting, 05 other goods receipts, 06 reversal of goods movements, 07 subsequent adjustment for subcontracting. Movement type {{move_type}} must be valid for code {{gm_code}}.

**Notes:** Skeleton for ECC and S/4HANA on-premise. Items for purchase order receipts need PO number and item (PO_NUMBER, PO_ITEM). In S/4HANA the long material field MATERIAL_LONG may be required for 40-character material numbers. Test with the TESTRUN parameter first and verify structure names in SE37 for your release.

## Content

**Intro:** BAPI_GOODSMVT_CREATE is the standard way to post goods receipts, issues and transfers from your own code or from middleware. This generator gives you the full call, error handling and commit sequence.

**Sections:**

#### How to use the generator

**Body:** Set the goods movement code, movement type and item data, then copy the code into a report. Run it first in a test client with TESTRUN set, read the RETURN table, and only then run it for real. For purchase order receipts add the reference fields to the item, and set the movement type to 101.

#### The BAPI commit pattern

**Body:** Standard BAPIs do not commit on their own. You check the RETURN table for messages of type E, A or X, then call BAPI_TRANSACTION_COMMIT when it is clean or BAPI_TRANSACTION_ROLLBACK when it is not. Using WAIT = X makes the commit synchronous, so the material document exists in the database when your next statement runs. Without it, a read straight afterwards can miss the document.

#### Reading the result

**Body:** A successful call returns the material document number and year in GOODSMVT_HEADRET and a success message in RETURN. Check the document in MB03 and the stock effect in MMBE. Errors such as missing storage location, blocked material or period closed come back as messages in RETURN rather than short dumps.

#### Common mistakes

**Body:** Calling the commit even when RETURN has errors, passing quantities in the wrong unit, and posting from several parallel jobs on the same material and plant, which causes lock errors.

**Bullets:**

- Convert material numbers to internal format if needed
- Handle lock errors with a retry
- Never write to stock tables directly

**Cta:** Netray helps SAP teams build integrations with human approval on every write-back. If you are wrapping posting BAPIs in an API or an assistant, ask for a design review.

**Faq:**

#### What does TESTRUN do in BAPI_GOODSMVT_CREATE?

**Answer:** Setting TESTRUN to X runs all checks and returns the messages you would get on a real posting, but writes nothing to the database. It is the safest way to validate an integration with real data before going live.

#### Why do I need BAPI_TRANSACTION_COMMIT?

**Answer:** BAPIs are written not to commit, so the caller controls the transaction and can group several BAPI calls. If you forget the commit, the posting is rolled back when your program ends and no material document remains.

#### How do I post a goods receipt against a purchase order?

**Answer:** Use goods movement code 01, movement type 101, and fill PO_NUMBER and PO_ITEM in the item. The system derives material and plant from the order. Provide quantity and unit, and make sure the order item is not already fully delivered.

#### Does this work for S/4HANA?

**Answer:** Yes, the BAPI remains available in S/4HANA on-premise, backed by the material document tables of the new data model. Newer cloud releases push you toward released APIs and OData services, so check the release notes before choosing.

## Search terms

bapi_goodsmvt_create example, bapi goods movement abap sample code, bapi_transaction_commit example, post goods receipt abap bapi, bapi_goodsmvt_create 501, goodsmvt_code values, bapiret2 error handling abap, abap material document create

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
