Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Author(s) Name: Nitesh Pandey and Rahul Sharma

 Introduction:
BDC stands for Batch Data Communication. It is a technique for mass input of data by simulating user inputs in screens of existing transactions. In simple words, it is used to transfer data in bulk from non sap systems to sap systems.
Standard SAP consistency checks and authorizations are automatically performed.
There are two main techniques widely used for this purpose. These are
Ø  Batch Input Session (BI session)
Ø  Call Transaction Using (CTU)
Both must be programmed in ABAP Programs. First recording of transaction needs to be done for both the methods by using SHDB transaction. Then this recording can be triggered by using any of these methods.
Steps involved in recording: Following steps are performed to record any transaction.
1.) Open transaction shdb.
2.) Click on New recording button.

3.) Enter recording name(ZNK_BDC1) and transaction name(SM30).

Note: Field description on the above screen shot:
i.)   Recording: Enter name of the recording.(e.g. ZNK_BDC1).
ii.)  Transaction code: Enter the transaction code for which recording needs to be   done.(e.g. SM30).
iii.) Update mode: Select any one from the dropdown list. It controls how the actual
 SAP database updates are performed.

Asynchronous mode: It is the default mode and the fastest mode for updating database. It does not wait till the update in database is complete either it is successful or un successful. Called transaction receives no completion message from the update module in asynchronous updating. The calling data transfer program, in turn, cannot determine whether a called transaction ended with a successful update of the database or not. That is why this mode is NOT recommended for processing any larger amount of data.
Synchronous mode: Execution is slower than asynchronous mode because called transactions wait for updating to be completed. However, the called transaction returns any update error message that occurs to your program. It is much easier for you to analyze and recover from errors.
Local mode: This mode is the one which is confined to a particular application server, where the BDC program is running.
4.) Click on Start recording button. Now the transaction first screen appears. Enter table/view name (ZNK_TABLE1).

5.) Click on Maintain button.

6.) Click on New Entries button and enter Plant = CH00 and Storage Location = 2000.

7.) Click on save button. Now keep on clicking on back button until the following screen appears.

8.) Till now the recording is complete. This recording can be used to generate the report program. For this, click on BACK button and select your recording name from the list of recordings and then click on Program tab.

9.) The following screen appears; fill the program name and description. Select the radio button Transfer from Recording and press enter.

10.)              A report program is generated is se38. Now use the Call Transaction Method to read the data from any file and upload into the table.
11.)              Logic to develop the program: If the data is coming from any .txt file, get the data into an internal table it_final_data. Now loop at the internal table and use the code got from above recording. Find below the code snippet for your reference.

*&---------------------------------------------------------------------*
*& Report  ZNK_TEST_BDC1
*&
*&---------------------------------------------------------------------*
REPORT  znk_test_bdc1.
*-Structure Declaration to hold the final data
 
TYPES: BEGIN OF ty_data,
        werks TYPE werks_d,
        lgort TYPE lgort_d,
       END OF ty_data.
*-Structure Declaration to hold the error message
TYPES: BEGIN OF ty_it_error,
        message TYPE string,
       END OF ty_it_error.

 
*-Internal table and work area declaration
DATA: it_final_data TYPE STANDARD TABLE OF ty_data,
      it_bdc_data   TYPE STANDARD TABLE OF bdcdata,
      it_message    TYPE STANDARD TABLE OF bdcmsgcoll,
      it_error TYPE STANDARD TABLE OF ty_it_error,
      wa_bdc_data   TYPE bdcdata,
      wa_ctu_params TYPE ctu_params,
      wa_message    TYPE bdcmsgcoll,
      wa_error TYPE ty_it_error,
      wa_data       TYPE ty_data,
      v_error       TYPE string,
      v_filename    TYPE string.
*----------------------------------------------------------------------*
*                       SELECTION SCREEN
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS: p_filenm TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.*-F4 help to get the file from
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_filenm.
* To get the file path and file name for uploading data
  PERFORM get_input_file CHANGING p_filenm.
START-OF-SELECTION.
* Perform to fill data into internal tables from file
  PERFORM fill_data.
* Perform to fill bdcdata type table.
  PERFORM fill_bdcdata_table.
&---------------------------------------------------------------------
*&      Form  GET_INPUT_FILE
&---------------------------------------------------------------------
*       text
----------------------------------------------------------------------
FORM get_input_file  CHANGING fp_v_filenm TYPE rlgrap-filename.
  DATA:  l_it_file     TYPE filetable, " File Name
         l_v_rc        TYPE i,         " it_error code
         l_v_title     TYPE string.    " Window title
  CLEAR l_v_title.
  l_v_title = text-t01.
* Calling method to select file to upload data
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = l_v_title
      default_extension       = '.txt'
      default_filename        = '*.txt'
      file_filter             = '.txt'
    CHANGING
      file_table              = l_it_file
      rc                      = l_v_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.
  IF sy-subrc NE 0.
    MESSAGE 'Cannot Provide F4 Help' TYPE 'I'. " Cannot Provide F4 Help
  ELSE.
*   Get the file name
    READ TABLE l_it_file INDEX 1 INTO fp_v_filenm.
  ENDIF.
ENDFORM.                    " get_input_file
&---------------------------------------------------------------------
*&      Form  FILL_DATA
&---------------------------------------------------------------------
*       text
----------------------------------------------------------------------
*  -->  p1        text
*  <--  p2        text
----------------------------------------------------------------------
FORM fill_data.
* Assigning file path to string type variable
  v_filename  = p_filenm.
