Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

changes in BDC program to upload data from text/excel file

Former Member
0 Likes
1,768

Hi friends i have obtained the BDc program after recording :

start-of-selection.



perform open_group.

perform bdc_dynpro      using 'SAPLCOIH' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              'CAUFVD-GSBER'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'AUFPAR-PM_AUFART'
                              'PM01'.
perform bdc_field       using 'CAUFVD-IWERK'
                              '460a'.
perform bdc_field       using 'CAUFVD-GSBER'
                              '2460'.
perform bdc_field       using 'CAUFVD-EQUNR'
                              '10000009'.
perform bdc_dynpro      using 'SAPLCOIH' '3000'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'CAUFVD-KTEXT'
                              'abc'.
perform bdc_field       using 'CAUFVD-INGPR'
                              'GM'.
perform bdc_field       using 'CAUFVD-VAPLZ'
                              'RTN_VELR'.
perform bdc_field       using 'CAUFVD-VAWRK'
                              '460A'.
perform bdc_field       using 'BDC_CURSOR'
                              'CAUFVD-GLTRP'.
perform bdc_field       using 'CAUFVD-GSTRP'
                              '1.8.2010'.
perform bdc_field       using 'CAUFVD-GLTRP'
                              '1.8.2010'.
perform bdc_field       using 'CAUFVD-TPLNR'
                              'RTNP-HIG'.
perform bdc_field       using 'CAUFVD-EQUNR'
                              '10000009'.
perform bdc_field       using 'AFVGD-INDET'
                              '1'.
perform bdc_field       using 'AFVGD-WERKS'
                              '460A'.
perform bdc_field       using 'AFVGD-STEUS'
                              'PMIN'.
perform bdc_field       using 'AFVGD-ARBEH'
                              'H'.
perform bdc_field       using 'AFVGD-DAUNE'
                              'H'.
perform bdc_dynpro      using 'SAPLCOIH' '3000'.
perform bdc_field       using 'BDC_OKCODE'
                              '=BU'.
perform bdc_field       using 'BDC_CURSOR'
                              'CAUFVD-KTEXT'.
perform bdc_field       using 'CAUFVD-KTEXT'
                              'abc'.
perform bdc_field       using 'CAUFVD-INGPR'
                              'GM'.
perform bdc_field       using 'CAUFVD-VAPLZ'
                              'RTN_VELR'.
perform bdc_field       using 'CAUFVD-VAWRK'
                              '460A'.
perform bdc_field       using 'CAUFVD-GSTRP'
                              '01.08.2010'.
perform bdc_field       using 'CAUFVD-GLTRP'
                              '01.08.2010'.
perform bdc_field       using 'CAUFVD-TPLNR'
                              'RTNP-HIG'.
perform bdc_field       using 'CAUFVD-EQUNR'
                              '10000009'.
perform bdc_field       using 'AFVGD-LTXA1'
                              'abc'.
perform bdc_field       using 'AFVGD-INDET'
                              '1'.
perform bdc_field       using 'AFVGD-ARBPL'
                              'RTN_VELR'.
perform bdc_field       using 'AFVGD-WERKS'
                              '460A'.
perform bdc_field       using 'AFVGD-STEUS'
                              'PMIN'.
perform bdc_field       using 'AFVGD-ARBEH'
                              'H'.
perform bdc_field       using 'AFVGD-DAUNE'
                              'H'.
perform bdc_transaction using 'IW31'.

perform close_group.

after executing it has two options.one is for call transactioin and other is for with session.Now i want to do the uploading from text file.Please guide me how to achieve that and what should be the order of fields in the text file.I am just a beginner.I know how to do that with LSMW bt trying BDc first time for creating PM orders

1 ACCEPTED SOLUTION
Read only

kerem_kayacan
Active Participant
0 Likes
1,469

You can use class "cl_gui_frontend_services". First you must select file with "file_open_dialog" method and the you can upload file with "gui_upload" method into internal table. Here is a sample: (I didn't try the code but I'm sure you can get it working, there are lots of sample codes about that. You can also search FM "GUI_UPLOAD").


REPORT  zreport.

DATA:
gv_subrc TYPE sysubrc,
gv_result TYPE abap_bool,
gt_data_tab type table of zstruc.

PARAMETERS: p_fname LIKE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
    PERFORM file_open_dialog USING space space space space
                        CHANGING p_fname gv_action gv_subrc.

START-OF-SELECTION.

 PERFORM gui_upload TABLES gt_data_tab
                 USING p_fname  CHANGING gv_subrc.

