SAP BAPI – Billing Document Creation using BAPI

How to Use SAP BAPI in SAP ABAP Program. Billing Document Creation using BAPI.

SAP ABAP Course for 2020

SAP ABAP Fundamentals

SAP ABAP Interview Questions – Real Time

This BAPI is used to create Billing document number from a Delivery number using excel file, Date can be created single as well as multiple records at a time. In this program we are using BAPI – BAPI_BILLINGDOC_CREATEMULTIPLE

Step 1: Go to Tcode SE38 to create a program first. Type program name & click on create button as per below screen.

Step 2: Now Create includes programs & subroutines to segregate the code as per below screen.

REPORT yrvr201_billing_doc_creation.

INCLUDE yrvr201_billdoc_creation_top.
INCLUDE yrvr201_billdoc_creation_f01.


  PERFORM get_data.
  PERFORM bapi_process.
  PERFORM bapi_logs.

Step 3: Double click on first include & type below-mentioned code.

*&  Include           YRVR201_BILLDOC_CREATION_TOP

TABLES: likp, lips, vbrk.

TYPES: BEGIN OF ty_likp,
         vbeln TYPE vbeln,
         vkorg TYPE vkorg,
         vbtyp TYPE vbtyp,
         kunag TYPE kunag,
         fkarv TYPE fkarv,
         fkdat TYPE fkdat,
       END OF ty_likp.

TYPES: BEGIN OF ty_lips,
         vbeln TYPE vbeln,
         posnr TYPE posnr,
         matnr TYPE matnr,
         werks TYPE werks,
         pstyv TYPE pstyv,
         vtweg TYPE vtweg,
         spart TYPE spart,
       END OF ty_lips.

TYPES: BEGIN OF ty_upload,
         vbeln TYPE vbeln,
*  vkorg TYPE vkorg,
         fkarv TYPE fkarv,
         fkdat TYPE fkdat,
       END OF ty_upload.

DATA: gt_likp   TYPE STANDARD TABLE OF ty_likp,
      gs_likp   TYPE ty_likp,
      gt_lips   TYPE STANDARD TABLE OF ty_lips,
      gs_lips   TYPE ty_lips,
      gt_upload TYPE TABLE OF ty_upload,
      gs_upload TYPE ty_upload,
      gt_upload_tmp TYPE TABLE OF ty_upload,
      gs_upload_tmp TYPE ty_upload.

DATA: gt_billing TYPE TABLE OF bapivbrk,
      gs_billing TYPE bapivbrk,
      gt_return  TYPE TABLE OF bapiret1,
      gs_return  TYPE bapiret1,
      gt_success TYPE TABLE OF bapivbrksuccess,
      gs_success TYPE bapivbrksuccess.

DATA: gt_file_table TYPE filetable,
      gs_file_table TYPE file_table,
      lv_rc         TYPE i.


PARAMETERS: p_file TYPE rlgrap-filename OBLIGATORY.

*SELECT-OPTIONS: so_vbeln FOR likp-vbeln OBLIGATORY,
*                so_fkarv FOR likp-fkarv OBLIGATORY NO INTERVALS No-EXTENSION,
*                so_fkdat FOR likp-fkdat OBLIGATORY NO INTERVALS No-EXTENSION.


  CALL METHOD cl_gui_frontend_services=>file_open_dialog
      window_title            = ‘Please select File to be uploaded’
      file_table              = gt_file_table
      rc                      = lv_rc
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4.
  IF sy-subrc <> 0.
    MESSAGE ‘Error occured while selecting file’ TYPE ‘E’.
    READ TABLE gt_file_table INTO gs_file_table INDEX 1.
    IF sy-subrc EQ 0.
      p_file = gs_file_table-filename.
*    ELSE.
*      MESSAGE ‘ Could not select file’ TYPE ‘E’.

Step 4: Now Double click on second include program & type the entire code as mentioned below.

*&  Include           YRVR201_BILLDOC_CREATION_F01
*&      Form  GET_DATA

