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

Determining the Fields of Data Object

w_k
Explorer
0 Likes
2,284

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).

  • I compose a name for a database table from which I want to select dynamically (result in global field g_tabname will be the value ‘/1CPMB/LKWSK4P8F’)

          CONCATENATE '/1CPMB/LKWS' g_field1 g_field2 INTO g_tabname.


  • I declare and assign a data object to use for data transfer when selecting from the database

      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.

  • Now, after I have retrieved the data I want to find out at which position in example the field with name ENTITY is. Because, as I want to use this ABAP construction for different database tables, the position of the field ENTITY will be at a different place each time. So basically I am looking for something like this:

     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.

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,207

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.

  • If not, then why do you want to get the position of the field?
  • If yes, then you can use the following construct to get the value of ENTITY w/o bothering about the position

ASSIGN COMPONENT 'ENTITY' OF STRUCTURE <struc> TO <comp>.

BR,

Suhas

13 REPLIES 13
Read only

FredericGirod
Active Contributor
0 Likes
2,207

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

Read only

0 Likes
2,207

This message was moderated.

Read only

0 Likes
2,207

This message was moderated.

Read only

0 Likes
2,207

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

Read only

former_member184497
Participant
0 Likes
2,207

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.

Read only

0 Likes
2,207

Hi Arunkumar,

Thanks for your help. I didn't know about this FM, it works fine.

Wendy

Read only

former_member206575
Participant
0 Likes
2,207

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.

Read only

0 Likes
2,207

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.

Read only

0 Likes
2,207

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

Read only

0 Likes
2,207

Hi Raymond,

This is also a very good idea.

I am picking up all the methods of this class quickly.

Thank you,

Wendy

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,208

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.

  • If not, then why do you want to get the position of the field?
  • If yes, then you can use the following construct to get the value of ENTITY w/o bothering about the position

ASSIGN COMPONENT 'ENTITY' OF STRUCTURE <struc> TO <comp>.

BR,

Suhas

Read only

0 Likes
2,207

Hi Suhas!

Your answer was just PERFECT!

  • Yes, indeed this was exactly what I intended to do: get position (for each table ENTITY is at different position) and read the content right after.
  • Unbelievable, I just had to add the field name, I was nearly there. Together with Frédéric who also mentioned the ASSIGN COMPONENT with fieldname, I find this the best solutions.

Thank you for helping, it is a great experience to be helped.

Wendy

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,207

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