Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Can we use BAPI inside Customer Exits or BADIs?

former_member721373
Participant
0 Kudos

Hello everyone,,

I have a question regarding the use of BAPI inside BADI, customer exits or any enhancement techniques. We need to use BAPI_COMMIT FM after calling the bapi but if we are using the BAPI inside anyone of the enhancement techniques, then in that case what should be our approach since we should not use commit inside any enhancement?

Also, my second question is that if the bapi returns a locking error here then what should we do? Use deque FM in the enhancement? Wouldn’t that release locks for other objects as well!

Any help would be very much appreciated here!

Thanks in advance.

9 REPLIES 9

DominikTylczyn
Active Contributor
0 Kudos

Hello eihtu

First of all, it is not carved in stone that you must not call BAPIs or execute commit work in enhancements. It all depends on the nature of the enhancement and when it is called. I do agree however, it is safer to avoid commits in enhancements.

The solution is to wrap BAPI call and subsequent BAPI_TRANSACTION_COMMIT is an RFC function module. Then call the function from the enhancement in a separate LUW (logical unit of work) using:

CALL FUNCTION func IN BACKGROUND TASK
                   AS SEPARATE UNIT

See SAP Help on AS SEPARATE UNIT addition.

That trick also solves locking problems in BAPI processing.

Best regards

Dominik Tylczynski

Hello Dominik,

Sorry for the late response! Thank you so much for your answer.

So if the Badi changes fails in between, and a rollback happens. The changes by the BAPI in this FM will also be rolled back? Or it is commited? I am new to SAP LUW’s scenarios. So if you can help me understand whether the Background task will be part of the same SAP LUW or a separate one in this case?


thanks in advance

0 Kudos

eihtu You should either call BAPI_TRANSACTION_COMMIT or BAPI_TRANSACTION_ROLLBACK depending on the BAPI call results, if it is successful or not.

0 Kudos

Hi Dominik,

I created RFC to update BP and added BAPI_TRANSACTION_COMMIT in it after BP update BAPI. Called RFC as below inside BAPI but it seems it does not consider BAPI_TRANSACTION_COMMIT added in the RFC. Commit happens only when it receives commit outside RFC..

CALL FUNCTION func IN BACKGROUND TASK AS SEPERATE UNIT

0 Kudos
eihtu

Can you share the code of your RFC function that wraps up the BADI?

0 Kudos

ls_centraldata-centralblock = space.

ls_centraldata_x-centralblock = 'X'.

CALL FUNCTION 'BAPI_BUPA_CENTRAL_CHANGE'
EXPORTING
businesspartner = iv_partner
centraldata = ls_centraldata
centraldata_x = ls_centraldata_x
TABLES
return = et_return.
IF sy-subrc = 0.
READ TABLE et_return TRANSPORTING NO FIELDS
WITH KEY type = 'E'.
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDIF.

…… Inside BADI

CALL FUNCTION 'ZRFC' IN BACKGROUND TASK
AS SEPARATE UNIT
EXPORTING
iv_partner = lv_bp_num
TABLES
et_return = lt_return.

0 Kudos

I would drop

IF sy-subrc = 0.
...
ENDIF.

right after the BAPI call.

venkateswaran_k
Active Contributor
0 Kudos

As a rule of thumb,

Inside the BADi - We should not use any BAPI that finished with Commit . Because BADi is are in generally in betwen process where we put of deviations and/or validations.

Anything that commits or locks - will create incosistencies.

for your second question also, though DEQUE releases the lock, it may create incosistencies too.

anjalihora
Explorer
0 Kudos

Yes we can use bapi inside badi .

I have worked on one requirement where badi is used to populate a bapi as some fields were not available in bapi so that it can be executed successfully.