*** FORMS ***
FORM file_open_dialog USING pv_window_title
                            pv_default_extension
                            pv_default_filename
                            pv_initial_directory
                   CHANGING pv_filename
                            pv_user_action
                            pv_subrc.

  DATA:
  lt_file_table TYPE filetable,
  lv_rc         TYPE i,
  lv_initial_directory TYPE string.

  IF pv_initial_directory IS INITIAL.
    PERFORM get_desktop_directory CHANGING lv_initial_directory.
  ELSE.
    lv_initial_directory = pv_initial_directory.
  ENDIF.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = pv_window_title
      default_extension       = pv_default_extension
      default_filename        = pv_default_filename
*     file_filter             =
*     with_encoding           =
      initial_directory       = lv_initial_directory
*     multiselection          =
    CHANGING
      file_table              = lt_file_table
      rc                      = lv_rc
      user_action             = pv_user_action
*     file_encoding           =
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5
          .

  pv_subrc = sy-subrc.

  IF pv_subrc EQ 0.
    READ TABLE lt_file_table INTO pv_filename INDEX 1.
  ENDIF.

ENDFORM.

FORM gui_upload TABLES pt_data_tab
                 USING pv_filename
              CHANGING pv_subrc    TYPE sysubrc.

  DATA:
  lt_data_tab TYPE TABLE OF string,
  lv_filename TYPE string.

  lv_filename = pv_filename.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = lv_filename
      filetype                = 'ASC'
     has_field_separator     = 'X'
*     header_length           = 0
*     read_by_line            = 'X'
*     dat_mode                = SPACE
*     codepage                = SPACE
*     ignore_cerr             = ABAP_TRUE
*     replacement             = '#'
*     virus_scan_profile      =
*     show_transfer_status    = 'X'
*   IMPORTING
*     filelength              =
*     header                  =
    CHANGING
      data_tab                = lt_data_tab
    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
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19
          .

  pv_subrc = sy-subrc.

  IF pv_subrc EQ 0.
    pt_data_tab[] = lt_data_tab[].
  ENDIF.

ENDFORM.

14 REPLIES 14
Read only

kerem_kayacan
Active Participant
0 Likes
1,470

You can use class "cl_gui_frontend_services". First you must select file with "file_open_dialog" method and the you can upload file with "gui_upload" method into internal table. Here is a sample: (I didn't try the code but I'm sure you can get it working, there are lots of sample codes about that. You can also search FM "GUI_UPLOAD").


REPORT  zreport.

DATA:
gv_subrc TYPE sysubrc,
gv_result TYPE abap_bool,
gt_data_tab type table of zstruc.

PARAMETERS: p_fname LIKE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.
    PERFORM file_open_dialog USING space space space space
                        CHANGING p_fname gv_action gv_subrc.

START-OF-SELECTION.

 PERFORM gui_upload TABLES gt_data_tab
                 USING p_fname  CHANGING gv_subrc.

*** FORMS ***
FORM file_open_dialog USING pv_window_title
                            pv_default_extension
                            pv_default_filename
                            pv_initial_directory
                   CHANGING pv_filename
                            pv_user_action
                            pv_subrc.

  DATA:
  lt_file_table TYPE filetable,
  lv_rc         TYPE i,
  lv_initial_directory TYPE string.

  IF pv_initial_directory IS INITIAL.
    PERFORM get_desktop_directory CHANGING lv_initial_directory.
  ELSE.
    lv_initial_directory = pv_initial_directory.
  ENDIF.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = pv_window_title
      default_extension       = pv_default_extension
      default_filename        = pv_default_filename
*     file_filter             =
*     with_encoding           =
      initial_directory       = lv_initial_directory
*     multiselection          =
    CHANGING
      file_table              = lt_file_table
      rc                      = lv_rc
      user_action             = pv_user_action
*     file_encoding           =
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5
          .

  pv_subrc = sy-subrc.

  IF pv_subrc EQ 0.
    READ TABLE lt_file_table INTO pv_filename INDEX 1.
  ENDIF.

ENDFORM.

FORM gui_upload TABLES pt_data_tab
                 USING pv_filename
              CHANGING pv_subrc    TYPE sysubrc.

  DATA:
  lt_data_tab TYPE TABLE OF string,
  lv_filename TYPE string.

  lv_filename = pv_filename.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = lv_filename
      filetype                = 'ASC'
     has_field_separator     = 'X'
*     header_length           = 0
*     read_by_line            = 'X'
*     dat_mode                = SPACE
*     codepage                = SPACE
*     ignore_cerr             = ABAP_TRUE
*     replacement             = '#'
*     virus_scan_profile      =
*     show_transfer_status    = 'X'
*   IMPORTING
*     filelength              =
*     header                  =
    CHANGING
      data_tab                = lt_data_tab
    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
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19
          .

  pv_subrc = sy-subrc.

  IF pv_subrc EQ 0.
    pt_data_tab[] = lt_data_tab[].
  ENDIF.