FORM get_data .

  DATA : lv_field_seperator TYPE char01 VALUE ‘X’,
         lv_line_header     TYPE char01 VALUE ‘X’,
         lt_tab_raw_data    TYPE truxs_t_text_data.

        i_field_seperator    = lv_field_seperator
        i_line_header        = lv_line_header
        i_tab_raw_data       = lt_tab_raw_data
        i_filename           = p_file
        i_tab_converted_data = gt_upload
        conversion_failed    = 1.
    IF sy-subrc NE 0.
      MESSAGE ‘Error fetching data from Excel File’ TYPE ‘I’.

  IF gt_upload[] IS NOT INITIAL.

    LOOP AT gt_upload INTO gs_upload.
          input  = gs_upload-vbeln
          output = gs_upload-vbeln.
      MODIFY gt_upload FROM gs_upload TRANSPORTING vbeln.
      CLEAR gs_upload.         .

    SORT gt_upload BY vbeln ASCENDING fkarv DESCENDING.
    SELECT vbeln vkorg vbtyp kunag fkarv fkdat FROM likp INTO TABLE gt_likp FOR ALL ENTRIES IN gt_upload WHERE vbeln = gt_upload-vbeln.
    IF gt_likp[] IS NOT INITIAL.
      SORT gt_likp BY vbeln.
      SELECT vbeln posnr pstyv matnr werks vtweg spart FROM lips INTO TABLE gt_lips FOR ALL ENTRIES IN gt_likp WHERE vbeln = gt_likp-vbeln.

*&      Form  BAPI_PROCESS
*       text
*  –>  p1        text
*  <–  p2        text
FORM bapi_process .

  LOOP AT gt_upload INTO gs_upload.
    READ TABLE gt_lips INTO gs_lips WITH KEY vbeln = gs_upload-vbeln.
    IF sy-subrc = 0.
      gs_billing-ref_item = gs_lips-posnr.
      gs_billing-itm_number = gs_lips-posnr.
*    gs_billing-price_date = sy-datum.
      gs_billing-material = gs_lips-matnr.
      gs_billing-plant = gs_lips-werks.
      gs_billing-distr_chan = gs_lips-vtweg.
      gs_billing-division = gs_lips-spart.
      gs_billing-item_categ = gs_lips-pstyv.

    READ TABLE gt_likp INTO gs_likp WITH KEY vbeln = gs_lips-vbeln.
    IF sy-subrc = 0.
      gs_billing-ref_doc_ca = gs_likp-vbtyp.
      gs_billing-ref_doc = gs_likp-vbeln.
      gs_billing-salesorg = gs_likp-vkorg.
      gs_billing-doc_number = gs_likp-vbeln.
      gs_billing-sold_to = gs_likp-kunag.
*      gs_billing-ordbilltyp = gs_likp-fkarv.
      gs_billing-bill_date = gs_likp-fkdat.

    gs_billing-ordbilltyp = gs_upload-fkarv.

    APPEND gs_billing TO gt_billing.

  REFRESH: gt_return, gt_success.
  SORT gt_billing BY ref_doc ASCENDING ordbilltyp DESCENDING.
      billingdatain = gt_billing
      return        = gt_return
      success       = gt_success.

      wait = ‘X’.


  REFRESH: gt_billing.” gt_return, gt_success.
