Tuesday, January 17, 2017

Goods Movements with BAPI

BAPIs in Inventory Management

There are two BAPIs for posting Goods Movements:
  • BAPI_GOODSMVT_CREATE (universal BAPI for posting Goods Movements)
  • BAPI_GOODSMVT_CANCEL (only for reverting Goods Movements)
And two in combination with for the transactional handling:
  • BAPI_TRANSACTION_COMMIT (Commit the posting, general)
  • BAPI_TRANSACTION_ROLLBACK (Rollback the posting, general)

Introduction

You can use the BAPI_GOODSMVT_CREATE to create a material document for a goods movement. You can only create one material document each time you call up the method. You can use a simulation function before posting the goods movement to check how the data is updated in the database. The standard system is configured so that the goods movement is posted immediately, without prior simulation.

Data population

Ensure that you transfer the data in the same way as it is managed in the database.
For example:
  • Material number 18-character with leading zeros
  • Batches with uppercase letters
  • Ensure that the header data of the material document includes the posting date for all transactions/events listed below. Populate the PSTNG_DATE field (import structure GOODSMVT_HEADER) in the interface BAPI2017_GM_HEAD_01 for this purpose.
The following information about the material document that is to be created is passed on to the BAPI:
  • A structure with the header data
  • A structure with the code for the movement
  • A table with the item data
  • A table with the serial numbers
  • The posting is made by the function module MB_CREATE_GOODS_MOVEMENT.
Confirmations
Messages are returned in the Return parameter. The parameter documentation shows the return codes and their meanings.

Further information

The BAPI can only perform your function if no error messages were generated in the Return table. This is the case if the header data and all the items were processed successfully.
Transaction control is not implemented. The calling program therefore has to execute the Commit Work itself after this method has been successfully invoked (in other words, if no errors were reported in the Return parameter).

Which fields must be populated during the various transactions/events?

The following example scenarios illustrate which fields have to be populated for which business transactions/events (code).
When calling the Goods Movement BAPI you have to specify a Goods Movement Code. The following codes are possible:
GM_CODE
TCODE
Description
01
MB01
Goods receipt for purchase order
02
MB31
Goods receipt for production order
03
MB1A
Goods issue
04
MB1B
Transfer posting
05
MB1C
Other goods receipt
06
MB11
Reversal of goods movements
07
MB04
Subsequent adjustment with regard to a subcontract order
Entering the movement indicator
Depending on the transaction (GM_CODE), you must enter the following in the movement indicator field:
GM_Code 01 (Goods receipt for purchase order): B
GM_Code 02 (Goods receipt for production order): F
For all other transactions, leave the field blank.

GM_Code 01: Goods receipt for purchase order

Purchase order known

The following fields must be populated:
  • Purchase order
  • Purchase order item
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry or quantity proposal
The following fields may have to be populated (this is not a complete list):
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and no automatic batch assignment has been set up)
  • Storage location (if no storage location us specified in the purchase order item)
The following fields can be populated (this is not a complete list):
  • Stock type
  • Item text
  • Unloading point
  • Delivery completed indicator
  • The following fields must not be populated (this is not a complete list):
  • Account assignment fields (the account assignment is adopted from the purchase order)
  • Reservation
  • Receiving/issuing material
  • Receiving/issuing plant
  • Receiving/issuing storage location
In the case of a purchase order item with the item category "subcontracting", only the GR item is to be passed on in the interface. The GI items are determined automatically in the system.

Purchase order unknown: Shipping notification known

The following fields must be populated:
  • Delivery
  • Delivery item
  • Movement type
Other fields as under "Purchase order known". The system determines the relevant purchase order item via the delivery/delivery item.

Purchase order unknown: Purchase order is to be created automatically

The following fields must be populated:
  • Material number
  • Plant
  • Storage location
  • Vendor
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry
The following fields may have to be populated (this is not a complete list):
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and batches are not assigned automatically)
The following fields can be populated (this is not a complete list):
  • Stock type
  • Item text
  • Unloading point
You cannot populate the following fields (this is not a complete list):
  • Account assignment fields (automatic creation of purchase order items with account assignment is not supported)
  • Reservation
  • Receiving/issuing material
  • Receiving/issuing plant
  • Receiving/issuing storage location

