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

Field availability in structure

Former Member
0 Likes
680

Hi experts. Problem is following:

I have an export parameter es_data in my method with type 'any'. Is there a possibility to check whether this structure contains some field. For example I do need to know whether es_data contains field 'userName' . If it contains I want to fill it. Guess  that it could be done with field-symbols, but dunno how.

Thanks in advance!

1 ACCEPTED SOLUTION
Read only

SharathYaralkattimath
Contributor
0 Likes
642

Using field-symbols,

FIELD-SYMBOLS:  <comp>  TYPE any.

DATA   name TYPE string,

name = 'userName'.

ASSIGN COMPONENT name OF STRUCTURE es_data TO <comp>.

IF sy-subrc EQ 0.

     "Field exists.

ENDIF.

Use RTTS classes & methods,

DATA: tab TYPE REF TO cl_abap_structdescr,

comp_tab   TYPE cl_abap_structdescr=>component_table.

tab?= cl_abap_typedescr=>describe_by_name( es_data ).

comp_tab =  tab->get_components( ).


READ TABLE comp_tab WITH KEY name = 'userName' TRANSPORTING NO FIELDS.

IF sy-subrc EQ 0.

     "Field exists.

ENDIF.

4 REPLIES 4
Read only

former_member201285
Active Participant
0 Likes
642

    DATA: info TYPE dd_x031l_table,
          descr_ref TYPE REF TO cl_abap_typedescr.

  descr_ref = cl_abap_typedescr=>describe_by_data( es_data ).

  info = descr_ref->get_ddic_object( ).  

Table info will contain all fields.

Regards,

Ulrich

Read only

SharathYaralkattimath
Contributor
0 Likes
643

Using field-symbols,

FIELD-SYMBOLS:  <comp>  TYPE any.

DATA   name TYPE string,

name = 'userName'.

ASSIGN COMPONENT name OF STRUCTURE es_data TO <comp>.

IF sy-subrc EQ 0.

     "Field exists.

ENDIF.

Use RTTS classes & methods,

DATA: tab TYPE REF TO cl_abap_structdescr,

comp_tab   TYPE cl_abap_structdescr=>component_table.

tab?= cl_abap_typedescr=>describe_by_name( es_data ).

comp_tab =  tab->get_components( ).


READ TABLE comp_tab WITH KEY name = 'userName' TRANSPORTING NO FIELDS.

IF sy-subrc EQ 0.

     "Field exists.

ENDIF.

Read only

Former Member
0 Likes
642

DATA:  lr_structdescr TYPE REF TO cl_abap_structdescr.
DATA:  lt_components TYPE abap_compdescr,
           ls_component like line of lt_components.
*

FIELD-SYMBOLS: <fs_line> TYPE any.

*

ASSIGN es_data TO <fs_line>.
IF sy-subrc EQ 0

     lr_structdescr ?= cl_abap_typedescr=>describe_by_data( <fs_line> ).     

     lt_components = lr_strucdescr->get_components( ). "get field names

     READ TABLE lt_components INTO ls_component WITH KEY name = 'userName'.

     IF sy-subrc EQ 0.

* Do your assignment here

     ENDIF.
ENDIF.

Read only

Former Member
0 Likes
642

Thanks guys)