1. 程式人生 > >SAP FB02 BAPI

SAP FB02 BAPI

更多內容關注公眾號:SAP Technical

對於會記憑證文字更改,前臺Tcode:FB02,後臺呼叫BAPI:FI_ITEMS_MASS_CHANGE
有測試過更改文字欄位:SGTXT,ZLSPR

DATA:WA_BSEG LIKE BSEG,
IT_BUZTAB TYPE TPIT_T_BUZTAB WITH HEADER LINE,
IT_FLDTAB TYPE TPIT_T_FNAME WITH HEADER LINE,
IT_ERRTAB TYPE TPIT_T_ERRDOC WITH HEADER LINE.

  WA_BSEG-ZLSPR = 'V'."你要更改的值

  IT_BUZTAB-BUKRS  = LS_BSEG-BUKRS.
  IT_BUZTAB-BELNR  = LS_BSEG-BELNR.
  IT_BUZTAB-GJAHR  = LS_BSEG-GJAHR.
  IT_BUZTAB-BUZEI  = LS_BSEG-BUZEI.
  IT_BUZTAB-BSCHL  = '31'. "過賬碼
  IT_BUZTAB-UMSKZ = ''."特別總賬標示
  APPEND IT_BUZTAB .

  IT_FLDTAB-FNAME = 'ZLSPR'."要更改的欄位
  IT_FLDTAB-AENKZ  = 'X'."給該欄位打上標示
  APPEND IT_FLDTAB.

*該函式是一行一行改的

  CALL FUNCTION 'FI_ITEMS_MASS_CHANGE'
    EXPORTING
      S_BSEG     = WA_BSEG
    IMPORTING
      ERRTAB     = IT_ERRTAB[]
    TABLES
      IT_BUZTAB  = IT_BUZTAB
      IT_FLDTAB  = IT_FLDTAB
    EXCEPTIONS
      BDC_ERRORS = 1
      OTHERS     = 2.
  IF SY-SUBRC <> 0.
  ENDIF

IF IT_ERRTAB IS NOT INITIAL.
  READ TABLE IT_ERRTAB INDEX 1.*BDC訊息處理.
  CLEAR L_TEXT.
  CALL FUNCTION 'FORMAT_MESSAGE'
    EXPORTING
      ID        = IT_ERRTAB-ERR-MSGID
      NO        = IT_ERRTAB-ERR-MSGNR
      V1        = IT_ERRTAB-ERR-MSGV1
      V2        = IT_ERRTAB-ERR-MSGV2
      V3        = IT_ERRTAB-ERR-MSGV3
      V4        = IT_ERRTAB-ERR-MSGV4
    IMPORTING
      MSG       = L_TEXT
    EXCEPTIONS
      NOT_FOUND = 1
      OTHERS    = 2.

  CONCATENATE LS_BSEG-BELNR L_TEXT INTO L_TEXT.
  E_STATUS = 'E'.
  E_MESS = L_TEXT.

ELSE.
  E_STATUS = 'S'.
  E_MESS = '成功!'.
  
ENDIF.

由於這個函式是一行一行改資料的,可能會出現憑證被鎖,解決方法:拷貝原函數出來,原函式中的:

call transaction 'FB02'  using    bdcdata
                         mode     bdcmode
                         update   'A'
                         messages into msgtab. 

改為

call transaction 'FB02'  using    bdcdata
                         mode     bdcmode
                         update   'S'
                         messages into msgtab. 

同步更新,但是比較慢點 。