Application Development 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: 

RTTS, RTTI and RTTC

lakshminarasimhan_n4
Active Contributor
0 Kudos

Hi Gurus,

I am a SAP BI consultant and require help from you people.

I have a internal table, it is filled in run time by the users. The user can enter any number of data elements.

Now i need to create a structure, with components and the type of the componenets must be the data elements entered by the user.

Ex :

User enters some data elements (A,B,C). "They can enter any number of data elements, not just 3.

These data elements are stored in an internal table "int" by me.

Now i require a structure / type.

type begin of xx,

component 1 type A.

component 2 type B.

component 3 type C.

endof xx.

The challenge is, i do not know how many data elements that user might enter.

I could know only at run time. I have to calculate the number of lines of internal table "int" and decide the total number of data elements. Then i need to create a dynamic type / structure at run time, which contain the same number of components.

From the structure i need to create a dynamic internal table.

I know that from my previous posts that we could use RTTS, RTTI and RTTI.

All of the examples are shown only for the table "SPFLI". in my case there is no standard DDIC type maintained at SE11.

We have to create at runtime and then generate an internal table for it. My case its more complicated.

Please suggest.

Regards,

Lakshminarasimhan.N

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Halo,

Lets call your internal data elements as it_data_elements and

work area is wa_data_element.



DATA: l_elem_descr  type ref to cl_abap_elemdescr,
          l_table_descr type ref to cl_abap_tabledescr,
         l_struct_descr type ref to cl_abap_structdescr.

data: dref type ref to data.
    
Loop at it_data_elements into wa_data_element.
concatenate 'COMP' sy-tabix into l_name.

l_elem_descr ?= cl_abap_elemdescr=>describe_by_name( wa_data_element ).
l_comp-type = l_elem_descr .
l_comp-name = l_name.
append l_comp to lt_comp.

endloop.

" Now you should create the structure descr from lt_comp.
l_struct_descr  = cl_abap_structdescr=>create( lt_comp ).

"Now you should create table descr from struct descr.
l_table_descr = cl_abap_tabledescr=>create(
                  p_line_type  = l_struct_descr 
                  p_table_kind = cl_abap_tabledescr=>tablekind_std
                  p_unique     = abap_false ).

For accessing and filling up the data.

You have to create a data reference variable for the same

create data dref type handle l_table_descr.
assign dref->* <ft_table>.


Regards

Arshad

11 REPLIES 11

asik_shameem
Active Contributor
0 Kudos

Hi,

It seems to be quite straight forward. I couldn't understand what complication you are facing to acheive this.

Do like this.

LOOP AT int.
  gwa_fieldcatalog-fieldname = int-field_name.
  gwa_fieldcatalog-intlen    = int-length.
  gwa_fieldcatalog-datatype  = int-type.
  APPEND gwa_fieldcatalog TO gt_fieldcatalog.
ENDLOOP.

*Create dynamic table
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = gt_fieldcatalog
  IMPORTING
    ep_table        = gref_table.

ASSIGN gref_table->* TO <gt_itab>. " <GT_ITAB> is your Internal Table

* Create work Area
CREATE DATA gref_wa LIKE LINE OF <gt_itab>.

ASSIGN gref_wa->* TO <gwa_itab>.   " <GWA_ITAB> is your work area

MarcinPciak
Active Contributor
0 Kudos

If the user provides data element names then you can refer them directly as you would be refering any other DDIC object


PARAMETERS pa_datel TYPE c LENGTH 40.

DATA gr_elem TYPE REF TO cl_abap_elemdescr.

gr_elem ?= cl_abap_typedescr=>describe_by_name( pa_datel ).

Then you do the same for every single data element. All these descriptors (RTTI results) have to be appended to special table ?(of type cl_abap_structdescr=>component_table ), which you eventually use to generate structure from its components.

If you have basic types instead of DDIC ones, you need to create each component using GET_... method of cl_abap_elemedescr . Then you repeat the process collecting all components' descriptions in the table -> then generate structure same as above.

I described the process in detail in [Do you really know everything about typing? - part 2|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/20887] [original link is broken] [original link is broken] [original link is broken];

Regards

Marcin

Former Member
0 Kudos

Halo,

Lets call your internal data elements as it_data_elements and