Purchase order unknown: A purchase order is not to be created

The following fields must be populated:
  • Material number
  • Plant
  • Storage location
  • Vendor
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry
The following fields may have to be populated in some cases (this is not a complete list):
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and automatic assignment of batch numbers is not set)
The following fields can be populated (this is not a complete list):
  • Special stock indicator
  • Item text
  • Unloading point
  • Goods recipient
The following fields cannot be populated (this is not a complete list):
  • Account assignment fields (automatic creation of purchase order items with account assignment is not supported)
  • Reservation
  • Receiving/issuing material
  • Receiving/issuing plant
  • Receiving/issuing storage location
  • Receiving/issuing batch
The input combinations listed above for the GM_CODE 01 can be combined within an input table so that they can be posted in a material document. Items can also be posted for different purchase orders, provided that all the purchase orders relate to the same vendor.
Transfer of subcontracting components
When the end product (ordered material of a subcontract order) is posted using movement type 101, the populated unique identification of the document line (LINE_ID) is additionally needed for the transfer of the subcontracting components.
More information on LINE_ID and PARENT_ID
The following fields must be populated for the components:
  • Unique identification of the document line
  • Identification of the immediately superordinate line
  • Material number
  • Plant
  • Storage location
  • Quantity in unit of entry
The following fields can be filled for the components:
  • All other fields in the method that are included in this list.
The following fields cannot be populated for the components:
  • All fields that are not included in the list above. The movement indicator must be initial.
  • Additional fields in goods-receipt-based invoice verification
  • When you post a goods receipt to purchase order with movement type 102, an issue with reference to a previously posted goods issue, the following fields must also be transferred:
  • REF_DOC_IT (LFPOS): Item of a reference document
  • REF_DOC (LFBNR): Document number of a reference document
  • REF_DOC_YR (LFBJA): Fiscal year of a reference document
  • Only if you transfer these fields can you be sure that the link to the original goods movement is retained.

GM_Code 02: Goods receipt for production order

The following fields have to be populated:
  • Order
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry or quantity proposal
The following fields may have to be populated in some cases (this is not a complete list):
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and automatic batch number assignment is not set)
  • Storage location (if storage location has not been specified in the order)
  • The following fields can be populated (this is not a complete list):
  • Order item (co-product)
  • Stock type
  • Item text
  • Unloading point
  • "Delivery completed" indicator
The following fields cannot be populated (this is not a complete list):
  • Account assignment fields (the account assignments are adopted from the purchase order)
  • Reservation
  • Receiving/issuing material
  • Receiving/issuing plant
  • Receiving/issuing storage location
  • Receiving/issuing batch

GM_Code 03: Goods issue

Goods issue without reference to a reservation

The following fields must be populated:
  • Material number
  • Plant
  • Storage location
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry
  • The following fields may have to be populated in some cases (this is not a complete list):
  • Special stock (e.g. sales order, project, vendor etc.)
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and automatic batch number assignment is not set)
  • Account assignment fields
The following fields can be populated (this is not a complete list):
  • Special stock indicator
  • Item text
  • Unloading point
  • Goods recipient
The following fields cannot be populated (this is not a complete list):
  • Reservation
  • Receiving/issuing material
  • Receiving/issuing plant
  • Receiving/issuing storage location
  • Receiving/issuing batch

Goods issue with reference to a reservation

The following fields must to be populated:
  • Reservation number
  • Reservation item
  • Record type of the reservation
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry
The following fields may have to be populated in some cases (this is not a complete list):
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and automatic batch number assignment is not set)
  • Storage location (if not planned in the reservation)
The following fields can be populated (this is not a complete list):
  • Special stock indicator
  • Item text
  • Unloading point
  • Goods recipient
The following fields cannot be populated (this is not a complete list):
  • Movement type
  • Material
  • Plant
  • Account assignment fields

GM_Code 04: Transfer posting

Transfer posting without reference to a reservation

The following fields must be populated:
  • Material number
  • Plant
  • Storage location
  • Movement type
  • Movement indicator
  • Quantity in unit of entry
  • ISO code unit of measurement for unit of entry
