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

Finding a field in a structure

raja_ksravankumarreddy
Product and Topic Expert
Product and Topic Expert
0 Likes
2,416

In the following Subroutine signature, P_SOURCE and P_DEST strutures can be any type.

FORM uc_move USING P_SOURCE TYPE ANY

CHANGING P_DEST TYPE ANY.

My requirement is to find if some field exists in P_DEST .

Example, I want to check if ROUTE is part of P_DEST structure.

Please help!!

1 ACCEPTED SOLUTION
Read only

franois_henrotte
Active Contributor
0 Likes
1,803

you can do it like this:

FIELD-SYMBOLS: <fld> TYPE ANY.

ASSIGN COMPONENT 'ROUTE' OF STRUCTURE 'PDEST' TO <fld>.

IF sy-subrc = 0.

  • your field is present

ENDIF.

you can check it later in the code with

IF <fld> IS ASSIGNED.

  • your field is present

ENDIF.

8 REPLIES 8
Read only

Former Member
0 Likes
1,803

Hello

Assign your p_dest to a field symbol..

Use "assign component 'ROUTE' of structure ..." statement..

if field symbol is assigned it means field exists else it implies field does not exist...

Regards,

Arun

Read only

franois_henrotte
Active Contributor
0 Likes
1,804

you can do it like this:

FIELD-SYMBOLS: <fld> TYPE ANY.

ASSIGN COMPONENT 'ROUTE' OF STRUCTURE 'PDEST' TO <fld>.

IF sy-subrc = 0.

  • your field is present

ENDIF.

you can check it later in the code with

IF <fld> IS ASSIGNED.

  • your field is present

ENDIF.

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
1,803

HI,

use the class.

CL_ABAP_TYPEDESCR and the method describe_by_data.

Here is the example.


DATA: lr_rtti_struc TYPE REF TO cl_abap_structdescr.
DATA: lt_comp       TYPE cl_abap_structdescr=>component_table.
DATA: ls_comp       LIKE LINE OF lt_comp.

    lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( data_incoming ).
    lt_comp = lr_rtti_struc->get_components( ).

READ TABLE lt_comp INTO ls_comp with key name = 'FIELD1'.
if sy-subrc = 0.
 " field exists
endif.

Regards,

Sesh

Read only

varma_narayana
Active Contributor
0 Likes
1,803

Hi Raja..

This is the way to find whether a COMPONENT exists using the FIELD SYMBOLS.

FORM uc_move USING P_SOURCE TYPE ANY

CHANGING P_DEST TYPE ANY.

FIELD-SYMBOLS: <FS_COMP> TYPE ANY.

ASSIGN-COMPONENT ROUTE OF STRUCTURE P_DEST TO <FS>.

if SY-SUBRC = 0.

<<<<COMPONENT EXISTS>>>

ENDIF.

<b>

reward if Helpful.</b>

Read only

former_member189059
Active Contributor
0 Likes
1,803

Hi,

Check if this code helps

It can find the field names of any table / structure

data: begin of myitab,
  number type i,
  name(30),
 end of myitab.


data: INT_TABNAME(40).
INT_TABNAME = 'MYITAB'.

* Insert internal table fieldname into itab
DATA : IRSTRUCINFO LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE.
CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    PROGRAM    = sy-repid
    FIELDNAME  = INT_TABNAME
  TABLES
    COMPONENTS = IRSTRUCINFO.

loop at IRSTRUCINFO.
  write:/ IRSTRUCINFO-COMPNAME.
endloop.

Read only

Former Member
0 Likes
1,803

Hi,

You can use Function modules DDIF_NAMETAB_GET or RPY_TABLE_READ

and get the list of fields in that structure,

Then check if the field you want is in it or not.

Regards,

Samson Rodrigues.

Read only

0 Likes
1,803

Thanks all for the help.

The problem is solved.

Read only

raja_ksravankumarreddy
Product and Topic Expert
Product and Topic Expert
0 Likes
1,803

François Henrotte reply has solved my problem