Application Development and Automation 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: 
Read only

ABAP OO context for dynamic data

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,141

Hi Team,

I am having a small problem. I did this kind of things earlier but I dont know for some reason I was not able to do this now.

I have a WA declared like this in METHOD A.

DATA : CS_DATA TYPE REF TO DATA.

I enhanced SAP Standard Class and in METHOD B.

I need to assign the structure of the CS_DATA only when required fields come in METHOD B

Both METHOD A and B are called in three different classes N number of times.

For example there is a structure like XYZ and has Fields A,B,C.

I know the fields of the structure but not the structure name. It is very dynamic one.

so I need to capture the structure name in METHOD B so that I can assign that type to my field-symbol for further processing.

Please advice..

1 ACCEPTED SOLUTION
Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

There is a structure like XYZ and has Fields A,B,C.

I know the fields of the structure but not the structure name. It is very dynamic one.

so I need to capture the structure name in a METHOD so that I can assign that type to my field-symbol for further processing.

Please help

8 REPLIES 8
Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,579

There is a structure like XYZ and has Fields A,B,C.

I know the fields of the structure but not the structure name. It is very dynamic one.

so I need to capture the structure name in a METHOD so that I can assign that type to my field-symbol for further processing.

Please help

Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

No body responded.. I pity to the Topic Leader and area experts. Can someone please respond.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

V S BHARGAV MYLAVARAPU wrote:

No body responded.. I pity to the Topic Leader and area experts. Can someone please respond.

I think people have not responded because they have not understood the context of your question, atleast i haven't

A person cannot help unless s/he knows what is it that you want help on

You should provide more details, code snippet may be.

- Suhas

Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

Hi Suhas, i atleast expect that people will question like you as if they dont understand.

Anyhow I got the solution. But tell me from your side I want to hear any alternates if there are.

1. I have a structure : lets say CS_DATA Type ref to DATA.

2. In this structure at runtime there are many types assigned to it for each call of the method. The method is called n number of times. The assignment place is unknown.

lets say there are 4 times that this method is called.

I need to know what type is assigned to this WA (CS_DATA) everytime.

Did you got my question

Read only

0 Likes
1,578

Class CL_ABAP_STRUCTDESCR  can be used to get structure details in runtime.

Here is the code sample taken from class documentation, and it lists type/components of structure my_data in runtime.

TYPES:
   BEGIN OF my_struct,
     comp_a type i,
     comp_b type f,
   END OF my_struct.

DATA:
   my_data   TYPE my_struct,
   descr_ref TYPE ref to cl_abap_structdescr.

FIELD-SYMBOLS:
   <comp_wa> TYPE abap_compdescr.

START-OF-SELECTION.
   descr_ref ?= cl_abap_typedescr=>describe_by_data( my_data ).

   WRITE: / 'Typename     :', descr_ref->absolute_name.
   WRITE: / 'Kind         :', descr_ref->type_kind.
   WRITE: / 'Length       :', descr_ref->length.
   WRITE: / 'Decimals     :', descr_ref->decimals.
   WRITE: / 'Struct Kind  :', descr_ref->struct_kind.
   WRITE: / 'Components'.
   WRITE: / 'Name              Kind   Length   Decimals'.
   LOOP AT descr_ref->components ASSIGNING <comp_wa>.
     WRITE: / <comp_wa>-name, <comp_wa>-type_kind,
              <comp_wa>-length, <comp_wa>-decimals.
   ENDLOOP.

EDIT: For getting type from data reference, it looks like CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA_REF needs to be used.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

PARAMETERS p_type TYPE typename OBLIGATORY.

DATA:
       gd_data TYPE REF TO data,
       go_type TYPE REF TO cl_abap_typedescr.

TRY .
     CREATE DATA gd_data TYPE (p_type).
   CATCH cx_sy_create_data_error ##no_handler.

ENDTRY.

go_type = cl_abap_typedescr=>describe_by_data_ref( gd_data ).

WRITE: / go_type->absolute_name.

May be use the absolute type to process further. If you know that CS_DATA will always be a structure you can narrow cast to CL_ABAP_STRUCTDESCR without any worries

BR,

Suhas

Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,578

Thanks all. Got solved by own Logic.

Read only

surendra_battula
Participant
0 Likes
1,578

bhargav

RTTC makes it possible to create both elementary and complex data types (such as structures and internal tables) at runtime. This can be demonstrated using an internal table.

In class cl_abap_tabledescr, there is a method describe_by_name in which we pass the internal table itab and this method  returns the structure description. and then we use method create and pass this  structure type to parameter P_LINE_TYPE .


sample code

DATA:

DATA ref_itab TYPE REF TO data.

FIELD-SYMBOLS: <fs_itab> TYPE ANY TABLE.

DATA: r_linetype TYPE REF TO cl_abap_structdescr,

r_tabletype TYPE REF TO cl_abap_tabledescr,

key TYPE abap_keydescr_tab.

PARAMETERS pa_tab TYPE dd02l-tabname DEFAULT 'SPFLI'.

*create a tabletype with RTTC techniquue

*** call static method CREATE of specific RTTC-class

r_linetype ?= cl_abap_typedescr=>describe_by_name( pa_tab ).

r_tabletype = cl_abap_tabledescr=>create(

p_line_type = r_linetype ).

*** new technique with RTTC-type creation **************

*** dynamic creation of internal table data object ****

CREATE DATA ref_itab TYPE HANDLE r_tabletype.

* old technique without type creation ******************

* CREATE DATA ref_itab TYPE STANDARD TABLE OF (pa_tab)

* WITH NON-UNIQUE DEFAULT KEY.

ASSIGN ref_itab->* TO <fs_itab>

SELECT * FROM (pa_tab)

INTO TABLE <fs_itab>

UP TO 100 ROWS.

IF sy-subrc <> 0.

MESSAGE a702(bc401).

ENDIF.

pls ref to http://www.sapdev.co.uk/tips/dynamic-structure.htm

regards

surendra