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: 

Dynamically create a type

Former Member
0 Kudos
11,988

Hi,

is it possible to create dynamically a type ? I take a look on class CL_ABAP_TYPEDESCR but i can't create a new type, so is there a solution ?

Cheers

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
1,886

With all due respect to all, I think he means to create dynamic data types by means of RTTS.

@ ToraTora

Like this


"example table
DATA: BEGIN OF it OCCURS 3,
        pernr TYPE persno,
        kostl TYPE kostl,
        endda TYPE endda,
      END OF it.

"data references
DATA: r_type_struct TYPE REF TO cl_abap_structdescr,
      r_type_table  TYPE REF TO cl_abap_tabledescr,
      r_data_tab    TYPE REF TO data,
      r_data_str    TYPE REF TO data.

" 1. ------------- filling example table IT
it-pernr = '12345678'.
it-kostl = '0112345678'.
it-endda = sy-datum.
APPEND it.

it-pernr = '45678909'.
it-kostl = '3452345678'.
it-endda = sy-datum - 1.
APPEND it.

" 2. ------------ components structure type
gs_comp-name = 'PERNR'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'PERSNO' ).
APPEND gs_comp TO gt_comp.

gs_comp-name = 'KOSTL'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'KOSTL' ).
APPEND gs_comp TO gt_comp.

gs_comp-name = 'BEGDA'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'BEGDA' ).
APPEND gs_comp TO gt_comp.

" 3. ------------- create structure type
TRY.
    r_type_struct = cl_abap_structdescr=>create(
                              p_components = gt_comp ).
  CATCH cx_sy_struct_creation .
ENDTRY.

" 4. ------------- create table type
TRY.
    r_type_table = cl_abap_tabledescr=>create( r_type_struct ).

  CATCH cx_sy_table_creation .
ENDTRY.

" 5. -------------- create table based on RTTS types
CREATE DATA: r_data_tab TYPE HANDLE r_type_table,
             r_data_str TYPE HANDLE r_type_struct.

FIELD-SYMBOLS: <fs_table> TYPE INDEX TABLE,
               <fs_wa>    TYPE ANY.

ASSIGN: r_data_tab->* TO <fs_table>,
        r_data_str->* TO <fs_wa>.

You can also create any component you want without knowledge about data element it is refering to (without using methods describe_by_name or describe_by_data ).

So the 2nd step could be replaced with


gs_comp-name = 'PERNR'.
gs_comp-type ?= cl_abap_datadescr=>get_n( 8 ).   "PERNR if of type N(8)
APPEND gs_comp TO gt_comp.

gs_comp-name = 'KOSTL'.
gs_comp-type ?= cl_abap_elemdescr=>get_c( 10 ).  "C(10)
APPEND gs_comp TO gt_comp.

gs_comp-name = 'BEGDA'.
gs_comp-type ?= cl_abap_elemdescr=>get_d( ).   "D
APPEND gs_comp TO gt_comp.

Regards

Marcin

10 REPLIES 10

kesavadas_thekkillath
Active Contributor
0 Kudos
1,886

What do you mean by dynamic type ?

check this

data:wf_ref TYPE REF TO data,

wf_line TYPE REF TO data.

field-symbols:<fs_tab> TYPE STANDARD TABLE.

CREATE DATA wf_ref TYPE TABLE OF (wf_tab). "Dynamic Itab

ASSIGN wf_ref->* TO <fs_tab>.

CREATE DATA wf_line LIKE LINE OF <fs_tab>. "Dynamic Work area

0 Kudos
1,886

Hi,

I think, Dynamic type means the one we declare using TYPES has to be dynamic.

You can create the fieldcatelog and call the method cl_alv_table_create=>create_dynamic_table


  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
*     I_STYLE_TABLE             =
      it_fieldcatalog           = i_fieldcat_dyn
    IMPORTING
      ep_table                  = <fs_table>
*     E_STYLE_FNAME             =
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2
            .

Here in internal table i_fieldcat_dyn define the fields dynamically based on the requirement.

Thanks,

Vinod.

0 Kudos
1,886

Hi Vinod,

Now i got it, just some more additions.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <fs_table> TYPE ANY TABLE,

<fs_line> TYPE ANY,

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = i_fieldcat_dyn

IMPORTING

ep_table = new_table

EXCEPTIONS

generate_subpool_dir_full = 1

OTHERS = 2.

ASSIGN newtable->* TO <fs_table>.

CREATE DATA new_line LIKE LINE OF <fs_table>.

ASSIGN new_line->* TO <l_line>.

MarcinPciak
Active Contributor
1,887

With all due respect to all, I think he means to create dynamic data types by means of RTTS.

@ ToraTora

Like this


"example table
DATA: BEGIN OF it OCCURS 3,
        pernr TYPE persno,
        kostl TYPE kostl,
        endda TYPE endda,
      END OF it.