* Filling file data into an internal table
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = v_filename
      filetype                = 'ASC'
      has_field_separator     = 'X'
    TABLES
      data_tab                = it_final_data
    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.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    " FILL_DATA
&---------------------------------------------------------------------
*&      Form  FILL_bdc_dataDATA_TABLE
&---------------------------------------------------------------------
*       text
----------------------------------------------------------------------
*  -->  p1        text
*  <--  p2        text
----------------------------------------------------------------------
FORM fill_bdcdata_table.
  CLEAR wa_ctu_params.
  wa_ctu_params-dismode = 'N'.
  wa_ctu_params-updmode = 'S'.
  wa_ctu_params-cattmode = ' '.
  wa_ctu_params-defsize = ' '.
  wa_ctu_params-racommit = ' '.
  wa_ctu_params-nobinpt = 'X'.
  wa_ctu_params-nobiend = ' '.
  LOOP AT it_final_data INTO wa_data.
    PERFORM bdc_dynpro      USING 'SAPMSVMA' '0100'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'VIEWNAME'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=UPD'.
    PERFORM bdc_field       USING 'VIEWNAME'
                                  'ZNK_TEST_TABLE1'.
    PERFORM bdc_field       USING 'VIMDYNFLDS-LTD_DTA_NO'
                                  'X'.
    PERFORM bdc_dynpro      USING 'SAPLZNK_TEST_TABLE1' '0001'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'VIM_POSITION_INFO'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=NEWL'.
    PERFORM bdc_dynpro      USING 'SAPLZNK_TEST_TABLE1' '0001'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'ZNK_TEST_TABLE1-LGORT(01)'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=SAVE'.
    PERFORM bdc_field       USING 'ZNK_TEST_TABLE1-WERKS(01)'
                                  wa_data-werks.
    PERFORM bdc_field       USING 'ZNK_TEST_TABLE1-LGORT(01)'
                                  wa_data-lgort.
    PERFORM bdc_dynpro      USING 'SAPLZNK_TEST_TABLE1' '0001'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'ZNK_TEST_TABLE1-WERKS(02)'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BACK'.
    PERFORM bdc_dynpro      USING 'SAPLZNK_TEST_TABLE1' '0001'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'ZNK_TEST_TABLE1-WERKS(01)'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '=BACK'.
    PERFORM bdc_dynpro      USING 'SAPMSVMA' '0100'.
    PERFORM bdc_field       USING 'BDC_OKCODE'
                                  '/EBACK'.
    PERFORM bdc_field       USING 'BDC_CURSOR'
                                  'VIEWNAME'.
    REFRESH it_message.
    CALL TRANSACTION 'SM30' USING it_bdc_data OPTIONS FROM wa_ctu_params
                            MESSAGES INTO it_message.
    BREAK-POINT.
    LOOP AT it_message INTO wa_message.
      CALL FUNCTION 'FORMAT_MESSAGE'
        EXPORTING
          id        = wa_message-msgid
          lang      = sy-langu
          no        = wa_message-msgnr
          v1        = wa_message-msgv1
          v2        = wa_message-msgv2
          v3        = wa_message-msgv3
          v4        = wa_message-msgv4
        IMPORTING
          msg       = v_error
        EXCEPTIONS
          not_found = 1
          OTHERS    = 2.
      IF sy-subrc = 0.
        MOVE v_error TO wa_error-message.
        APPEND wa_error TO it_error.
        CLEAR wa_error.
      ENDIF.
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    ENDLOOP.
    REFRESH it_bdc_data.
  ENDLOOP.
ENDFORM.                    " FILL_bdc_dataDATA_TABLE
&---------------------------------------------------------------------
*&      Form  BDC_DYNPRO
&---------------------------------------------------------------------
*       text
----------------------------------------------------------------------
*      -->P_0262   text
*      -->P_0263   text
----------------------------------------------------------------------
FORM bdc_dynpro USING fp_program TYPE any
                      fp_dynpro  TYPE any.
* filling prg name , screen no in bdcdata type table
  CLEAR wa_bdc_data.
  wa_bdc_data-program  = fp_program.
  wa_bdc_data-dynpro   = fp_dynpro.
  wa_bdc_data-dynbegin = 'X'.
  APPEND wa_bdc_data TO it_bdc_data.
ENDFORM.                    " BDC_DYNPRO
&---------------------------------------------------------------------
*&      Form  BDC_FIELD
&---------------------------------------------------------------------
*       text
----------------------------------------------------------------------
*      -->P_0267   text
*      -->P_0268   text
----------------------------------------------------------------------
FORM bdc_field USING fp_fnam TYPE any
                     fp_fval TYPE any.
* Filling field name and field value in bdcdata type table
  CLEAR wa_bdc_data.
  wa_bdc_data-fnam = fp_fnam.
  wa_bdc_data-fval = fp_fval.
  APPEND wa_bdc_data TO it_bdc_data.
  CLEAR wa_bdc_data.
ENDFORM.                    "BDC_FIELD

Error Handling: While using Call Transaction store all the messages into some internal table and interpret them by using FM 'FORMAT_MESSAGE'.
  
Have content to add? Just click here, be sure to add labels to help others find the relevant information quickly!

  • No labels