‎2013 Sep 04 8:07 AM
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..
‎2013 Sep 04 9:06 AM
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
‎2013 Sep 04 9:06 AM
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
‎2013 Sep 04 10:02 AM
No body responded.. I pity to the Topic Leader and area experts. Can someone please respond.
‎2013 Sep 04 1:04 PM
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
‎2013 Sep 04 2:05 PM
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
‎2013 Sep 04 2:28 PM
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.
‎2013 Sep 04 2:38 PM
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
‎2013 Sep 05 8:07 AM
‎2013 Sep 04 11:16 AM
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