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

dynamic workarea-fields

Former Member
0 Likes
1,308

Hello everyone,

this is what I want to do: I have a work-area with several fields, let's say "field1", "field2", "field3". I do have another workarea with the same structure, but diferent content. I want ty run through both work-areas dynamically and compare the values of both work-areas.

Something like:

if wa_nr1-<dynfield> eq wa_nr2-<dynfield>.

  "do some code...

endif.


1 ACCEPTED SOLUTION
Read only

ramakrishnappa
Active Contributor
0 Likes
1,155

Hi Markus,

You can achieve your requirement as below


  DATA ls_wa_1 TYPE pa0001. "work area 1

  DATA ls_wa_2 TYPE  pa0001. " work area 2

  DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

  DATA lt_fields TYPE cl_abap_structdescr=>component_table.

  DATA ls_fields LIKE LINE OF lt_fields.

  FIELD-SYMBOLS: <fs_val_1> TYPE any,

                 <fs_val_2> TYPE any.

    

"get structure descr reference

  lo_structdescr ?= cl_abap_structdescr=>describe_by_data( ls_wa_1 ).

    

  lt_fields = lo_structdescr->get_components( ).

  LOOP AT lt_fields INTO ls_fields.

    ASSIGN COMPONENT ls_fields-name OF STRUCTURE ls_wa_1 TO <fs_val_1>.

    ASSIGN COMPONENT ls_fields-name OF STRUCTURE ls_wa_2 TO <fs_val_2>.

    IF <fs_val_1> EQ <fs_val_2>.

      " do some thing

    ENDIF.

  ENDLOOP.

Hope this helps you.

Regards,

Rama

7 REPLIES 7
Read only

Former Member
0 Likes
1,155

I do have already assigned the component structure and hold the fieldnames in an array. I am only lacking the last steps to finish.

Read only

adrian_mejido
Contributor
0 Likes
1,155

Hi Markus,

Look at this example code:

DATA: l_index type sy-index.

CALL FUNCTION 'GET_COMPONENT_LIST'

    EXPORTING

      program    = sy-repid

      fieldname  =   wa_nr1

    TABLES

      components = lt_nr1.

CALL FUNCTION 'GET_COMPONENT_LIST'

    EXPORTING

      program    = sy-repid

      fieldname  =   wa_nr2

    TABLES

      components = lt_nr2.

LOOP  AT lt_nr1 ASSIGNING <fs_nrq1>.

l_index = sy-tabix.

READ TABLE lt_nr2 ASSIGNING <fs_nr2> INDEX l_index.

IF sy-subrc eq 0.


if <fs_nr1> eq <fs_nr2>.

  " Code

endif.

ENDIF.

ENDLOOP.

Read only

himanshu_gupta2
Participant
0 Likes
1,155

first get all the fields of the sturcutre through FM"CATSXT_GET_DDIC_FIELDINFO"

then

create a field symbol

loop at li_field into ls_field.

<fs_field1> = 'StructureName1-ls_field-nam3'

<fs_field2> = 'StructureName2-ls_field-name'

if <fs_field1> eq <fs_field2>

do some code

endif.

endloop

Read only

ramakrishnappa
Active Contributor
0 Likes
1,156

Hi Markus,

You can achieve your requirement as below


  DATA ls_wa_1 TYPE pa0001. "work area 1

  DATA ls_wa_2 TYPE  pa0001. " work area 2

  DATA lo_structdescr TYPE REF TO cl_abap_structdescr.

  DATA lt_fields TYPE cl_abap_structdescr=>component_table.

  DATA ls_fields LIKE LINE OF lt_fields.

  FIELD-SYMBOLS: <fs_val_1> TYPE any,

                 <fs_val_2> TYPE any.

    

"get structure descr reference

  lo_structdescr ?= cl_abap_structdescr=>describe_by_data( ls_wa_1 ).

    

  lt_fields = lo_structdescr->get_components( ).

  LOOP AT lt_fields INTO ls_fields.

    ASSIGN COMPONENT ls_fields-name OF STRUCTURE ls_wa_1 TO <fs_val_1>.

    ASSIGN COMPONENT ls_fields-name OF STRUCTURE ls_wa_2 TO <fs_val_2>.

    IF <fs_val_1> EQ <fs_val_2>.

      " do some thing

    ENDIF.

  ENDLOOP.

Hope this helps you.

Regards,

Rama

Read only

0 Likes
1,155

Thanks for your help!

Read only

0 Likes
1,155

You are welcome

Read only

SharathYaralkattimath
Contributor
0 Likes
1,155

Try this & see...        

DATA:

         lo_struct1   TYPE REF TO cl_abap_structdescr,
         li_comp1     TYPE cl_abap_structdescr=>component_table,
         li_comp_line1 LIKE LINE OF li_comp,        

         lo_struct2   TYPE REF TO cl_abap_structdescr,
         li_comp2     TYPE cl_abap_structdescr=>component_table,
         li_comp_line2 LIKE LINE OF li_comp,

         wa_nr1 type STRUCTURE1,

         wa_nr2 type STRUCTURE2.

FIELD-SYMBOLS : <fs_1> type any,

                                <fs_2> type any.

lo_struct1 ?= cl_abap_typedescr=>describe_by_name( 'STRUCTURE1').
li_comp1  = lo_struct->get_components( ).

lo_struct2 ?= cl_abap_typedescr=>describe_by_name( 'STRUCTURE2').
li_comp2  = lo_struct->get_components( ).

LOOP AT li_comp1 INTO li_comp_line1.

ASSIGN COMPONENT li_comp_line1-name OF STRUCTURE wa_nr1TO <fs_1>.

READ TABLE li_comp2 INTO li_comp_line2 WITH KEY name = li_comp_line1-name.

IF sy-subrc EQ 0.

     ASSIGN COMPONENT li_comp_line2-name OF STRUCTURE wa_nr2TO <fs_2>.

     IF <fs_1> = <fs_2>.

          "do some code...

     ENDIF.

ENDIF.

ENDLOOP.

Thanks,

Sharath