BAPI Post Document FI

Kebutuhan untuk melakukan posting document FI dalam suatu program sering saya temui. Untuk memnuhi kebutuhan ini, alangkah baiknya bila menggunakan BAPI standard dari SAP, daripada menggunakan BDC. Kenapa? Karena BDC itu based on screen to screen, dan apabila functional melakukan costumizing screen untuk transaction FI, repotlah kita para abap untuk mengubah BDC-nya sesuai dengan costumizing functional tadi.

BAPI yang biasa digunakan untuk posting document FI adalah BAPI_ACC_DOCUMENT_POST. berikut parameter yang perlu diisi untuk menjalankan BAPI ini.

  data: ld_documentheader like bapiache09,
        lt_accountgl like table of bapiacgl09 with header line,
        lt_currencyamount like table of bapiaccr09 with header line,
        lt_return like table of bapiret2 with header line.

  data: ld_comp_code type bukrs,
        ld_gl1 type hkont,
        ld_gl2 type hkont,
        ld_gsber type gsber,
        ld_costcent type kostl,
        ld_profcent type prctr,
        ld_waers type waers,
        ld_amount type bapiwrbtr,
        ld_key like bapiache09-obj_key,
        ls_return like bapiret2,
        ls_nodoc like bseg-belnr.

*  - Documentheader
    ld_documentheader-username = sy-uname.
    ld_documentheader-header_txt = 'Header Text'.
    ld_documentheader-comp_code = ld_comp_code.
    ld_documentheader-doc_date = sy-datum.
    ld_documentheader-pstng_date = sy-datum.
    ld_documentheader-fisc_year = sy-datum(4).
    ld_documentheader-fis_period = sy-datum+4(2).
    ld_documentheader-doc_type = 'SA'.
    ld_documentheader-ref_doc_no = 'Ref Doc No.'.

*  - Accountgl
*  -- Item 1
    lt_accountgl-itemno_acc = 1.
    lt_accountgl-gl_account = ld_gl1.
    lt_accountgl-item_text = 'Text GL 1'.
    lt_accountgl-doc_type = 'SA'.
    lt_accountgl-comp_code = ld_documentheader-comp_code.
    lt_accountgl-bus_area = ld_gsber.
    lt_accountgl-pstng_date = sy-datum.
    lt_accountgl-value_date = sy-datum.
    lt_accountgl-costcenter = ld_costcent.
    lt_accountgl-profit_ctr = ld_profcent.
    append lt_accountgl.

*  -- Item 2
    lt_accountgl-itemno_acc = 2.
    lt_accountgl-gl_account = ld_gl2.
    lt_accountgl-item_text = 'Text GL 2'.
    clear lt_accountgl-costcenter.
    append lt_accountgl.

*  - Currencyamount
*  -- Item 1
    lt_currencyamount-itemno_acc = 1.
    lt_currencyamount-currency = ld_waers.
    lt_currencyamount-amt_doccur = ld_amount.
    if lt_currencyamount-currency eq 'IDR'.
      lt_currencyamount-amt_doccur =
        lt_currencyamount-amt_doccur * 100.
    endif.
    append lt_currencyamount.

*  -- Item 2
    lt_currencyamount-itemno_acc = 2.
    lt_currencyamount-amt_doccur = -1 * lt_currencyamount-amt_doccur.
    append lt_currencyamount.

*  - Call BAPI
    call function 'BAPI_ACC_DOCUMENT_POST'
      exporting
        documentheader = ld_documentheader
      importing 
        obj_key        = ld_key
      tables
        accountgl      = lt_accountgl
        currencyamount = lt_currencyamount
        return         = lt_return.

    read table lt_return with key type = 'S'.
    if sy-subrc eq 0.
*  - Commit BAPI
    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait   = 'X'
      importing
        return = ls_return.
      if not ls_return is initial.
*  - ROLLBACK
        call function 'BAPI_TRANSACTION_ROLLBACK'
          importing
            return = fs_return.
      else.
        ld_nodoc = ld_key(10).
      endif.
    else.
*  - ROLLBACK
      call function 'BAPI_TRANSACTION_ROLLBACK'
        importing
          return = fs_return.
    endif.

Bila posting berhasil, maka program akan meng-commit transaksi dan akan meng-import nomer document ke dalam local data ld_key. Namu bila BAPI gagal, maka program akan me-rollback transaksi.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s