Está en la página 1de 10

Post FI document using BAPI

By Rajesh Kumar, Infosys Technologies

INTRODUCTION:

The accounting documents record changes in values in a company code arising from accounting
transactions. They consist of one or more line items (postings), each of which represents an individual
transaction posted to an account. When posting an accounting document, the SAP system updates the
transaction figures in the accounts to which the document is posted.An accounting document is a
representation within the SAP System of the document (for example, an invoice) that triggered the
posting.
BAPI ‘BAPI_ACC_DOCUMENT_POST’ acts as the interface between your code and standard
transactions FB01/FB50.
This document explains the various steps involved to post a FI document using BAPI
‘BAPI_ACC_DOCUMENT_POST’ from an excel file.This is helpful to SAP Technical consultants as it
gives the details of all the required parameters and data in document posting and validation to check if
the GL account in which we are posting amounts is present in SAP database. Transaction code for FI
documents are FB01/FB50(Create), FB02(Change) and FB03(Display). In one document, more than 999
line item cannot be posted because BAPI can post up to 999 lines in one document. The file should be
balanced for the document to be posted i.e. credit amount and debit amount should be equal. In FI,
header table for document data is BKPF and line item table is BSEG.

STEPS:

Various steps involved in posting a document from a file are:

1. Upload data from excel file into an internal table using FM ‘GUI_UPLOAD’ as explained in subroutine
UPLOAD_FORM_EXCEL.
2. After we upload excel data into an internal table we need split the data into field patterns into header
data and line item data as explained in subroutine ‘SPLIT_INTO_RECORDS’. This splitting is done
according to my file format. In header data in the file in line no.2, posting date and header company
code. That is why I have used case when 2. And all the other information is present at the lines as
described by CASE statement. Line item details start from line number 14, that’s why LOOP AT
t_upload INTO w_upload FROM 14is used. User can split the data according to their file format.
3. After the splitting, loop at the line item table and validate the GL account numbers. If the GL accounts
are valid, Populate the line item data into BAPI GL and currency tables otherwise append that GL
account number into error message table. This is explained in subroutines ‘VALIDATE_GL’ and
‘POPULATE_BAPI’.

7. these are automatically populated by BAPI. This step is explained in subroutine ‘POST_DOCUMENT’. OBJ_TYPE and OBJ_SYS. Note:In case of intercompany code postings. If this BAPI do not return any error message. In case of intercompany postings. commit the work using ‘BAPI_TRANSACTION_COMMIT’. if the exchange rate between 2 currencies is given in the file. Find out line item company code currency key and compare if the line item currency key and header currency key given is same or different. SUMMARY: This document explains the various steps involved in FI document posting from excel file. If these are different then we need to populate ‘EXCHANGE_RATE’ field of currency table if it is given in the file. Also Field status group configurations of GL accounts are not considered by BAPI as it is bypassed by BAPI. company code currency and group currency. . While providing header data in W_HEADER work area do not provide OBJ_KEY. if initial. In case of the intra company code postings all the currencies are same so the amounts are same In all3 currencies. Check for sy-subrc.4. Main steps involved are uploading and splitting data into internal table. Post the document using BAPI ‘BAPI_ACC_DOCUMEN_POST’. Above step is required because a document is posted in 3 currencies document currency. 5. A sample source code is given for the references. Next step in document posting is to check the data using BAPI ‘BAPI_ACC_DOCUMEN_CHECK’. 6. but these are considered when doing posting using FB50. go to next step otherwise display error message given by BAPI. Currency value conversions will be done by the BAPI. Populate data into BAPI tables and call BAPI to post the documents and committing the transaction. in which line item and Header Company codes are different. Due to this we need to give and exchange rate base on which the BAPI will convert the amounts. programmer should pass only the exchange rate given. But in case of inter company code postings the currencies are different so the amounts are also different. Otherwise BAPI itself will fetch the exchange rate between 2 currencies and do the conversion.

CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = lw_filename filetype = 'ASC' TABLES data_tab = t_upload EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. w_dummy(1) TYPE c. lw_filename = p_file. *&--------------------------------------------------------------------* *&Form UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* FORM upload_from_excel. *. t_bukrs TYPE STANDARD TABLE OF type_bukrs. END OF type_upload. "Work table used for upload rec(500) TYPE c.End of change @ECC ENDFORM. "#EC NEEDED w_item_curr TYPE bapiaccr09.SOURCE CODE: *Global DATA declarations TYPES: BEGIN OF type_upload. *--upload data from excel to internal table DATA: lw_filename TYPE string. w_item_gl TYPE bapiacgl09. w_upload TYPE type_upload. t_return TYPE STANDARD TABLE OF bapiret2. t_item_curr TYPE STANDARD TABLE OF bapiaccr09." UPLOAD_FROM_EXCEL *&--------------------------------------------------------------------* *&Form SPLIT_INTO_RECORDS *&--------------------------------------------------------------------* . t_upload TYPE STANDARD TABLE OF type_upload. w_header TYPE bapiache09. DATA:t_posting TYPE STANDARD TABLE OF . *Global work area declarations DATA: w_postingsTYPE . t_item_gl TYPE STANDARD TABLE OF bapiacgl09. w_bukrs TYPE type_bukrs. t_filename TYPE STANDARD TABLE OF type_filename. REFRESH t_upload.

lv_account. *Split header data LOOP AT t_upload INTO w_upload.FORM split_into_records. lv_product TYPE char20. DATA: lv_doc_date TYPE sy-datum. lv_period TYPE char2. lv_kursf TYPE char9. SPLIT w_upload AT c_tab INTO w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_dummyw_dummyw_dummy w_header-ac_doc_no w_dummy. lv_amount TYPE char30. lv_pstng_date TYPE sy-datum. lv_customer. lv_plant TYPE char10. lv_cust_hie2 TYPE hiezu02. WHEN 8. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy lv_doc_date w_dummyw_dummy w_header-header_txt w_dummyw_dummy w_header-comp_code w_dummy. SPLIT w_upload AT c_tab INTO w_dummyw_dummy . * end of insert @007 WHEN 4. lv_plant. lv_product. lv_cust_hie2. CASE sy-tabix. SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy lv_pstng_date w_dummyw_dummy w_header-ref_doc_no w_dummyw_dummy w_header-doc_type w_dummy. WHEN 6. lv_account TYPE char10. CLEAR:lv_doc_date. lv_pstng_date. lv_aufnr TYPE char12. lv_amount. lv_customer TYPE kunnr. lv_period. * start of insert @007 WHEN 2. lv_kursf.

ENDCASE. *Split line item data LOOP AT t_upload INTO w_upload FROM 14. CLEAR lv_account. "@008 EXIT.w_dummyw_dummy lv_period w_dummyw_dummy g_jtype w_dummyw_dummy"@008 w_header1-curr "@008 w_dummy. "@008 WHEN 10. SPLIT w_upload AT c_tab INTO w_postings-company lv_account w_postings-ttype w_postings-pkey lv_amount w_postings-ccent w_postings-prcent w_postings-description w_postings-assignment w_postings-tax_code w_postings-juridiction_code lv_aufnr w_postings-psa lv_plant w_postings-wbs_element lv_product w_postings-product_hie w_postings-meins w_postings-quantity w_postings-asgm w_postings-asgc w_postings-billing_type lv_customer lv_cust_hie2 w_postings-order_reason w_postings-dsd_return_rsn w_postings-reson1 w_postings-item_sales w_postings-sales_order w_postings-dist_channel w_postings-sales_org w_postings-division w_postings-shipping_type w_postings-partner_pc w_postings-item_category . SPLIT w_upload AT c_tab INTO w_dummyw_dummy w_dummyw_dummy g_rdate w_dummyw_dummy w_header-reason_rev w_dummyw_dummy "@008 lv_kursf "@008 w_dummy.

