使用BDC的CALL TRANSACTION编程方式导入数据

BDC的两种通用写法。
1.CallTransaction:顾名思义,就是直接调用BDC进行数据批量导入。优点:方便快捷,程序处理方便。缺点:日志管理能力差,需自己建透明表来维护数据。我只是把它用作测试用途,不做正式使用。
2.BDCInsert:这是一种不直接运行,而是将BDC程序生成session,间接运行的一种方法。优点:通过T-codeSM35可以进行运行管理及日志管理,方便查错。缺点:相对方法1来说实现起来比较繁琐。

call transaction方法将数据归集到BDCDATA 结构的内表中,然后导入到系统中,通过BDCMSGCOLL结构返回BDC导入操作的成功与否消息。
BDCDATA结构中比较关键的几个字段解释:
PROGRAM  -   BDC module pool
DYNPRO-        BDC Screen number
DYNBEGIN-    BDC screen start                                                          
FNAM-            Field name
FVAL-              BDC field value

1、需要导入的execl模板数据格式
 
实际导入时,把前2行删掉

2、SHDB录制屏幕操作
我们是录制ME51的操作,然后生成标准步骤,用程序批量处理,所以录制时不可含有多余步骤
我们可以先在ME51中操作几遍,熟悉之后再操作SHDB录制屏幕,尽量不用回车,可以用点击替代。
 
SHDB->输入录制屏幕产生记录的名称 和 需要录制的T-Code
 
结束之后就可以看到这些代码,单击‘导出’按钮,可将数据导出到本地
BDC命令行的解读:
T             ME51:表示调用TCODEME51
BDC_OKCODE   =AB:表示点了按钮
BDC_OKCODE   /00:表示回车
BDC_OKCODE   XXXX:一般表示为点击XXXX按扭
SAPMM06B0106 X                  :表示调用程序的0106画面
BDC_CURSOR   EBAN-LGORT    :表示需要输入值的字段
EBAN-MATNR 100702002 :表示给字段MATNR赋值P100702002
(通常表、字段的表示格式为:XXXXXX-ZZZZZ,XXXXXX为表名,ZZZZ为字段名)

3、点击program按钮,将记录生成代码
 
查看生成的代码,然后我们写程序就可以参考生成的代码了
 


4、se38创建程序

*Structure for error message
TYPES : BEGIN OF ty_s_error,
           msg_err(60) TYPE c,
         END OF ty_s_error.

*Input Path
SELECTION-SCREEN BEGIN OF BLOCK blck WITH FRAME TITLE text-011.
PARAMETERS:
   p_file   TYPE rlgrap-filename,                  " File Path
   e_file   TYPE rlgrap-filename OBLIGATORY,       " Error File Path
   p_mode   TYPE c OBLIGATORY DEFAULT 'N'.         " Mode
SELECTION-SCREEN END OF BLOCK blck.

* Structure Decleration
DATA :
   BEGIN OF fs_field,
     bsart TYPE eban-bsart,             " Document Type.
     matnr TYPE eban-matnr,             " Material Number.
     menge TYPE eban-menge,             " Quantity Requested.
     werks TYPE eban-werks,             " Plant.
  END OF fs_field.

*Internal table decleration
DATA:
    t_field    LIKE TABLE OF fs_field,
    t_bdcdata  LIKE TABLE OF bdcdata.

DATA:
   fs_bdcdata LIKE LINE OF t_bdcdata,   " Structure type of bdcdata
    w_str  TYPE string.

* Data decleration
DATA:
   wa_path TYPE string ,
   wa_error TYPE string,
   wa_cnt   TYPE i,
   w_mode    TYPE c,
   wa_cnt1(2) TYPE n,
   it_output TYPE TABLE OF ty_s_error,
   wa_output LIKE LINE OF it_output.

AT SELECTION-SCREEN.
* Mode 'A' = Foreground mode
* Mode 'N' = Background mode
  IF p_mode = 'A' OR p_mode = 'N' .
    w_mode = p_mode.
  ELSE.
*Error Message
    MESSAGE 'PLEASE ENTER THE MODE A or N' TYPE 'E'.
  ENDIF.

* Opening window for path selection
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = ' '
    IMPORTING
      file_name     = p_file.

  TYPES:
    fs_struct(4096) TYPE c OCCURS 0 .

  DATA:
    w_struct TYPE fs_struct.

* Uploading excel file.
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_field_seperator          = 'X'
*   I_LINE_HEADER              =
      i_tab_raw_data             = w_struct
      i_filename                 = p_file
    TABLES
      i_tab_converted_data       = t_field
    EXCEPTIONS
      conversion_failed          = 1
      OTHERS                     = 2
            .
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