The following fields may have to be populated in some cases (this is not a complete list):
  • Receiving material
  • Receiving plant
  • Receiving storage location
  • Receiving batch (if material is handled in batches and you want to change batches)
  • Receiving/issuing special stock (e.g. sales order, project, vendor etc.)
  • Minimum shelf life expiration date (if set up in the system)
  • Reason for movement (if set up in the system)
  • Batch (if the material is handled in batches and automatic batch number assignment is not set)
The following fields can be populated (this is not a complete list):
  • Special stock indicator
  • Item text
  • Account assignment fields (for automatic posting lines: expense/income from stock transfer)
The following fields cannot be populated (this is not a complete list):
  • Reservation
  • Transfer posting with reference to a reservation
  • See goods issue with reference to a reservation

GM_Code 05: Other goods receipts

Other goods receipt without reference to a reservation
  • See goods issue without reference to a reservation
    Other goods receipt with reference to a reservation
  • See goods issue with reference to a reservation

GM_Code 06: Reversal of goods movements

You can use the BAPI_GOODSMVT_CANCEL to reverse goods movements. But you may also want to cancel a goods movement without referring to a material document (see the Cancel method). If you want to work without referring to the document, you have to populate the BAPI_GM_ITEM_CREATE-XSTOB field in the interface (import table GOODSMVT_ITEM). In this case, the system converts the movement type assigned in the interface to the reversal movement type. Alternatively, you can set the reversal movement type in the interface. In this case, the BAPI_GM_ITEM_CREATE_XSTOB field must be transferred blank.
If you want to cancel a goods movement with reference to a reservation (without using the Cancel method) you can only transfer the BAPI_GM_ITEM_CREATE_XSTOB field if it is populated. Movement types are not transferred when you post with reference to a reservation, as the movement type is contained in the reservation item.

GM_Code 07: Subsequent adjustment with regard to a subcontract order

Ordered material of subcontract order
The following fields must be populated:
  • Unique identification of document line (can be freely assigned)
  • Posting date in document header
  • Document number of purchasing document
  • Item number of purchasing document
  • Material number check
  • Plant check
  • Movement type (select movement type 121)
  • Movement indicator (must be O)
  • Further entries are not allowed. The entry quantity for the ordered material of a subcontract order must be zero.
Components
The following fields must be populated:
  • Unique identification of document line (can be freely assigned)
  • Identification of the immediately superordinate line
  • Plant
  • Material number check
  • Quantity in unit of entry; can also be negative
  • The following fields can be populated:
  • Storage location
  • All other fields in the method that are included in this list.
The following fields may not be populated:
  • All fields that are not in the list above. The movement indicator must be blank.

More information on LINE_ID and PARENT_ID

The structure BAPI2017_GM_ITEM_CREATE (BAPI communication structure: Create material document item) of the BAPI_GOODSMVT_CREATE BAPI includes the following two fields:
  • LINE_ID (Unique identification of the document line)
  • PARENT_ID (Identification of the immediately superior line)
These two fields define the hierarchie levels of all items. To import the data for the subcontracting components, these fields must have entries. All information about the goods movement to be posted is transferred to BAPI BAPI_GOODSMVT_CREATE using the BAPI2017_GM_ITEM_CREATE structure.
An application that calls the BAPI_GOODSMVT_CREATE BAPI, has the following choices:
Goods receipt for subcontract order with components
  • Only transfer of end product (ordered material of subcontract order) with movement type 101, all components are therefore read with the ME_READ_COMP_GOODS_RECEIPT function.
  • Transfer of end product (ordered material of subcontract order) and components, the components are posted with the data from the BAPI2017_GM_ITEM_CREATE (BAPI communication structure: Create material document item) structure.
  • Subsequent adjustment, the end product and all components must be transferred to the BAPI_GOODSMVT_CREATE BAPI.
How for the last two cases the BAPI2017_GM_ITEM_CREATE structure is to be filled, is explained in the following examples.
Nomenclature of the examples
In the following, you will find a list of fields mentioned in the following examples. The name of the particular field in the interface is specified in brackets.
  • Field Interface Description
  • LINE_ID (MB_LINE_ID): Unique identification of document line
  • PARENT_ID (MB_PARENT_ID): Identification of immediately superior line
  • MATERIAL (MATNR): Material number
  • PLANT (WERKS_D): Plant
  • STGE_LOC (LGORT_D): Storage location
  • ENTRY_QNT (ERFMG): Quantity in unit of entry
  • MOVE_TYPE (BWART): Movement type (Inventory Management)
  • MVT_IND (KZBEW): Movement indicator
  • PO_NUMBER (BSTNR EBELN): Purchase order number
  • PO_ITEM (EBELP): Item number of purchasing document
