Monday, December 20, 2010
The below program demonstrate on how to create internal table dynamically using class method CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE.
 Steps

  • Based on table name given on selection screen, get all table fields from function module CREATE_DYNAMIC_TABLE.
  • Prepare fieldcatalog based on table fields. 
  • Create dynamic table using  cl_alv_table_create=>create_dynamic_table method.

Report  ztest_upload_update.
*&---------------------------------------------------------------------*
*& Declarations
*&---------------------------------------------------------------------*
*Types
types:
    ty_dfies     type dfies,
    ty_fcat      type lvc_s_fcat,
    begin of ty_file,
      data(4096) type c,
    end of ty_file.
*Type-pools
type-pools:
    truxs.
*Work areas
data:
    wa_dfies     type ty_dfies,
    wa_fcat      type ty_fcat,
    wa_file      type ty_file.
*Internal tables
data:
    it_dfies     type standard table of ty_dfies,
    it_fcat      type standard table of ty_fcat,
    it_file      type standard table of ty_file,
    it_data      type truxs_t_text_data.
*Variables
data:
    it_dyn_tab   type ref to data.
*Filed symbols
field-symbols:
   <it_table> type standard table.
*&---------------------------------------------------------------------*
*& selection-screen
*&---------------------------------------------------------------------*
parameters:
    p_table      type ddobjname,
    p_lfile      type char120.

*&---------------------------------------------------------------------*
*& selection-screen PBO
*&---------------------------------------------------------------------*
at selection-screen output.
  perform s-s-pbo.

*&---------------------------------------------------------------------*
*& start-of-selection.
*&---------------------------------------------------------------------*
start-of-selection.

  perform get_table_info.
  perform build_fieldcat.
  perform create_dynamic_table.
  perform upload_data.
  modify (p_table) from table <it_table>.
  if sy-subrc <> 0.
    write 'successfully updated'.
  else.
    write 'Updation unsuccessful'.
  endif.
*&---------------------------------------------------------------------*
*&      Form  S-S-PBO
*&---------------------------------------------------------------------*
form s-s-pbo .
  if p_table is not initial.
    concatenate 'C:\Documents and Settings\Administrator\Desktop\'
    p_table
    '.txt'
    into p_lfile.
  endif.
endform.                    " S-S-PBO
*&---------------------------------------------------------------------*
*&      Form  upload_data
*&---------------------------------------------------------------------*
form upload_data.

  data:
  l_fname type string.
  l_fname = p_lfile.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = l_fname
      filetype                = 'ASC'
    TABLES
      data_tab                = it_file
    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.

  append lines of it_file to it_data.
  replace all occurrences of '|' in table it_data with ';'.

  CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'
    EXPORTING
      i_field_seperator    = ';'
      i_tab_raw_data       = it_data
    TABLES
      i_tab_converted_data = <it_table>.
  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.                    " upload_data
*&---------------------------------------------------------------------*
*&      Form  GET_TABLE_INFO
*&---------------------------------------------------------------------*
form get_table_info .

  CALL FUNCTION 'DDIF_FIELDINFO_GET'
    EXPORTING
      tabname        = p_table
      langu          = sy-langu
    TABLES
      dfies_tab      = it_dfies
    EXCEPTIONS
      not_found      = 1
      internal_error = 2
      others         = 3.

endform.                    " GET_TABLE_INFO
*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCAT
*&---------------------------------------------------------------------*
form build_fieldcat .
  loop at it_dfies into wa_dfies.
    perform fieldcatalog using: wa_dfies-fieldname
    wa_dfies-tabname
    wa_dfies-fieldtext.
  endloop.
endform.                    " BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
form create_dynamic_table .

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_fcat
    IMPORTING
      ep_table        = it_dyn_tab.

  assign it_dyn_tab->* to <it_table>.

endform.                    " CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*&      Form  FIELDCATALOG
*&---------------------------------------------------------------------*
*      -->FIELD
*      -->TABLE
*      -->F_TXT
*----------------------------------------------------------------------*
form fieldcatalog using field table f_txt.

  wa_fcat-fieldname = field.
  wa_fcat-tabname   = table.
  wa_fcat-seltext = f_txt.

  append wa_fcat to it_fcat.
  clear  wa_fcat.

endform.                    " FIELDCATALOG

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.