ENDFORM.

Read only

0 Likes
1,469

@kerem : thnx dear for help.Actually what i want to know is that in what order the fields should be in the text file.Aslo one thing more,if I use the code posted by you then is there any need of the code generated through recording.

Read only

0 Likes
1,469

You can decide the order of fields in text file but internal table must have the same order.

After you get data into internal table, you must build your bdc table data. This code may be copied from the code generated by recording.

Read only

0 Likes
1,469

@kerem : i have changed the code to this but still not able to obtain the require output i m using call transactioni on selection screen and then also choose excel file from my pc but it does not work.plz helpI have used debugging and found that it is not entering the "loop at itab" statement :

PARAMETERS: P_FNAME LIKE RLGRAP-FILENAME OBLIGATORY.

DATA: it_raw TYPE truxs_t_text_data.

DATA: BEGIN OF IT_FILE_UPLOAD OCCURS 0.
INCLUDE STRUCTURE ALSMEX_TABLINE. " Rows for Table with Excel Data
DATA: END OF IT_FILE_UPLOAD.


DATA: GV_ANS TYPE C.

data : begin of itab occurs 10,
         GSBER LIKE CAUFVD-GSBER ,
         PM_AUFART LIKE AUFPAR-PM_AUFART,
         IWERK LIKE CAUFVD-IWERK,
         EQUNR LIKE CAUFVD-EQUNR,
         KTEXT LIKE CAUFVD-KTEXT,
         INGPR LIKE CAUFVD-INGPR,
         VAPLZ LIKE CAUFVD-VAPLZ,
         VAWRK LIKE CAUFVD-VAWRK,
         GSTRP LIKE CAUFVD-GSTRP,
         GLTRP LIKE CAUFVD-GLTRP,
         TPLNR LIKE CAUFVD-TPLNR,
         INDET LIKE AFVGD-INDET,
         WERKS LIKE AFVGD-WERKS,
         STEUS LIKE AFVGD-STEUS,
         ARBEH LIKE AFVGD-ARBEH,
         DAUNE LIKE AFVGD-DAUNE,
       END OF ITAB.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

 CALL FUNCTION 'F4_FILENAME'

 EXPORTING

 field_name = 'p_fname'

 IMPORTING

 file_name  = p_fname.


start-of-selection.


CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

 EXPORTING

 i_line_header        = 'X'

 i_tab_raw_data       = it_raw

 i_filename           = p_fname

 TABLES

 i_tab_converted_data = itab[]

 EXCEPTIONS

 conversion_failed    = 1

 OTHERS               = 2.

 
perform open_group.

loop at itab.
break-point.

perform bdc_dynpro      using 'SAPLCOIH' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              'itab-GSBER'.

perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'AUFPAR-PM_AUFART'
                              'itab-PM_AUFART'.
perform bdc_field       using 'CAUFVD-IWERK'
                              'itab-IWERK'.

REST IS SAME AS IN EARLIAR POST.

perform bdc_transaction using 'IW31'.
endloop.

perform close_group.

Edited by: achalmehra on Aug 6, 2010 7:47 AM

Read only

0 Likes
1,469

Hi,

Your code is not at all readable. Can you post small peice of your code?

So that may be i can help you out.

Thanks,

Archana

Read only

0 Likes
1,469

@ arachna : it is not even entring the code after break-point.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

 CALL FUNCTION 'F4_FILENAME'

 EXPORTING

 field_name = 'p_fname'

 IMPORTING

 file_name  = p_fname.

start-of-selection.

Edited by: achalmehra on Aug 10, 2010 10:30 AM

Read only

0 Likes
1,469

after above code rest is :

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

 EXPORTING

 i_line_header        = 'X'

 i_tab_raw_data       = it_raw

 i_filename           = p_fname

 TABLES

 i_tab_converted_data = itab[]

 EXCEPTIONS

 conversion_failed    = 1

 OTHERS               = 2.


Edited by: achalmehra on Aug 10, 2010 10:31 AM

Read only

0 Likes
1,469

then :

perform open_group.

loop at itab.
break-point.

perform bdc_dynpro      using 'SAPLCOIH' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              'itab-GSBER'.

perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'AUFPAR-PM_AUFART'
                              'itab-PM_AUFART'.


REST IS SAME AS BOVE FOR OTHER FIELDS.

perform bdc_transaction using 'IW31'.
endloop.

perform close_group.

Edited by: achalmehra on Aug 10, 2010 10:32 AM

Read only

0 Likes
1,469

Hi,

Did you check whether your internal table 'itab' contains any data?

Thanks,

Archana

Read only

0 Likes
1,469