Examples
Transfer of end product (ordered material of subcontract order) and components
LINE_ID
PARENT_ID
MATERIAL
PLANT
STGE_LOC
ENTRY_QNT
MOVE_TYPE
MVT_IND
PO_NUMBER
PO_ITEM
000001
000000
END
0001
0001
10
101
B
4500000012
10
000002
000001
COMP1
0001
0001
15
-
-
-
-
000003
000001
COMP1
0001
0001
20
-
-
-
-
Subsequent adjustment
LINE_ID
PARENT_ID
MATERIAL
PLANT
STGE_LOC
ENTRY_QNT
MOVE_TYPE
MVT_IND
PO_NUMBER
PO_ITEM
000001
000000
END
0001
0001
0
-
O
4500000012
10
000002
000001
COMP1
0001
0001
15
-
-
-
-
000003
000001
COMP1
0001
0001
20
-
-
-
-
Note that
the ENTRY_QNT field can be negative in subsequent adjustment. This is then allowed if a goods receipt instead of a goods issue is posted for a component. In the BAPI for the MVT_IND field, the special stock indicator O is supported.
In the example above, only a few fields are used, although the BAPI2017_GM_ITEM_CREATE structure has over one hundred fields. Only a few of these fields are useful for subcontracting components; they can be found in the GOCOMP (Components for subcontracting) structure (see this list).

Additional fields in goods-receipt-based invoice verification

When you post a subsequent adjustment, the following fields must also be transferred:
  • REF_DOC_IT (LFPOS): Item of a reference document
  • REF_DOC (LFBNR): Document number of a reference document
  • REF_DOC_YR (LFBJA): Fiscal year of a reference document
    Only if you transfer these fields can you be sure that the link to the original goods movement is retained.

How to test he BAPI

You can use transaction SE37 to execute/ test the BAPI. You can also specify a test sequence of calling several BAPI in a series via the menu. For a real simulation you should create a test report with Tx SE38. See example followed on.

Example Code for calling the GM BAPI to post a 561 Goods Movement

ZBAPI_GDSMVT
*&---------------------------------------------------------------------*
*& Report  Z_BAPI_GDSMVT
*&
*&---------------------------------------------------------------------*
REPORT  z_bapi_gdsmvt.
DATA: ls_mmdochdr LIKE bapi2017_gm_head_01,
      lt_gm       TYPE STANDARD TABLE OF bapi2017_gm_item_create,
      ls_gm       LIKE bapi2017_gm_item_create,
      lt_ret      TYPE STANDARD TABLE OF bapiret2,
      ls_ret      LIKE bapiret2,
      ls_hdr      LIKE bapi2017_gm_head_ret,
      ls_ser      LIKE bapi2017_gm_serialnumber,
      lt_ser      LIKE  STANDARD TABLE OF bapi2017_gm_serialnumber.
PARAMETERS: p_pstdat LIKE bapi2017_gm_head_01-pstng_date,
            p_docdat  LIKE bapi2017_gm_head_01-doc_date,
            p_matnr    LIKE bapi2017_gm_item_create-material,
            p_plant    LIKE bapi2017_gm_item_create-plant,
            p_sloc     LIKE bapi2017_gm_item_create-stge_loc,
            p_quant    LIKE bapi2017_gm_item_create-entry_qnt,
            p_batch    LIKE bapi2017_gm_item_create-batch.
START-OF-SELECTION.
* Prepare Data for Goods Movement
  ls_mmdochdr-pstng_date = p_pstdat.
  ls_mmdochdr-doc_date = p_docdat.
  ls_gm-move_type = '561'.
  ls_gm-material = p_matnr.
  ls_gm-plant = p_plant.
  ls_gm-stge_loc = p_sloc.
  ls_gm-entry_qnt = p_quant.
  ls_gm-batch = p_batch.
  APPEND ls_gm TO lt_gm.
  CLEAR ls_gm.
