2015 Apr 15 11:46 AM
Dear ALL,
I have created a dynamic table using DOC-53788, but due to the special character in my input, i have used "p_strict" in my code as shown below.
TRY.
gr_struct_typ ?= cl_abap_structdescr=>create( p_components = gt_component p_strict = cl_abap_structdescr=>false ).
CATCH cx_sy_struct_creation.
ENDTRY.
but again a new exception is coming and as i am the new one in abap , so unable to handle this issue please help me out.
Error analysis
An exception occurred which is explained in detail below.
The exception, which is assigned to class 'CX_SY_TABLE_LINE_TYPE',
caught and
therefore caused a runtime error.
The reason for the exception is:
When you created a tyble type, an empty row type was specified.
Complete code is given below..
*&---------------------------------------------------------------------*
*& Report ZHR_EMP_DESIG1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZHR_EMP_DESIG1.
*&---------------------------------------------------------------------*
*& Report ZDYNAMIC_TABLE
*&---------------------------------------------------------------------*
TYPE-POOLS : slis.
TYPES : BEGIN OF ty_final_summary,
ZZ_DESIGTEXT TYPE string, "DESIGNATION
BTEXT TYPE string, "SSA
TOT TYPE i. "TOTAL
TYPES : END OF ty_final_summary.
* Input table declarations
DATA : it_final_summary TYPE TABLE OF ty_final_summary,
wa_final_summary LIKE LINE OF it_final_summary,
wa_final_summary1 LIKE LINE OF it_final_summary.
** Dynamic Table Declarations
DATA : gt_dyn_table TYPE REF TO data,
gw_dyn_line TYPE REF TO data,
gw_dyn_line1 TYPE REF TO data.
* Field Symbold Declaration
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.
* RTTS Declaratoins.
DATA : gr_struct_typ TYPE REF TO cl_abap_datadescr,
gr_dyntable_typ TYPE REF TO cl_abap_tabledescr,
ls_component TYPE cl_abap_structdescr=>component,
gt_component TYPE cl_abap_structdescr=>component_table.
* SALV Declarations.
DATA : lo_cols TYPE REF TO cl_salv_columns,
lo_salv_table TYPE REF TO cl_salv_table,
lo_column TYPE REF TO cl_salv_column,
col_name(30),
col_desc(20).
*START-OF-SELECTION.
* Populate the initial input table. Usually this input table contents will be populated at run time, which raises the requirement of dynamic table. The table contents are filled here for illustration purpose.
perform fill_table using :
'SDE' 'CO, ND' '100',
'JTO' 'CO, ND' '250',
'SDE' 'MTR' '200',
'JTO' 'MTR' '150',
'TTA' 'MTR' '250'.
WRITE : / 'Initial Internal Table', /.
WRITE :/(6) 'DESIGNATION',(12) 'SSA' , (3) 'TOT' .
LOOP AT it_final_summary INTO wa_final_summary.
WRITE :/ wa_final_summary-ZZ_DESIGTEXT, wa_final_summary-BTEXT, wa_final_summary-TOT.
ENDLOOP.
* Create structure of dynamic internal table – DESIGNATION SSA1 SSA2...
ls_component-name = 'ZZ_DESIGTEXT '.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_final_summary-ZZ_DESIGTEXT ).
APPEND ls_component TO gt_component.
*Loop through the internal table creating a column for every distinct SSA in the internal table
LOOP AT it_final_summary INTO wa_final_summary.
* Search the component table if the SSA column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_final_summary-btext.
IF sy-subrc NE 0.
* The name of the column would be the SSA and the data type would be same as the TOT field of internal table.
ls_component-name = wa_final_summary-btext.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_final_summary-tot ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_final_summary.
ENDLOOP.
TRY.
gr_struct_typ ?= cl_abap_structdescr=>create( p_components = gt_component p_strict = cl_abap_structdescr=>false ).
CATCH cx_sy_struct_creation.
ENDTRY.
gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).
CREATE DATA:
gt_dyn_table TYPE HANDLE gr_dyntable_typ,
gw_dyn_line TYPE HANDLE gr_struct_typ,
gw_dyn_line1 TYPE HANDLE gr_struct_typ.
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
ASSIGN gw_dyn_line->* TO <gfs_line>.
ASSIGN gw_dyn_line1->* TO <gfs_line1>.
*
** Populate the dynamic table
*
LOOP AT it_final_summary INTO wa_final_summary.
* Avoid duplicate entries for key field DESIGNATION.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('ZZ_DESIGTEXT') = wa_final_summary-ZZ_DESIGTEXT.
IF sy-subrc = 0.
*if <gfs_line1> is ASSIGNED.
* UNASSIGN <gfs_line1>.
CONTINUE.
ENDIF.
ASSIGN COMPONENT 'ZZ_DESIGTEXT ' OF STRUCTURE <gfs_line> TO <fs1>.
*if <fs1> is ASSIGNED.
<fs1> = wa_final_summary-ZZ_DESIGTEXT.
UNASSIGN <fs1>.
**endif.
LOOP AT gt_component INTO ls_component.
IF ls_component-name = 'ZZ_DESIGTEXT '.
CONTINUE.
ENDIF.
READ TABLE it_final_summary WITH KEY ZZ_DESIGTEXT = wa_final_summary-ZZ_DESIGTEXT btext = ls_component-name INTO wa_final_summary1.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = wa_final_summary1-tot.
UNASSIGN <fs1>.
ENDIF.
ENDIF.
CLEAR : wa_final_summary1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
CLEAR: wa_final_summary, wa_final_summary1.
ENDLOOP.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv_table
CHANGING
t_table = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.
* get columns object
lo_cols = lo_salv_table->get_columns( ).
*…Individual Column Names
LOOP AT gt_component INTO ls_component.
TRY.
col_name = ls_component-name.
lo_column = lo_cols->get_column( col_name ). " < <
IF col_name = 'ZZ_DESIGTEXT'.
col_desc = 'DISGNATION'.
ELSE.
col_desc = col_name.
"CONCATENATE col_name '''13' INTO col_desc.
ENDIF.
lo_column->set_medium_text( col_desc ).
lo_column->set_output_length( 10 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
ENDLOOP.
* display table
lo_salv_table->display( ).
FORM fill_table
USING p_fld1 TYPE ty_final_summary-ZZ_DESIGTEXT
p_fld2 TYPE ty_final_summary-btext
p_fld3 TYPE ty_final_summary-tot.
clear wa_final_summary.
wa_final_summary-ZZ_DESIGTEXT = p_fld1 .
wa_final_summary-btext = p_fld2.
wa_final_summary-tot = p_fld3.
APPEND wa_final_summary TO it_final_summary.
ENDFORM.
2015 Apr 16 7:53 PM
You have duplicate fields in your table structure alongwith the special character ','. You need to have unique name for each field. Also replace all special characters with '_' using the regex '[^0-9a-zA-Z]+'
perform fill_table using :
'SDE' 'CO, ND' '100',
'JTO' 'CO, ND' '250',
'SDE' 'MTR' '200',
'JTO' 'MTR' '150',
'TTA' 'MTR' '250'.
For unique name and replace, you can do something like this. Use the field COLNAME to create the final table:
TYPES : BEGIN OF ty_final_summary, ZZ_DESIGTEXT TYPE string, "DESIGNATION BTEXT TYPE string, "SSA TOT TYPE i, "TOTAL org_text type char30, colname type char30. TYPES : END OF ty_final_summary.
TYPES : END OF ty_final_summary.
DATA : it_final_summary TYPE TABLE OF ty_final_summary,
wa_final_summary LIKE LINE OF it_final_summary.
perform fill_table using :
'SDE' 'CO, ND' '100',
'JTO' 'CO, ND' '250',
'SDE' 'MTR' '200',
'JTO' 'MTR' '150',
'TTA' 'MTR' '250'.
FORM fill_table
USING p_fld1 TYPE ty_final_summary-ZZ_DESIGTEXT
p_fld2 TYPE ty_final_summary-btext
p_fld3 TYPE ty_final_summary-tot.
data: lv_cnt type i.
clear wa_final_summary.
wa_final_summary-ZZ_DESIGTEXT = p_fld1 .
wa_final_summary-btext = p_fld2.
wa_final_summary-tot = p_fld3.
wa_final_summary-org_text = wa_final_summary-btext.
replace all OCCURRENCES OF REGEX '[^0-9a-zA-Z]+'
in wa_final_summary-org_text
with '_'.
loop at it_final_summary TRANSPORTING NO FIELDS
where org_text = wa_final_summary-org_text.
lv_cnt = lv_cnt + 1.
ENDLOOP.
wa_final_summary-colname = lv_cnt.
CONDENSE wa_final_summary-colname.
CONCATENATE wa_final_summary-org_text wa_final_summary-colname
into wa_final_summary-colname.
APPEND wa_final_summary TO it_final_summary.
ENDFORM.
Regards,
Naimesh Patel