*&      Form  BAPI_LOGS
*       text
*  –>  p1        text
*  <–  p2        text
FORM bapi_logs .

  TYPES: BEGIN OF ty_output,
           vbeln    TYPE vbeln,
           vbelv    TYPE vbelv,
           fkarv    TYPE fkarv,
           bill_doc TYPE vbeln,
         END OF ty_output.

  TYPES: BEGIN OF ty_vbfa,
           vbelv   TYPE vbelv,
           vbeln   TYPE vbeln,
           vbtyp_v TYPE vbtyp_v,
         END OF ty_vbfa.

  TYPES: BEGIN OF ty_vbrk,
           vbeln TYPE vbeln,
           fkart TYPE fkart,
         END OF ty_vbrk.

  DATA: lt_fcat     TYPE TABLE OF slis_fieldcat_alv,
        ls_fcat     TYPE slis_fieldcat_alv,
        lt_output   TYPE TABLE OF ty_output,
        ls_output   TYPE ty_output,
        lt_vbfa     TYPE TABLE OF ty_vbfa,
        ls_vbfa     TYPE ty_vbfa,
        lt_vbfa_tmp TYPE TABLE OF ty_vbfa,
        ls_vbfa_tmp TYPE ty_vbfa,
        ls_vbfa_chk TYPE vbfa,
        lt_vbrk     TYPE TABLE OF ty_vbrk,
        ls_vbrk     TYPE ty_vbrk.

  IF gt_upload IS NOT INITIAL.
    SELECT vbelv vbeln vbtyp_v FROM vbfa INTO TABLE lt_vbfa FOR ALL ENTRIES IN gt_upload
      WHERE vbeln = gt_upload-vbeln AND vbtyp_v = ‘C’.

    IF lt_vbfa IS NOT INITIAL.
      CLEAR gs_upload.

      gt_upload_tmp = gt_upload.
      SORT gt_upload_tmp BY vbeln.
      IF gt_upload_tmp IS NOT INITIAL.
        SELECT vbelv vbeln INTO TABLE lt_vbfa_tmp FROM vbfa FOR ALL ENTRIES IN gt_upload_tmp
         WHERE vbelv = gt_upload_tmp-vbeln AND vbtyp_n = ‘M’.
        IF sy-subrc = 0.
          SELECT vbeln fkart INTO TABLE lt_vbrk FROM vbrk FOR ALL ENTRIES IN lt_vbfa_tmp WHERE vbeln = lt_vbfa_tmp-vbeln.

      LOOP AT lt_vbrk INTO ls_vbrk.
        ls_output-bill_doc = ls_vbrk-vbeln.
        ls_output-fkarv = ls_vbrk-fkart.
*        READ TABLE gt_success INTO gs_success WITH KEY bill_doc = ls_vbrk-vbeln.
*        IF sy-subrc = 0.
*          ls_output-vbeln = gs_success-ref_doc.
*        ENDIF.
        SELECT SINGLE * FROM vbfa INTO ls_vbfa_chk WHERE vbeln = ls_vbrk-vbeln AND VBTYP_V = ‘J’.
        IF sy-subrc = 0.
          ls_output-vbeln = ls_vbfa_chk-vbelv.
        READ TABLE lt_vbfa INTO ls_vbfa WITH KEY vbeln = ls_vbfa_chk-vbelv.
        IF sy-subrc = 0.
          ls_output-vbelv = ls_vbfa-vbelv.
        APPEND ls_output TO lt_output.
        CLEAR: ls_output, ls_vbfa, gs_upload, gs_success, ls_vbrk.


  CLEAR ls_fcat.
  ls_fcat-fieldname = ‘VBELN’.
  ls_fcat-seltext_m = ‘Delivery’.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.

  CLEAR ls_fcat.
  ls_fcat-fieldname = ‘VBELV’.
  ls_fcat-seltext_m = ‘Sales Order’.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.

  CLEAR ls_fcat.
  ls_fcat-fieldname = ‘FKARV’.
  ls_fcat-seltext_m = ‘Billing Type’.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.

  CLEAR ls_fcat.
  ls_fcat-fieldname = ‘BILL_DOC’.
  ls_fcat-seltext_m = ‘Billing No.’.
  APPEND ls_fcat TO lt_fcat.
  CLEAR ls_fcat.

      i_callback_program = sy-repid
*     i_structure_name   = ls_output
      it_fieldcat        = lt_fcat
      i_save             = ‘X’
      t_outtab           = lt_output
*     PROGRAM_ERROR      = 1
*     OTHERS             = 2
  IF sy-subrc <> 0.
* Implement suitable error handling here


Step 5: Press F8 to execute the program and upload excel file which must have delivery numbers & check the output.

Step 6: Now Select excel file from system.

Step 7: Again press F8 to finally execute the program to run BAPI & it will automatically create billing document numbers from excel file.