Wednesday, March 18, 2020

BAPI for FI documents post (BAPI_ACC_DOCUMENT_POST).


BAPI BAPI_ACC_DOCUMENT_POST.

BAPI_ACC_DOCUMENT_POST.



This post FI documents in a program. To this need, it would be better if you use the standard BAPI from SAP, rather than using BDC. Why? Because BDC is based on screen to screen, and if functional does customizing screens for FI transactions, it is difficult for developer to change their BDC according to customizing functional.

This FI documents post BAPI is commonly used BAPI_ACC_DOCUMENT_POST.

Below we discuss the following parameters need to be filled in to run this BAPI.

 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)."optional
    ld_documentheader-fis_period = sy-datum+4(2)."optional
    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.


If this document post is successful, after that the program will commit the transaction and will import the document number into the local data ld_key. If  BAPI fails, after that program will rollback the transaction.



No comments:

Post a Comment