* Call BAPI
  CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
    EXPORTING
      goodsmvt_header  = ls_mmdochdr
      goodsmvt_code    = '05'
    IMPORTING
      goodsmvt_headret = ls_hdr
    TABLES
      goodsmvt_item    = lt_gm
      return           = lt_ret.
* If no error, commit
  IF lt_ret IS INITIAL.
    WRITE: 'Material Document posted:', ls_hdr-mat_doc, ' ', ls_hdr-doc_year.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
*   Alternative COMMIT WORK.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
*   Alternative ROLLBACK WORK.
    WRITE: 'Error during posting Material document:', /.
    LOOP AT lt_ret INTO ls_ret.
      WRITE: ls_ret-type,
             ls_ret-id,
             ls_ret-number,
             ls_ret-message,
             ls_ret-log_no,
             ls_ret-log_msg_no,
             ls_ret-message_v1,
             ls_ret-message_v2,
             ls_ret-message_v3,
             ls_ret-message_v4.
    ENDLOOP.
  ENDIF.

Calling the BAPI several times within one roll area

When you want to post depending Goods Movements in a series like GR and immediate transfer posting or GR and GI for the same material. The stock is only read from the database in the case of an actual goods issue. For a goods receipt, the data is read from the buffer and thereby at the time before the last posting in the same roll area. A goods receipt, for example with movement type 315, automatically posts an implicit goods issue in the stock in transfer. This situation is not taken into account in the current design. You may get an error message like M7 021 (Deficit of ... stock). To avoid this, you have to call the BAPI in a different way:
Make sure that the program buffer is deleted by changing the roll area. You can accomplish this by logging off and then logging in again for the BAPI process. If the BAPI is called from an ABAP program, you can also use the command
  • CALL FUNCTION func DESTINATION 'NONE'.
(see the documentation for the ABAP keyword CALL FUNCTION).

Here you have to adhere to the following procedure:

  1. The call of the BAPI, for example, BAPI_GOODSMVT_CREATE and the subsequent call of BAPI BAPI_TRANSACTION_COMMIT or in case of an error of BAPI BAPI_TRANSACTION_ROLLBACK has to be included in a function module.
  2. This function module must be called with command CALL FUNCTION func DESTINATION 'NONE'. As a result, the system opens a new roll area.
  3. Then you must implement function module RFC_CONNECTION_CLOSE.This function module closes the roll area of the function module.As a result, a new roll area is opened when you call a function module with DESTINATIN 'NONE' for the next time, and thus, for example, no more internal buffer data is available.

Example 1:

Test
Program Test.
...
LOOP.
....
  CALL FUNCTION func1 DESTINATION 'NONE'.
  CALL FUNCTION RFC_CONNECTION_CLOSE.
....
ENDLOOP.
Func1
FUNCTION func1.
....
   CALL FUNCTION 'BAPI_GOODSMVT_CREATE'.
   ...
   IF 'no errors'.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
   ELSE.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
   ENDIF.
...
ENDFUNCTION.

Example 2:

ZBAPI_GDSMVT
*&---------------------------------------------------------------------*
*& Report  ZBAPI_GDSMVT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  zbapi_gdsmvt.
* DATA: ...
* PARAMETERS: ...
START-OF-SELECTION.
* Prepare data for first Goods Movement
* Call BAPI to create Goods Movement
CALL FUNCTION 'BAPI_GOODSMVT_CREATE' DESTINATION 'NONE'
* If no error, commit
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' DESTINATION 'NONE'
    EXPORTING
      wait = 'X'.
* ELSE
* Error handling 
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' DESTINATION 'NONE'.
* Close RFC connection
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = 'NONE'.
* Prepare data for next Goods Movement
* Call BAPI to create Goods Movement
CALL FUNCTION 'BAPI_GOODSMVT_CREATE' DESTINATION 'NONE'
* If no error, commit
  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' DESTINATION 'NONE'
    EXPORTING
      wait = 'X'.
* ELSE
* Error handling 
  CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK' DESTINATION 'NONE'.
* Close RFC connection
  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = 'NONE'.
Caution!
The commit work executed in func1 processes all function modules of DESTINATION 'NONE' that were called in 'update task'.If function modules in 'update task' are called in the calling program, these do not lead to a database update.In this case, you have to execute a further commit work in the calling program.

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...