12-18-2013 3:01 AM
Hi, Experts
Now I'm developing the function module which calls 2 other function modules by itself as below.
---- FM which I'm developing ----
Call Function 'FM1'
Call Function 'FM2'
But I need to write 'Commit work' or 'BAPI_TRANSACTION_COMMIT' between FM1 and FM2 because FM2 is based on commit of FM1.
---- FM which I'm developing ----
Call Function 'FM1'.
IF SY-SUBRC = 0.
COMMIT WORK. "or BAPI_TRANSACTION_COMMIT
ENDIF.
Call Function 'FM2'.
And then, I have a problem.
Even though FM2 is failed, commit of FM1 is completed. I would like to rollback the result of FM1 and FM2 when failing.
I heard that assigning 'COMMIT WORK AND WAIT' into between FM1 and FM2 achieves my above requirement.
But I don't know how to write it.
Let me know how to use COMMIT WORK AND WAIT or the way to rollback the result of all function modules.
Thanks,
yuki
12-18-2013 3:15 AM
Hello Yuki,
Regards to your problem, I give you my code which I have given a pieces of my code as the example, and hopefully you can find anything useful from it .
form uploadData.
DATA: it_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
EXPORTING
salesdocument = bapi_vbeln
ORDER_HEADER_IN = ls_ord_header
order_header_inx = ls_ord_header_inx
tables
return = it_return
.
READ TABLE it_output WITH KEY vbeln = bapi_vbeln.
* APPEND LINES OF it_return TO lt_return.
LOOP AT it_return.
CALL FUNCTION 'ZFM_MESSAGE_APPEND'
EXPORTING
* MSGID =
* MSGNO =
msgty = it_return-type
* MSGV1 =
* MSGV2 =
* MSGV3 =
* MSGV4 =
MESSAGE = it_return-message
obj = bapi_vbeln
operate = '上传'
.
ENDLOOP.
READ TABLE it_return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
it_output-flag = 'C601'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'x'
IMPORTING
return = it_return.
* APPEND LINES OF it_return TO lt_return.
LOOP AT it_return.
CALL FUNCTION 'ZFM_MESSAGE_APPEND'
EXPORTING
* MSGID =
* MSGNO =
msgty = it_return-type
* MSGV1 =
* MSGV2 =
* MSGV3 =
* MSGV4 =
MESSAGE = it_return-message
obj = bapi_vbeln
operate = '数据库操作'
.
ENDLOOP.
READ TABLE it_return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
it_output-flag = 'C501'.
ELSE.
it_output-flag = 'C301'.
ENDIF.
ENDIF.
MODIFY TABLE it_output.
endform.
***************************************************************************************************
12-18-2013 3:15 AM
Hello Yuki,
Regards to your problem, I give you my code which I have given a pieces of my code as the example, and hopefully you can find anything useful from it .
form uploadData.
DATA: it_return TYPE TABLE OF bapiret2 WITH HEADER LINE.
CALL FUNCTION 'BAPI_SALESORDER_CHANGE'
EXPORTING
salesdocument = bapi_vbeln
ORDER_HEADER_IN = ls_ord_header
order_header_inx = ls_ord_header_inx
tables
return = it_return
.
READ TABLE it_output WITH KEY vbeln = bapi_vbeln.
* APPEND LINES OF it_return TO lt_return.
LOOP AT it_return.
CALL FUNCTION 'ZFM_MESSAGE_APPEND'
EXPORTING
* MSGID =
* MSGNO =
msgty = it_return-type
* MSGV1 =
* MSGV2 =
* MSGV3 =
* MSGV4 =
MESSAGE = it_return-message
obj = bapi_vbeln
operate = '上传'
.
ENDLOOP.
READ TABLE it_return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
it_output-flag = 'C601'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'x'
IMPORTING
return = it_return.
* APPEND LINES OF it_return TO lt_return.
LOOP AT it_return.
CALL FUNCTION 'ZFM_MESSAGE_APPEND'
EXPORTING
* MSGID =
* MSGNO =
msgty = it_return-type
* MSGV1 =
* MSGV2 =
* MSGV3 =
* MSGV4 =
MESSAGE = it_return-message
obj = bapi_vbeln
operate = '数据库操作'
.
ENDLOOP.
READ TABLE it_return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc EQ 0.
it_output-flag = 'C501'.
ELSE.
it_output-flag = 'C301'.
ENDIF.
ENDIF.
MODIFY TABLE it_output.
endform.
***************************************************************************************************
12-18-2013 3:40 AM
Additionally, I always recommend to use "commit work and wait" statement where ever you want to upload the data to database especially "BAPI" . And that doesn't mean that "commit work" is not very useful.
Regards to the difference between them, please read > ref https://help.sap.com/abapdocu_70/en/ABAPCOMMIT.htm#&ABAP_VARIANT_1@1@ <
"This executes all high-priority (VB1) update function modules in the order of their registration and in a common database LUW. If you do not specify the addition AND WAIT, the program does not wait until the update work process has executed it (asynchronous updating), but instead is resumed immediately after COMMIT WORK. However, if the addition AND WAIT is specified, program processing afterCOMMIT WORK will not continue until the update work process has executed the high-priority update function modules (synchronous updating). "
12-18-2013 5:30 AM
Hi, Hai
Here is very helpful answer for me!
Thank you.
But I still can't achieve it.
There is the detailed source code image.
The result of BAPI_OUTB_CHANGE_DELIVERY(FM1) is commit after BAPI_TRANSACTION_ROLLBACK even though I write BAPI_TRANSACTION_COMMIT with WAIT option according to your mention between FM1 and FM2.
Which is wrong point do you think?
* For batch split
CALL FUNCTION 'BAPI_OUTB_CHANGE_DELIVERY'
Exporting
header_data
header_control
delivery
Tables
header_partner
item_data
item_control
return.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
Exporting
WAIT = 'X'.
* For PGI
CALL FUNCTION 'WS_UPDATE_DELIVERY_2'
Exporting
Vbkok_wa
Delivery
If_error_message_send.
IF SY-SUBRC <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
ENDIF.
12-18-2013 5:53 AM
I think you'd better to write your code in this way:
( Please notice the BOLD statement)
CALL FUNCTION 'BAPI_OUTB_CHANGE_DELIVERY'
Exporting
header_data = header_data
header_control = header_control
delivery = delivery
Tables
header_partner = header_partner
item_data = item_data
item_control = item_control
return = return.
READ TABLE return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
Exporting
WAIT = 'X'
IMPORTTING
return = return.
READ TABLE return WITH KEY type = 'S' TRANSPORTING NO FIELDS.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ENDIF.