导航

http://wiki.sdn.sap.com/wiki/display/ERPSCM/How+To+Goods+Movements+with+BAPI

 

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.