work area is wa_data_element.



DATA: l_elem_descr  type ref to cl_abap_elemdescr,
          l_table_descr type ref to cl_abap_tabledescr,
         l_struct_descr type ref to cl_abap_structdescr.

data: dref type ref to data.
    
Loop at it_data_elements into wa_data_element.
concatenate 'COMP' sy-tabix into l_name.

l_elem_descr ?= cl_abap_elemdescr=>describe_by_name( wa_data_element ).
l_comp-type = l_elem_descr .
l_comp-name = l_name.
append l_comp to lt_comp.

endloop.

" Now you should create the structure descr from lt_comp.
l_struct_descr  = cl_abap_structdescr=>create( lt_comp ).

"Now you should create table descr from struct descr.
l_table_descr = cl_abap_tabledescr=>create(
                  p_line_type  = l_struct_descr 
                  p_table_kind = cl_abap_tabledescr=>tablekind_std
                  p_unique     = abap_false ).

For accessing and filling up the data.

You have to create a data reference variable for the same

create data dref type handle l_table_descr.
assign dref->* <ft_table>.


Regards

Arshad

0 Kudos

Hi,

Thanks for your reply.

In the code given above, the elements l_comp-type and lt_comp are not declared.

To the best of m undersatnding, are they of type,

l_comp TYPE cl_abap_structdescr=>component,

lt_comp TYPE cl_abap_structdescr=>component_table. ?

0 Kudos

Yes

0 Kudos

Hi Arshad Ansary,

Thanks for your reply. You have helped me in creating the dynamic structure "l_struct_descr" and a dynamic internal table "l_table_descr" from that structure. Now i have captured their references in two field symbols,

FIELD-SYMBOLS : <wa> TYPE ANY, <itab> TYPE STANDARD TABLE.

ASSIGN dref->* TO <wa>. " Structure

ASSIGN dref->* TO <itab>. " internal table.

Now i have a internal table with values and i need to supply those values to the field symbol <wa>, then i need to insert the <wa> into the table <itab>.

For ex: my internal table "etk_row" contains all values,

loop at etk_row inro wa_etk_row.

assign component 3 of structure etk_wa to <wa>-/BIC/OIZFECOMTYP.

insert <wa> into <itab>.

endloop.

now the above code gives me error,

"The data object <wa> has no structure and therefore no component called /BIC/OIZFECOMTYP. "".

But in my debugger i can see <wa> is a field symbol and it is a structure type of Flat Structure(12). it contains components

/BIC/OIZFECOMTYP

/BIC/OIZFESUBCT

How to assign the data of my internal table to the field symbol <wa> which is a structure type and has two components

/BIC/OIZFECOMTYP

/BIC/OIZFESUBCT

Regards,

Lakshminarasimhan.N

0 Kudos

The anwser is simple. How the system will know that your field symbols are of your structure type? You have defined them as of type ANY and STANDARD TABLE . So the system doesn't statically know the structure, but dynamically he does. Therefore you need to address these components dynamically.


field-symbols: <comp_src> type any,
                        <comp_trg> type any.

loop at etk_row inro wa_etk_row.
"source component
   assign component 3 of structure wa_etk_row to <comp_src>. 
"target component (addressed dynamically)
   assign component '/BIC/OIZFECOMTYP' of structure <wa> to <comp_trg>.    
   <comp_trg> = <comp_src>.
   insert <wa> into <itab>.
endloop.

Regards

Marcin

0 Kudos

In continuation to above logic you can also use move corresponding if the field name is same. Move corresponding also works for the run time objects.

BR,

Raj

0 Kudos

>

> In continuation to above logic you can also use move corresponding if the field name is same. Move corresponding also works for the run time objects.

You can! But definitely not a fool-proof solution

0 Kudos

I don't see any harm in the move corresponding statement rather in assign statement i feel it would be hardcoded like component name BIC/OIZFECOMTYP or number as '3'.


 assign component 3 of structure wa_etk_row to <comp_src>. 
"target component (addressed dynamically)
   assign component '/BIC/OIZFECOMTYP' of structure <wa> to <comp_trg>.    

BR,

Raj

0 Kudos

Thanks a lot...The problem has been solved.

I was able to create dynamic structure and dynamic internal table and then assign all data to the internal table.