Application Development and Automation 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: 
Read only

BAPI and Ztable COMMIT

Former Member
0 Likes
3,879

Dear Experts

In a RFC, our codes are as follows:

     CALL FUNCTION 'BAPI_GOODSMVT_CREATE'

     ...

     ...

     ...

     CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

     ...

     ...

     MODIFY Ztable FROM TABLE it_ztable.

     COMMIT WORK.



We wonder to know that how do we pack above actions into a LUW for the consistency of commit or rollback.

16 REPLIES 16
Read only

Arun_Prabhu_K
Active Contributor
0 Likes
3,182

Hello KuoTing Chu.

     You perform commit if both BAPI and MODIFY statement are successful.

     For instance,

     * After BAPI_GOODSMVT_CREATE, call BAPI_TRANSACTION_COMMIT only if the return internal table of BAPI_GOODSMVT_CREATE is initial.

     * After MODIFY statement, perform COMMIT WORK only if sy-subrc = 0.

Regards.

Read only

0 Likes
3,182

Dear

Read only

0 Likes
3,182

Hi all,

I think you don't even need the COMMIT WORK; IF the BAPI BAPI_GOODSMVT_CREATE is successful do the MODIFY ZTABLE and if THIS is successful call the BAPI_TRANSACTION_COMMIT. I believe this would achieve the best consistency.

Cheers,

Custodio

Read only

0 Likes
3,182

Hi Custodio de Oliveira,

What if we want to modify ztable after BAPI is successful?

How can we do a rollback for ztable if not BAPI is unsuccessful?

Thanks,

Sharath

Read only

0 Likes
3,182

Hi Sharath

You are commiting both changes at one. so if your BAPI fails you wont update the custom table and wont call commit after it so no data gets updated

Nabheet

Read only

0 Likes
3,182

Hi Sharath,

Exactly what Nabheet says. So it would look something like this:

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'.

   IF sy-subrc EQ 0. "or maybe check return table

     MODIFY ztable FROM TABLE it_ztable.

     IF sy-subrc EQ 0.

       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'" commits change in BAPI and ZTABLE

     ELSE.

       bapi_transaction_rollback"rolls back changes in BAPI and ZTABLE

     ENDIF.

   ELSE.

    

     bapi_transaction_rollback. "Did not update table, rolls back change from BAPI.

   ENDIF

Cheers,

Custodio

Read only

0 Likes
3,182

Hi

I have followed your suggestion and written sample codes to create sales order.

  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'

    EXPORTING

      order_header_in    = lt_order_header

    TABLES

      return            = lt_return     

      order_items_in    = lt_order_item

      order_partners    = lt_order_partner

      order_schedules_in = lt_order_schdl.

  READ TABLE lt_return WITH KEY TYPE    = 'S'

                                ID      = 'V1'

                                NUMBER  = '311'.

  IF SY-SUBRC EQ 0.

    MODIFY ZT_PROG_MSG FROM TABLE lt_msg.

    IF SY-SUBRC EQ 0.

      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

        EXPORTING

          WAIT  = 'X'

        IMPORTING

          RETURN = lt_return2.

    ELSE.

      IF SY-SUBRC NE 0.

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

      ENDIF.

    ENDIF.

  ELSE.

    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

  ENDIF.

And I manually adjust duplication number range for causing an update error.

In debug mode, I check the return type in "lt_return", and then I can get 'S'.

However, after BAPI_TRANSACTION_COMMIT, there is update error for sales order creation, but our Ztable(ZT_PROG_MSG)is still updated successfully. How can we assure consistency, if there is any system error making bapi commit failure?

Read only

ThangaPrakash
Active Contributor
0 Likes
3,182

Hello KuoTing,

Instead of COMMIT WORK, you can use COMMIT WORK AND WAIT.

COMMIT WORK- Asynchronous update.

COMMIT WORK AND WAIT - Synchronous update.

There are two sy-subrc possible for COMMIT WORK AND WAIT.

sy-subrc = 0 (Update Successful)

sy-subrc = 4 (Update Unsuccessful)

So after COMMIT WORK AND WAIT do a sy-subrc check like below.

COMMIT WORK AND WAIT.

IF sy-subrc EQ 4.

ROLLBACK WORK.

ENDIF.

Regards,

Thanga

Read only

Former Member
0 Likes
3,182

After writing your code whats Error u got ?

Tell me in details the Error ,,,,

Read only

0 Likes
3,182

RFC can get successfful message.

Error message are as follows

But Our Ztable still has be updated.

Read only

0 Likes
3,182

Any suggestion ?

Read only

RaymondGiuseppi
Active Contributor
0 Likes
3,182

Don't execute the database update in the program, create an update task FM (V1) which execute the INSERT/UPDATE statement. Call this FM IN


UPDATE TASK before the COMMIT.

CALL FUNCTION 'BAPI_GOODSMVT_CREATE'.
CALL FUNCTION 'Z_ZTABLE_UPDATE' IN UPDATE TASK
   EXPORTING
     it_ztable = it_ztable.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

The FM code looking like

FUNCTION z_ztable_update.
*"----------------------------------------------------------------------
*"  IMPORTING
*"     REFERENCE(IT_ZTABLE) TYPE ZTABLE_T
*"----------------------------------------------------------------------
   MODIFY ztable FROM TABLE it_ztable.
   IF sy-subrc NE 0.
     MESSAGE a999(zzzz) WITH 'ZTABLE'.
   ENDIF.
ENDFUNCTION.

Regards,

Raymond

Read only

0 Likes
3,182

Hello Raymond,

Can you please explain me how does UPDATE TASK works.

Will it run in the same work process of the function module call.

Does it work as synchronous or asynchronous ?

Regards,

TP

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,182

Can you please explain me how does UPDATE TASK works.

Read the SAP documentation, it's all in there.

Read only

0 Likes
3,182

Hello Suhas,

I read it, but doesn't understand clearly. I just want to know it in simple words.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,182

A couple of things -


UPDATE TASK before the COMMIT.

What did you mean by this?


*"---------------------------------------------------------------------- 

*"  IMPORTING 

*"     REFERENCE(IT_ZTABLE) TYPE ZTABLE_T 

*"----------------------------------------------------------------------

Update FMs can't have parameters passed by reference

Cheers,

Suhas