*Fecth the currency for company codes SELECT bukrs waers FROM t001 INTO TABLE t_curr FOR ALL ENTRIES IN t_bukrs WHERE bukrs EQ t_bukrs-bukrs. ENDIF. SORT t_curr BY bukrs. lv_customer. lt_gl_flag TYPE STANDARD TABLE OF ltype_gl. bukrs TYPE bukrs. IF t_curr IS NOT INITIAL. lv_aufnr. TYPES: BEGIN OF ltype_gl. lv_amount.lv_cust_hie2." SPLIT_INTO_RECORDS *&---------------------------------------------------------------------* *& Form VALIDATE_GL *&---------------------------------------------------------------------* FORM validate_gl. CLEAR: w_postings. END OF ltype_gl. ENDIF. saknr TYPE saknr. ENDLOOP. w_postings-amount = lv_amount. ENDFORM.w_postings-trading_partner w_postings-market w_postings-sales_office w_postings-country w_postings-resp w_postings-value_date w_postings-trade_type w_postings-controlling_area w_postings-cost_object w_postings-cust_hie1 w_postings-cust_hie3 w_postings-cust_hie4 w_postings-cust_hie5 w_postings-cust_hie6 w_postings-cust_hie7 w_postings-ship_to_party w_postings-fin_plning_lvl w_postings-ext_ic_mod_std w_postings-vv160_intrprtation w_postings-vv161_intrprtation w_postings-crm_marketing w_dummy. lv_product. lv_plant. . IF t_bukrs IS NOT INITIAL. APPEND w_postings TO t_posting. lv_account. * END OF @007 IF NOT ( w_postings-pkey IS INITIAL ). DELETE ADJACENT DUPLICATES FROM t_bukrs COMPARING bukrs. ENDIF. lw_gl_flag TYPE ltype_gl. DATA: lt_postings_temp TYPE STANDARD TABLE OF type_item. xintb TYPE xintb.

APPEND lw_message TO t_message. * lt_postings_temp = t_posting.lv_msg TYPE char100. lw_message-text = lv_msg. lw_message TYPE type_message. ELSE. lv_counter. CLEAR lw_message. CONCATENATE text-004 w_postings-account text-006 INTO lv_msg SEPARATED BY space. SELECT "#EC CI_SGLSELECT bukrs saknr xintb FROM skb1 INTO TABLE lt_gl_flag FOR ALL ENTRIES IN lt_postings_temp WHERE bukrs = lt_postings_temp-company AND saknr = lt_postings_temp-account. ELSE. g_error_flag = c_x. g_error_flag = c_x. ENDFORM. IF lt_postings_temp IS NOT INITIAL. * Else display the error message for GL account. READ TABLE lt_gl_flag INTO lw_gl_flag WITH KEY saknr = w_postings-account bukrs = w_postings-company. APPEND lw_message TO t_message. "error message lv_msg * Else display the error message for GL account. lw_message-mes_type = c_e. DELETE ADJACENT DUPLICATES FROM lt_postings_temp COMPARING company account. lw_message-text = lv_msg. PERFORM populate_bapi USING lv_counter. **Intialize counter lv_counter = 1." VALIDATE_GL *&---------------------------------------------------------------------* *& Form POPULATE_BAPI . lv_counter TYPE posnr_acc. CLEAR: lw_gl_flag. SORT lt_postings_tempBY company account. lt_gl_flag. lt_postings_temp = t_posting. ENDIF. * Increase counter by one lv_counter = lv_counter + 1. IF lw_gl_flag-xintb = c_x. CLEAR lw_message. ENDIF. IF sy-subrc IS INITIAL. lw_message-mes_type = c_e. ENDIF. ENDLOOP. LOOP AT t_posting INTO w_postings. REFRESH: lt_postings_temp.

w_item_gl-gl_account = w_postings-account. w_item_gl-profit_ctr = w_postings-prcent. APPEND w_item_gl TO t_item_gl. w_item_curr-curr_type = c_doc_curr. w_item_gl-pstng_date = w_header-pstng_date. w_item_curr. g_cross_flag = c_x. w_item_gl-item_text = w_postings-description. *Fill Debit line item data ELSEIF w_postings-pkey= 40. w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-profit_ctr = w_postings-prcent. APPEND w_item_curr TO t_item_curr. w_item_gl-gl_account = w_postings-account. *Fill Credit line item data IF w_postings-pkey=50. **Populate line item details IF w_postings-company NE w_header-comp_code.*&---------------------------------------------------------------------* FORM populate_bapi USING rlv_counter TYPE posnr_acc. w_item_gl-itemno_acc = rlv_counter. w_curr. w_item_gl-trade_id = w_postings-trading_partner. w_item_gl-costcenter = w_postings-ccent. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. * Clear variables CLEAR: w_postings. ENDIF. w_item_gl-taxjurcode = w_postings-juridiction_code. CLEAR w_item_curr. . w_item_gl-orderid = w_postings-internal_order. w_item_gl-cs_trans_t = w_postings-ttype. IF w_header2-kursf IS NOT INITIAL. w_item_gl. w_item_gl-base_uom = w_postings-meins. w_item_gl-alloc_nmbr = w_postings-assignment. ENDIF. w_item_gl-tax_code = w_postings-tax_code. w_item_curr-itemno_acc =rlv_counter. w_item_gl-pstng_date = w_header-pstng_date. w_item_gl-value_date = w_postings-value_date. w_item_gl-costcenter = w_postings-ccent. w_item_gl-comp_code = w_postings-company. w_item_gl-comp_code = w_postings-company. IF w_curr-waers NE w_header1-curr. w_item_gl-orderid = w_postings-internal_order. w_item_curr-amt_doccur =w_postings-amount * ( -1 ). w_item_curr-exch_rate = w_header2-kursf. w_item_gl-quantity = w_postings-quantity. g_credit_total = g_credit_total + w_postings-amount. w_item_gl-cs_trans_t = w_postings-ttype. "Document Currency w_item_curr-currency = w_header1-curr. w_item_gl-itemno_acc = rlv_counter. ENDIF.