"data references
DATA: r_type_struct TYPE REF TO cl_abap_structdescr,
      r_type_table  TYPE REF TO cl_abap_tabledescr,
      r_data_tab    TYPE REF TO data,
      r_data_str    TYPE REF TO data.

" 1. ------------- filling example table IT
it-pernr = '12345678'.
it-kostl = '0112345678'.
it-endda = sy-datum.
APPEND it.

it-pernr = '45678909'.
it-kostl = '3452345678'.
it-endda = sy-datum - 1.
APPEND it.

" 2. ------------ components structure type
gs_comp-name = 'PERNR'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'PERSNO' ).
APPEND gs_comp TO gt_comp.

gs_comp-name = 'KOSTL'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'KOSTL' ).
APPEND gs_comp TO gt_comp.

gs_comp-name = 'BEGDA'.
gs_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'BEGDA' ).
APPEND gs_comp TO gt_comp.

" 3. ------------- create structure type
TRY.
    r_type_struct = cl_abap_structdescr=>create(
                              p_components = gt_comp ).
  CATCH cx_sy_struct_creation .
ENDTRY.

" 4. ------------- create table type
TRY.
    r_type_table = cl_abap_tabledescr=>create( r_type_struct ).

  CATCH cx_sy_table_creation .
ENDTRY.

" 5. -------------- create table based on RTTS types
CREATE DATA: r_data_tab TYPE HANDLE r_type_table,
             r_data_str TYPE HANDLE r_type_struct.

FIELD-SYMBOLS: <fs_table> TYPE INDEX TABLE,
               <fs_wa>    TYPE ANY.

ASSIGN: r_data_tab->* TO <fs_table>,
        r_data_str->* TO <fs_wa>.

You can also create any component you want without knowledge about data element it is refering to (without using methods describe_by_name or describe_by_data ).

So the 2nd step could be replaced with


gs_comp-name = 'PERNR'.
gs_comp-type ?= cl_abap_datadescr=>get_n( 8 ).   "PERNR if of type N(8)
APPEND gs_comp TO gt_comp.

gs_comp-name = 'KOSTL'.
gs_comp-type ?= cl_abap_elemdescr=>get_c( 10 ).  "C(10)
APPEND gs_comp TO gt_comp.

gs_comp-name = 'BEGDA'.
gs_comp-type ?= cl_abap_elemdescr=>get_d( ).   "D
APPEND gs_comp TO gt_comp.

Regards

Marcin

0 Kudos
1,886

>

> With all due respect to all, I think he means to create dynamic data types by means of RTTS.

And i think creating dynamic program components using RTTS is much easier to understand / maintain than the cl_alv_table_create=>create_dynamic_table . In the latter you donot have an insight how the field catalog is converted into a dynamic table.

In the RTTS method, the entire process can be broken down into individual steps & easily understood (as you have already mentioned in your post)

Any comments are welcome.

Cheers,

Suhas

PS: BTW Marcin very good example. 10 p*ints from me

0 Kudos
1,886

Hi all,

big thanks to Marcin, he understand well my need, I did everything I wanted.

And i agree with Suhas it's more easier and flexibe to understand RTTS.

Cheers

0 Kudos
1,886

Any comments are welcome.

I will dare to add mine then.

BTW: thanks for kind words, as usual you make my day:)

RTTS also allow to create individual data types, complex structures (including nested ones) and even reference types. This cannot be achieved in any way by means of create_dynamic_table . The fieldcatalog this method uses however allows to type components not only refering to DDIC ones.

Anyhow I think the main difference in using both techniques is that first serves the purpose of creating only types , which we can later use to create real data objects. The latter, in contrary allows only to create data objects, more specifically a table. Of course deriving the work area from it is feasible later (as shown by Keshav).

So depending on context we can use one technique as a replacement for the other. Personally it gives me a great joy to work with RTTS as I am able to "sculpt" every single data element.

Cheers

Marcin

0 Kudos
1,886

Hello Marcin,

can you please please share another (complete) example ? on creating a dynamic type. I will really appreciate your help.

My requirement is that i have developed a complex code using the normal internal tables and defined normal structures in it. After completion of the code my requirement changed as to make all the structures dynamic and so internal tables.

I created a dynamic internal table by passing CALL METHOD cl_alv_table_create=>create_dynamic_table. By using this method i can get a field symbol as my internal table ( my old code is in normal internal tables.. i cannot change the entire code now to fieldsymbols).

I will use your approach of creating dynamic types and use my same old internal tables.

Please help me!

1,886

This message was moderated.

Former Member
0 Kudos
1,886

Everything is possible in sap ABAP, you can create dynamic internal table, structure, data element, field, table and class so firstly tell me which thing you want to create..

Thanks

Nammi.