*Opening window for Error file download
AT SELECTION-SCREEN ON VALUE-REQUEST FOR e_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = ' '
    IMPORTING
      file_name     = e_file.

* start of selection event.
START-OF-SELECTION.

  LOOP AT t_field INTO fs_field .
    REFRESH: t_bdcdata.
    CLEAR fs_bdcdata.
    PERFORM populate_bdcdata.
    PERFORM insert_data.
  ENDLOOP.                               " LOOP AT it_c.

*********************(populate_bdcdata)***********************
* part 1
FORM populate_bdcdata.
  PERFORM :
     fill_bdc_data USING 'SAPMM06B' '0100' 'X'  ' '  ' ',
     fill_bdc_data USING  ''  ''  ''   'EBAN-BSART' fs_field-bsart,   " Document Type.
     fill_bdc_data USING  ''  ''  ''   'BDC_OKCODE' '/00',            " Enter.

     fill_bdc_data USING 'SAPMM06B'  '0106' 'X' ' ' ' ',
     fill_bdc_data USING  '' '' '' 'EBAN-MATNR(01)'  fs_field-matnr,   " Material Number.
     fill_bdc_data USING  '' '' ''  'EBAN-MENGE(01)' fs_field-menge,   " Quantity Requested.
     fill_bdc_data USING  '' '' ''  'EBAN-WERKS(01)' fs_field-werks,   " Plant.
     fill_bdc_data USING  '' '' ''  'BDC_OKCODE'   '/00',              " Enter.

     fill_bdc_data USING 'SAPMM06B' '0102' 'X' ''  '' ,
     fill_bdc_data USING  '' '' ''  'BDC_OKCODE' '=BU'.                 " Save.
ENDFORM.                               " Form populate_bdc.

* part 2
FORM fill_bdc_data USING value(p_program)
                       value(p_dynpro)
                       value(p_dynbegin)
                       value(p_fnam)
                       value(p_fval).
  CLEAR fs_bdcdata .
  IF p_dynbegin = 'X' .
    fs_bdcdata-program = p_program .
    fs_bdcdata-dynpro  = p_dynpro .
    fs_bdcdata-dynbegin = p_dynbegin .
    APPEND fs_bdcdata TO t_bdcdata.
  ELSE.
    fs_bdcdata-fnam = p_fnam.
    fs_bdcdata-fval = p_fval.
    CONDENSE fs_bdcdata-fval.
    APPEND fs_bdcdata TO t_bdcdata.
  ENDIF.                               " IF p_dynbeg..
ENDFORM .                              " Fill_entry

*********************(insert_data)****************************
FORM insert_data.
*Data decleration for Error Message
  DATA:
       t_msg TYPE TABLE OF bdcmsgcoll,   " Collecting Error messages
       w_msg TYPE bdcmsgcoll,
       w_msg1(51).

* Call transaction 'ME51'
  CALL TRANSACTION 'ME51' USING t_bdcdata
    MODE w_mode
    UPDATE 'S'
    MESSAGES INTO t_msg.

  IF sy-subrc EQ 0.
*    Uploaded into the database
    WRITE :/ 'DATA UPLOADED IN TABLE EBAN...' .
  ELSE.
*    Error Found
    LOOP AT t_msg INTO w_msg WHERE msgtyp EQ 'E'.
*     Format Message
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = w_msg-msgid
          msgnr               = w_msg-msgnr
          msgv1               = w_msg-msgv1
          msgv2               = w_msg-msgv2
          msgv3               = w_msg-msgv3
          msgv4               = w_msg-msgv4
        IMPORTING
          message_text_output = w_msg1.
      wa_output-msg_err = w_msg1.
*Error message in downloaded file
      DATA:wa_string(10) TYPE c.

      wa_string = fs_field-matnr.
      CONCATENATE wa_string wa_output-msg_err INTO wa_output-msg_err SEPARATED BY space.
      APPEND wa_output-msg_err TO it_output.

      wa_error = e_file.
      CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
*         BIN_FILESIZE                    =
          filename                        = wa_error
*         FILETYPE                        = 'ASC'
*         APPEND                          = ' '
          write_field_separator           = 'X'
        TABLES
          data_tab                        = it_output
*
                .
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.                           " IF sy-subrc <> 0.
    ENDLOOP.
  ENDIF.
ENDFORM.                    "insert_data

  5、执行效果


 
如果产生错误信息,则通过txt文件,导出到本地

posted @ 2019-04-26 14:57  Park_SAP  阅读(1153)  评论(0编辑  收藏  举报