w_item_gl-item_text = w_postings-description. lw_bkpf TYPE type_bkpf. lw_message TYPE type_message. * To fetch company code currency READ TABLE t_curr INTO w_curr WITH KEY bukrs = w_postings-company BINARY SEARCH. w_item_gl-alloc_nmbr = w_postings-assignment. w_item_curr-curr_type = c_doc_curr. TYPES:BEGIN OF type_bkpf. lt_return TYPE STANDARD TABLE OF bapiret2. w_item_gl-trade_id = w_postings-trading_partner. w_item_curr. lv_gjahr TYPE gjahr. * Clear variables CLEAR: w_postings. w_item_gl-base_uom = w_postings-meins. lv_msg TYPE symsgv." POPULATE_BAPI *&---------------------------------------------------------------------* *&Form POST_DOCUMENT *&---------------------------------------------------------------------* FORM post_document. . w_item_curr-itemno_acc =rlv_counter. END OF type_bkpf. w_item_gl-taxjurcode = w_postings-juridiction_code. APPEND w_item_gl TO t_item_gl. w_item_gl-part_prctr = w_postings-partner_pc.w_item_gl-wbs_element = w_postings-wbs_element. w_item_gl-orderid = w_postings-internal_order. lv_bvorg TYPE bvor-bvorg. w_item_gl-quantity = w_postings-quantity. w_item_gl. w_item_gl-tax_code = w_postings-tax_code. CLEAR w_item_curr. bukrs TYPE bkpf-bukrs. "#EC NEEDED lt_bkpf TYPE STANDARD TABLE OF type_bkpf. lv_rdate TYPE sy-datum. lw_return TYPE bapiret2. w_item_gl-value_date = w_postings-value_date. ENDIF. ENDFORM. w_item_gl-orderid = w_postings-internal_order. IF w_header2-kursf IS NOT INITIAL. g_debit_total = g_debit_total + w_postings-amount. *Local variable declarations DATA: lv_belnr TYPE belnr_d. "Document Currency w_item_curr-currency = w_header1-curr. ENDIF. ENDIF. belnr TYPE bkpf-belnr. gjahr TYPE bkpf-gjahr. IF w_curr-waers NE w_header1-curr. lv_bukrs TYPE bukrs. w_item_curr-amt_doccur =w_postings-amount . w_curr. w_item_curr-exch_rate = w_header2-kursf. APPEND w_item_curr TO t_item_curr.

IF g_error_flag NE c_x. lw_return. w_header-obj_key. IF g_credit_total<> 0 AND g_debit_total<> 0. IF sy-subrc<> 0. lw_bkpf. w_header-obj_sys. If t_return is not initial. lv_msg. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = c_x. ENDIF. CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK' EXPORTING documentheader= w_header TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return. . CLEAR: w_header-obj_type. * Posting journal entries CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST' EXPORTING documentheader = w_header IMPORTING obj_type = g_obj_type obj_key = g_obj_key obj_sys = g_obj_sys TABLES accountgl = t_item_gl currencyamount = t_item_curr return = t_return.*Clear local work area and variables CLEAR: lw_message. ENDIF. Endif.