‎2013 May 27 12:04 PM
Hi ABAP gurus,
I have a ABAP OO question. After using an anonymous data object, I need to find out at what position a certain fieldNAME is, not the VALUE of the field (I know that can be found out with ASSIGN COMPONENT keywords).
CONCATENATE '/1CPMB/LKWS' g_field1 g_field2 INTO g_tabname.
DATA: struc TYPE REF TO data.
FIELD-SYMBOLS: <struc> TYPE any.
CREATE DATA struc TYPE (g_tabname).
ASSIGN struc->* TO <struc>.
SELECT * FROM (g_tabname) INTO <struc> UP TO 1 ROWS.
ENDSELECT.
FIELD-SYMBOLS: <comp> TYPE any.
DO.
ASSIGN COMPONENT/FIELD NAME sy-index
OF STRUCTURE <struc> TO <comp>.
IF sy-subrc = 0.
--> use field-symbol <comp> here <--
ELSE.
EXIT.
ENDIF.
ENDDO.
Is there any such statement? Thanks in advance.
‎2013 May 28 8:00 AM
Hello Wendy,
So basically you want to get the position of the field 'ENTITY' so that you can read the content, correct me if i'm wrong.
ASSIGN COMPONENT 'ENTITY' OF STRUCTURE <struc> TO <comp>.
BR,
Suhas
‎2013 May 27 7:47 PM
Hi Wendy,
I'm not sure to understand the whole question, but maybe this code will help you
* Get the fields catalog.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = w_tabname
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 3.
IF sy-subrc NE space.
RAISE not_an_allowed_table.
ENDIF.
* Loop on the table content.
LOOP AT <dyn_tab>
INTO <dyn_str>
FROM w_start TO w_end.
* Max entries.
* IF NOT W_SIZE_PAGE IS INITIAL.
* CHECK sy-tabix LE W_SIZE_PAGE.
* ENDIF.
* Initialize
CLEAR : w_pos, is_out.
* Loop on fields.
LOOP AT it_fcat
INTO is_fcat.
* Check if we would like the colum.
IF NOT it_column[] IS INITIAL.
READ TABLE it_column
INTO is_column
WITH KEY fieldname = is_fcat-fieldname.
CHECK sy-subrc EQ space.
ENDIF.
* Save the colum.
ASSIGN COMPONENT is_fcat-fieldname
OF STRUCTURE <dyn_str>
TO <field>.
IF is_fcat-datatype NE 'CHAR'.
WRITE <field> TO w_field.
CONDENSE w_field NO-GAPS.
ELSE.
MOVE <field> TO w_field.
ENDIF.
MOVE w_field TO is_out+w_pos(is_fcat-dd_outlen).
w_pos = w_pos + is_fcat-dd_outlen.
ENDLOOP.
* If internal table export.
APPEND is_out TO it_out.
ENDLOOP.
IF w_mode_memory EQ 'M' OR w_mode_memory EQ 'N'.
EXPORT <dyn_tab> TO MEMORY ID w_mem_id.
w_message = ' et Export dans la mémoire'.
ENDIF.
ENDIF.
regards
Fred
‎2013 May 28 11:14 AM
‎2013 May 28 11:14 AM
‎2013 May 28 11:18 AM
Hi Frédéric,
Your answer was helpful and correct for 2 reasons:
- LVC_FIELDCATALOG_MERGE worked fine, I did not know about this FM
- it contained the ASSIGN COMPONENT with <fieldname> statement, I did not know I could use fieldname in stead of sy-index
Many thanks!
Wendy
‎2013 May 28 6:22 AM
Hi,
* Get the fields catalog.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = w_tabname
CHANGING
ct_fieldcat = it_fcat
EXCEPTIONS
OTHERS = 3.
IF sy-subrc NE space.
RAISE not_an_allowed_table.
ENDIF.
Read the field for which you want to find the position...
From work area you can find its position in the current table.
Field 'COL_POS'.
Hope this solves you issue.
‎2013 May 28 11:22 AM
Hi Arunkumar,
Thanks for your help. I didn't know about this FM, it works fine.
Wendy
‎2013 May 28 7:47 AM
Hello,
This example will return a table with information about the fields in the structure.
DATA: g_tabname TYPE tabname VALUE 'MARA'.
DATA: rf_root TYPE REF TO cx_root.
DATA: lp_msg TYPE string.
DATA: lp_lines TYPE i.
DATA: lr_type TYPE REF TO cl_abap_typedescr.
DATA: lr_structure TYPE REF TO cl_abap_structdescr.
DATA: ev_exists TYPE flag.
DATA: lt_components TYPE abap_compdescr_tab.
DATA: ls_components TYPE abap_compdescr.
CALL METHOD cl_abap_structdescr=>describe_by_name
EXPORTING
p_name = g_tabname
RECEIVING
p_descr_ref = lr_type
EXCEPTIONS
type_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
ev_exists = 'X'.
ENDIF.
TRY.
lr_structure ?= lr_type.
CATCH cx_root INTO rf_root.
lp_msg = rf_root->get_text( ).
ENDTRY.
IF lr_structure IS BOUND.
lt_components = lr_structure->components.
ENDIF.
DESCRIBE TABLE lt_components LINES lp_lines.
LOOP AT lt_components INTO ls_components.
WRITE:/ ls_components-length,
ls_components-decimals,
ls_components-type_kind,
ls_components-name.
ENDLOOP.
‎2013 May 28 8:56 AM
I would replace method "describe_by_name" with "describe_by_data" here.
lr_type ?= cl_abap_structdescr=>describe_by_data( p_data = <struc> ).
Use search tool on cl_abap_structdescr=>describe_by_data.
Regards,
Raymond
PS: On a strict OO programming, you may be required to pass the internal table by reference (for the ANY type) so use describe _by_data_ref.
‎2013 May 28 11:26 AM
Hi Edwin,
I had tried this method CALL METHOD cl_abap_structdescr=>describe_by_name earlier, but wasn't succesful in determination of components.
With your code I know how to do it now.
Thank you very much!
Wendy
‎2013 May 28 11:28 AM
Hi Raymond,
This is also a very good idea.
I am picking up all the methods of this class quickly.
Thank you,
Wendy
‎2013 May 28 8:00 AM
Hello Wendy,
So basically you want to get the position of the field 'ENTITY' so that you can read the content, correct me if i'm wrong.
ASSIGN COMPONENT 'ENTITY' OF STRUCTURE <struc> TO <comp>.
BR,
Suhas
‎2013 May 28 11:33 AM
Hi Suhas!
Your answer was just PERFECT!
Thank you for helping, it is a great experience to be helped.
Wendy
‎2013 May 28 11:43 AM
Thank you for helping, it is a great experience to be helped.That's what SCN is supposed to do
And thanks to you for providing your feedback against each reply, really appreciate it! Hope other OPs can learn something from this, amen!
Cheers,
Suhas