http://wiki.sdn.sap.com/wiki/display/ERPSCM/How+To+Goods+Movements+with+BAPI
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
(see the documentation for the ABAP keyword CALL FUNCTION).
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.
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.