@arachna : it is not entering the loop after selecting some random excel files on selection screen.but after choosing some other excel files not relevant to data for this prog it works but obviously the data converted is not acceptable by fields on the screen.

perform open_group.

loop at itab.
break-point.

perform bdc_dynpro      using 'SAPLCOIH' '0100'.
perform bdc_field       using 'BDC_CURSOR'
                              'itab-GSBER'.

perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'AUFPAR-PM_AUFART'
                              'itab-PM_AUFART'.
perform bdc_field       using 'CAUFVD-IWERK'
                              'itab-IWERK'.
perform bdc_field       using 'CAUFVD-GSBER'
                              'itab-GSBER'.

Read only

0 Likes
1,469

basically right now i am facing how to decide :

1)how many fields should be there in excel file.

2)in which order they should be placed

3)only values should be placed or there should be heading to columns also in file.

Read only

0 Likes
1,469

Hi,

First of all, let me tell you use of BDC.

Using BDC you can automatically upload data dat in any transaction, for that you need data in the form of file.

This file is provided by the client. Then this file you need to convert into internal table. Then you can use this internal table to upload your data. Bow, to upload your data, you should call your transaction in a loop at your internal table. So, that one by one, your data will be uploaded into the database.

So,

If you are creating a recording for any particular transaction, you should have all the fileds to be filled in that transaction input fields in your excel file. This excel file might contain headers as well.

You will have to delete these headers while converting file into Internal table.

I hope this helps.

Thanks,

Archana

Read only

0 Likes
1,469

kkk but the thing is that when i am doing following steps at selection screen :

1) selecting option "call transaction"

2) display all screens

and then choose the excel file from system.For some non-relevent excel files it calls the screen and then displays error as data converted is not proper but when I choose the excel file prepared to be uploaded it does not call any screen.So i think there is some problem with excel sheet.

I have followinf fields in it :

2460	1	460A	10000009	ACHAL TEST1	GM	RTN_VELR	460A	1.8.2010	25.8.2010	RTNP-HIG	1	460A	h	l	m
2460	1	460A	10000009	ACHAL TEST2	GM	RTN_VELR	460A	1.8.2010	25.8.2010	RTNP-HIG	1	460A	h	l	m

Read only

0 Likes
1,469

Hi,

You need to have your excel file in comma separated format or tab delimited form. Comma separated file will be a better option.

You then need to convert it in internal table using split. Please refer below code. So, your internal table will have data of your excel sheet. Then, you can use this internal table for call transaction.

*- CALL FUNCTION MODULE GUI_UPLOAD
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      FILENAME = V_FILE
      FILETYPE = 'ASC'
    TABLES
      DATA_TAB = I_STRING.

  IF SY-SUBRC EQ 0.
*- DELETING THE FIRST 2 LINES WHICH IS HAPPENED TO BE HEADER
    DELETE I_STRING FROM C_NUM1 TO C_NUM2.
    V_COUNT = C_ZERO.

*- Looping at internal table which contains records in excel sheet
    LOOP AT I_STRING INTO WA_STRING.

*- Inserting data in item level table
        SPLIT WA_STRING AT ','
        INTO
         WA_HEADER-CONDN_TYPE       "Condition Type (KSCHAL)
         WA_HEADER-CONDN_TABLE      "Condition Table (COND_TABLE)
         WA_HEADER-SERV_AGENT       "Service Agent (TDLNR)
         WA_HEADER-MAT_TYPE         "Packing Material Type (VHART)
         WA_HEADER-DEPPOSTCDE       "DepPostCde (PSTLZA)
         WA_HEADER-DEST             " (TRFZNZ)
         WA_HEADER-TRANSFER_ZONE    "(TRFZNA)
         WA_HEADER-TAR_POST_CODE    "(PSTLZZ)
         WA_HEADER-SEARCH_TERM      "(KOSRT)
         WA_HEADER-SHIP_TYPE        "(SHTYP)
         WA_HEADER-MAT_NUM          "(MATNR)
         WA_HEADER-HAND_GRP                                 "(VEGR1)
         WA_HEADER-FROM_DATE        "(DATAB)
         WA_HEADER-TO_DATE          "(DATBI)
         WA_HEADER-RATE_UNIT        "(KONWA)
         WA_HEADER-SCALE_ID         "(Scale id)
         WA_HEADER-FREIGHT_RATE     "(ATWRT)
         WA_HEADER-SCALE_SEQ.       "(Scale Number)

        REPLACE ALL OCCURRENCES OF '/' IN WA_HEADER WITH '.'.
        REPLACE ALL OCCURRENCES OF '.' IN WA_HEADER-FREIGHT_RATE WITH ','.
        INSERT WA_HEADER INTO TABLE I_HEADER.
ENDLOOP.