SAP 在增强里面使用多线程STARTING NEW TASK调用BAPI_TRANSACTION_COMMIT或者COMMIT WORK

业务背景:当有时候需要在SAP标准的增强出口里面调用其他的BAPI进行订单的创建或者修改的时候,避免不了需要使用BAPI_TRANSACTION_COMMIT或者COMMIT WORK进行处理,但是直接使用COMMIT是有问题的,需要使用多线程进行处理避免COMMIT对订单的保存有影响

处理方式: 

1. 定义一个RFC函数,在RFC函数里面调用BAPI创建或者修改进行commit处理

SAP 在增强里面使用多线程STARTING NEW TASK调用BAPI_TRANSACTION_COMMIT或者COMMIT WORK_第1张图片

2. 在增强出口里面使用STARTING NEW TASK调用以上创建的RFC函数,调用格式如下:

CALL FUNCTION 'ZMM_PO_CHANGE'  STARTING NEW TASK 'PURCHASEORDER_CHANGE'

注意: start new task后面的名字可以任意命名

具体代码如下:

CALL FUNCTION 'ZMM_PO_CHANGE' STARTING NEW TASK 'PURCHASEORDER_CHANGE'
      EXPORTING
        in_aufnr       = header_imp-aufnr
      TABLES
        return         = lt_return_c
              .

-----------------------------------------------------------------------------------------
*注意函数需要设置成RFC函数

FUNCTION zmm_po_change.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IN_AUFNR) TYPE  AUFNR
*"  TABLES
*"      RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
  TYPES: BEGIN OF lty_po,
           ebeln TYPE ekpo-ebeln,
         END OF lty_po.
  DATA: ls_ekkn   TYPE ekkn,
        lt_ekkn   TYPE TABLE OF ekkn,
        lt_ekpo   TYPE TABLE OF ekpo,
        ls_ekpo   TYPE ekpo,
        ls_ekpo_c TYPE ekpo,
        lt_return TYPE TABLE OF bapiret2,
        ls_return TYPE bapiret2,
        lt_item   TYPE TABLE OF bapimepoitem,
        ls_item   TYPE bapimepoitem,
        lt_itemx  TYPE TABLE OF bapimepoitemx,
        ls_itemx  TYPE bapimepoitemx,
        lt_po     TYPE TABLE OF lty_po,
        ls_po     TYPE lty_po.

  CLEAR: ls_ekkn,
         lt_ekkn,
         lt_ekpo,
         ls_ekpo,
         ls_ekpo_c,
         lt_return,
         ls_return,
         lt_item,
         ls_item,
         lt_itemx,
         ls_itemx,
         lt_po,
         ls_po.

  SELECT  * INTO TABLE lt_ekkn
      FROM ekkn
      WHERE aufnr = in_aufnr.
  IF lt_ekkn IS NOT INITIAL.
    SELECT * INTO TABLE lt_ekpo
      FROM ekpo
      FOR ALL ENTRIES IN lt_ekkn
      WHERE ebeln = lt_ekkn-ebeln
        AND ebelp = lt_ekkn-ebelp
        AND erekz <> 'X'.
    IF sy-subrc = 0 AND lt_ekpo IS NOT INITIAL.
      LOOP AT lt_ekpo INTO ls_ekpo.
        ls_po-ebeln = ls_ekpo-ebeln.
        COLLECT ls_po INTO lt_po.
        CLEAR: ls_po.
      ENDLOOP.

      LOOP AT lt_po INTO ls_po.
        CLEAR: lt_item,
               ls_item,
               lt_itemx,
               ls_itemx,
               lt_return,
               ls_return..

        LOOP AT lt_ekpo INTO ls_ekpo_c WHERE ebeln = ls_po-ebeln.
          ls_item-po_item = ls_ekpo_c-ebelp.
          ls_item-final_inv = 'X'.
          APPEND ls_item TO lt_item.
          CLEAR: ls_item.

          ls_itemx-po_item = ls_ekpo_c-ebelp.
          ls_itemx-final_inv = 'X'.
          APPEND ls_itemx TO lt_itemx.
          CLEAR: ls_itemx.
        ENDLOOP.

        CALL FUNCTION 'BAPI_PO_CHANGE'
          EXPORTING
            purchaseorder = ls_po-ebeln
*           POHEADER      =
*           POHEADERX     =
*           POADDRVENDOR  =
*           TESTRUN       =
*           MEMORY_UNCOMPLETE            =
*           MEMORY_COMPLETE              =
*           POEXPIMPHEADER               =
*           POEXPIMPHEADERX              =
*           VERSIONS      =
*           NO_MESSAGING  =
*           NO_MESSAGE_REQ               =
*           NO_AUTHORITY  =
*           NO_PRICE_FROM_PO             =
*           PARK_UNCOMPLETE              =
*           PARK_COMPLETE =
*         IMPORTING
*           EXPHEADER     =
*           EXPPOEXPIMPHEADER            =
          TABLES
            return        = lt_return
            poitem        = lt_item
            poitemx       = lt_itemx
*           POADDRDELIVERY               =
*           POSCHEDULE    =
*           POSCHEDULEX   =
*           POACCOUNT     =
*           POACCOUNTPROFITSEGMENT       =
*           POACCOUNTX    =
*           POCONDHEADER  =
*           POCONDHEADERX =
*           POCOND        =
*           POCONDX       =
*           POLIMITS      =
*           POCONTRACTLIMITS             =
*           POSERVICES    =
*           POSRVACCESSVALUES            =
*           POSERVICESTEXT               =
*           EXTENSIONIN   =
*           EXTENSIONOUT  =
*           POEXPIMPITEM  =
*           POEXPIMPITEMX =
*           POTEXTHEADER  =
*           POTEXTITEM    =
*           ALLVERSIONS   =
*           POPARTNER     =
*           POCOMPONENTS  =
*           POCOMPONENTSX =
*           POSHIPPING    =
*           POSHIPPINGX   =
*           POSHIPPINGEXP =
*           POHISTORY     =
*           POHISTORY_TOTALS             =
*           POCONFIRMATION               =
*           SERIALNUMBER  =
*           SERIALNUMBERX =
*           INVPLANHEADER =
*           INVPLANHEADERX               =
*           INVPLANITEM   =
*           INVPLANITEMX  =
*           POHISTORY_MA  =
          .

        APPEND LINES OF lt_return TO return.

        READ TABLE lt_return INTO ls_return WITH KEY type = 'S'
                                                     id = '06'
                                                     number = '023'.
        IF sy-subrc = 0.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'
*           IMPORTING
*             RETURN        =
            .

        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
*           IMPORTING
*             RETURN        =
            .

        ENDIF.
      ENDLOOP.
    ENDIF.
  ENDIF.


ENDFUNCTION.

你可能感兴趣的:(sap,abap)