Monday, 5 August 2013

Dynamic Internal Table Creation using RTTS!

*&---------------------------------------------------------------------*
*&      Form  CREATE_DYNAMIC
*&---------------------------------------------------------------------*
*       Dynamic Structure!!
*----------------------------------------------------------------------*

form create_dynamic .

* Dynamic Table creation
  
datalo_struct   type ref to cl_abap_structdescr,
        lo_element  
type ref to cl_abap_elemdescr,
        lo_new_type 
type ref to cl_abap_structdescr,
        lo_new_tab  
type ref to cl_abap_tabledescr,
        lo_data     
type ref to data,
        lo_line     
type ref to data,
        lt_comp     
type cl_abap_structdescr=>component_table,
        lt_tot_comp 
type cl_abap_structdescr=>component_table,
        la_comp     
like line of lt_comp.

  
data lt_0002    type table of ty_0002.
  
field-symbols <lfs_0002> type ty_0002.

  
data  lv_index    type numc2.

  
check t_out[] is not initial.

*======================================================================*
*--------------------* Dynamic Internal Table *------------------------*
*======================================================================*

*======================================================================*
*---* Comman Structure...
*======================================================================*

* 1. Getting Compoents from existing type
 
  lo_struct ?= cl_abap_typedescr
=>describe_by_name'TY_FINAL' ).
  lt_comp  
lo_struct->get_components( ).
  
append lines of lt_comp to lt_tot_comp.



*======================================================================*
*---* For With Nominee
*======================================================================*
* 2. Adding required fields based on the single data element
 
  
do v_value times.
    
move sy-index to v_index.
    lo_element ?= cl_abap_elemdescr
=>describe_by_name'PIN_RELTN' ).
    
concatenate 'REL' v_index into la_comp-name.
    
try.
        la_comp
-type cl_abap_elemdescr=>get_c(
            p_length 
=  lo_element->length ).
      
catch cx_parameter_invalid_range .
    
endtry.
    
append la_comp to lt_tot_comp.
    
clearla_comp.

    lo_element ?= cl_abap_elemdescr
=>describe_by_name'PIN_NNAME' ).
    
concatenate 'NAM' v_index into la_comp-name.
    
try.
        la_comp
-type cl_abap_elemdescr=>get_c(
            p_length 
=  lo_element->length ).
      
catch cx_parameter_invalid_range .
    
endtry.
    
append la_comp to lt_tot_comp.
    
clearla_comp.

    lo_element ?= cl_abap_elemdescr
=>describe_by_name'PIN_DATOB' ).
    
concatenate 'DAT' v_index into la_comp-name.
    
try.
        la_comp
-type cl_abap_elemdescr=>get_c(
            p_length 
=  lo_element->length ).
      
catch cx_parameter_invalid_range .
    
endtry.
    
append la_comp to lt_tot_comp.
    
clearla_comp.

    lo_element ?= cl_abap_elemdescr
=>describe_by_name'PIN_NMADD' ).
    
concatenate 'ADD' v_index into la_comp-name.
    
try.
        la_comp
-type cl_abap_elemdescr=>get_c(
            p_length 
=  lo_element->length ).
      
catch cx_parameter_invalid_range .
    
endtry.
    
append la_comp to lt_tot_comp.
    
clearla_comp.
  
enddo.

*======================================================================*
*---* For Without Nominee
*======================================================================*
 
  
if v_value is initial.
    
check lt_tot_comp[] is not initial.
    lo_element ?= cl_abap_elemdescr
=>describe_by_name'CHAR10' ).
    
move 'DOJ' to la_comp-name.
    
try.
        la_comp
-type cl_abap_elemdescr=>get_c(
            p_length 
=  lo_element->length ).
      
catch cx_parameter_invalid_range .
    
endtry.
    
append la_comp to lt_tot_comp.
    
clearla_comp.

    
clear v_indexlv_index.

    v_index 
linest_t591s[] ).

    
loop at t_t591s assigning <fs_t591s>.
      lo_element ?= cl_abap_elemdescr
=>describe_by_name'CHAR03' ).
      
move <fs_t591s>-subty to la_comp-name.
      
try.
          la_comp
-type cl_abap_elemdescr=>get_c(
              p_length 
=  lo_element->length ).
        
catch cx_parameter_invalid_range .
      
endtry.
      
append la_comp to lt_tot_comp.
      
clearla_compv_addrlv_index.
    
endloop.
    unassign <fs_t591s>
.
  
endif.

*======================================================================*
*---* Comman fields...
*======================================================================*
 
  
check lt_tot_comp[] is not initial.
  lo_element ?= cl_abap_elemdescr
=>describe_by_name'CHAR10' ).
  
move 'KDATE' to la_comp-name.
  
try.
      la_comp
-type cl_abap_elemdescr=>get_c(
          p_length 
=  lo_element->length ).
    
catch cx_parameter_invalid_range .
  
endtry.
  
append la_comp to lt_tot_comp.
  
clearla_comp.

  lo_element ?= cl_abap_elemdescr
=>describe_by_name'CHAR10' ).
  
move 'RDATE' to la_comp-name.
  
try.
      la_comp
-type cl_abap_elemdescr=>get_c(
          p_length 
=  lo_element->length ).
    
catch cx_parameter_invalid_range .
  
endtry.
  
append la_comp to lt_tot_comp.
  
clearla_comp.

*======================================================================*
*---* Create Dynamic Internal Table & Structure...
*======================================================================*
 
  lo_new_type 
cl_abap_structdescr=>createlt_tot_comp ).
  lo_new_tab 
cl_abap_tabledescr=>create(
                   p_line_type  
lo_new_type
                   p_table_kind 
cl_abap_tabledescr=>tablekind_std
                   p_unique     
abap_false ).

  
create data lo_data type handle lo_new_tab.
  
assign lo_data->*   to <fs_final>.
  
create data lo_line type handle lo_new_type.
  
assign lo_line->*   to <fs_line>.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.