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

Compare two different WA.

0 Likes
4,272

Hi Team, I need a help on a topic in ABAP where I need to compare all fields of 2 different Work Areas and for any discrepancy in a particular field.

I need to highlight in a different color.

It would not be advisable to compare each field of both Work Areas as numbers of fields are more than 40. Please suggest. 
I have attached a sample over here. Thanks in advance.

1 ACCEPTED SOLUTION
Read only

gaurab_banerji
Active Participant
0 Likes
3,109

I just made this report for you.....

REPORT ztest_gb1.

DATA: wa_1 TYPE t001,

       wa_2 TYPE t001.

DATA: rf_descr_ref TYPE REF TO cl_abap_typedescr.

FIELD-SYMBOLS: <fs1> TYPE any,

                <fs2> TYPE any.

START-OF-SELECTION.

   SELECT * FROM t001 INTO wa_1

   UP TO 1 ROWS.

   ENDSELECT.

   wa_2 = wa_1.

*forcefully changing variable to test

   wa_2-stceg = '1'.

   DO.

* Assign every field of this structure subsequently to the untyped

*fieldsymbol.

*    BREAK-POINT.

     ASSIGN COMPONENT sy-index OF STRUCTURE wa_1 TO <fs1>.

     IF sy-subrc NE 0.

       EXIT.

     ENDIF.

     ASSIGN COMPONENT sy-index OF STRUCTURE wa_2 TO <fs2>.

     IF sy-subrc NE 0.

       EXIT.

     ENDIF.

     IF <fs1> NE <fs2>.

*      mismatch found.

       CALL METHOD cl_abap_typedescr=>describe_by_data

         EXPORTING

           p_data      = <fs1>

         RECEIVING

           p_descr_ref = rf_descr_ref.

       WRITE: / sy-index, rf_descr_ref->type_kind,

       rf_descr_ref->absolute_name.

     ENDIF.

   ENDDO.


It should give an output like


        14  C

\TYPE=STCEG




Hi Team, I need a help on a topic in ABAP where I need to compare all fields of 2 different Work Areas and for any discrepancy in a particular field.

I need to highlight in a different color.

It would not be advisable to compare each field of both Work Areas as numbers of fields are more than 40. Please suggest. 
I have attached a sample over here. Thanks in advance.

10 REPLIES 10
Read only

Former Member
0 Likes
3,109

Abishek,

Instead of comparing all the fields, all the time just check if wa1 = wa2 (assuming the structure of wa1 and wa2 are the same).

if they are not the same then create a subroutine where you check all the 40 fields. This way you will only end up comparing only if the work areas are not same.

Thanks,

Vikram.M

Read only

0 Likes
3,109

This message was moderated.

Read only

0 Likes
3,109

Alpesh, I am wondering why dont you want to compare the work areas directly but assigning the components of WA to FS and then comparing the FS?

Read only

0 Likes
3,109

Might be a solution, if you got a dynamic structure. But there is a way more to do than just a assign.

I'm not sure, if this could be a practical way to compare something

~Florian

Read only

0 Likes
3,109

vikram, would you be able to find the difference cells if you compare the whole structure? That was the main requirement as you may see.

Alpesh's answer is correct if the structures are the same (meaning both structures belong to the same table). If they were not the same, but hold in particular some common fields, then one need to use some RTTI methods. But I suppose this is not the case.

Abhishek, please close the thread if your requirement is answered.

Read only

0 Likes
3,109

Hello Everyone..
Thanks for your reply but just to update that structure of both work areas are same.
I just need to compare each field of both WA.

e.g. wa1-bukrs = 100 and wa2-bukrs = 200

     then I want to highlight wa2-bukrs in my final output of ALV.

and this comparison will applicable for more than 40 fields.

So need your guidance.

Read only

0 Likes
3,109

you already had the solution written here, by some reason it is hidden now.

DO.

ASSIGN COMPONENT sy-index of strcutre wa_1 to <lv_field1>

if ( sy-subrc <> 0 ).

  exit

endif.

ASSIGN COMPONENT sy-index of strcutre wa_2 to <lv_field2>

if ( sy-subrc <> 0 ).

  exit

endif.

if <lv_field1> <> <lv_field2>.

* this field is different between the structures - do your own logic (e.g. find sy-index's position of the field in fieldcatalog, if it was not changed and highlight the field, or whatever you would like to do). If you need to find the real name of the fieldname in structrue, use e.g. FM DDIF_FIELDINFO_GET, where you can find sy-index's position and the name of the column.

endif.

ENDDO.

Read only

0 Likes
3,109

This message was moderated.

Read only

gaurab_banerji
Active Participant
0 Likes
3,110

I just made this report for you.....

REPORT ztest_gb1.

DATA: wa_1 TYPE t001,

       wa_2 TYPE t001.

DATA: rf_descr_ref TYPE REF TO cl_abap_typedescr.

FIELD-SYMBOLS: <fs1> TYPE any,

                <fs2> TYPE any.

START-OF-SELECTION.

   SELECT * FROM t001 INTO wa_1

   UP TO 1 ROWS.

   ENDSELECT.

   wa_2 = wa_1.

*forcefully changing variable to test

   wa_2-stceg = '1'.

   DO.

* Assign every field of this structure subsequently to the untyped

*fieldsymbol.

*    BREAK-POINT.

     ASSIGN COMPONENT sy-index OF STRUCTURE wa_1 TO <fs1>.

     IF sy-subrc NE 0.

       EXIT.

     ENDIF.

     ASSIGN COMPONENT sy-index OF STRUCTURE wa_2 TO <fs2>.

     IF sy-subrc NE 0.

       EXIT.

     ENDIF.

     IF <fs1> NE <fs2>.

*      mismatch found.

       CALL METHOD cl_abap_typedescr=>describe_by_data

         EXPORTING

           p_data      = <fs1>

         RECEIVING

           p_descr_ref = rf_descr_ref.

       WRITE: / sy-index, rf_descr_ref->type_kind,

       rf_descr_ref->absolute_name.

     ENDIF.

   ENDDO.


It should give an output like


        14  C

\TYPE=STCEG




Read only

0 Likes
3,109

THANKS A LOT guys..you made it easy for me.
Closing this thread with smile