04-14-2016 1:25 PM
Hi,
I often have the requirement to copy an internal table into another and add/change some of the target's table members. E.g.:
REPORT zsdsm_mf_test_corresponding.
INCLUDE <icons>.
TYPES t_bapicond TYPE bapicond.
TYPES:
BEGIN OF t_bapicond2.
TYPES: icon TYPE icon_d.
INCLUDE TYPE bapicond.
TYPES: END OF t_bapicond2.
DATA:
lt_source TYPE STANDARD TABLE OF t_bapicond
,lt_target TYPE STANDARD TABLE OF t_bapicond2
.
START-OF-SELECTION.
BREAK-POINT.
"Sample Data
lt_source = VALUE #(
LET o_rnd = cl_abap_random_int=>create(
seed = CONV i( sy-uzeit ) min = 1 max = 599 ) IN
( itm_number = lines( lt_source ) * 10
applicatio = 'V' cond_value = o_rnd->get_next( ) )
( itm_number = lines( lt_source ) * 10
applicatio = 'V' cond_value = o_rnd->get_next( ) condisacti = abap_true )
( itm_number = lines( lt_source ) * 10
applicatio = 'V' cond_value = o_rnd->get_next( ) condisacti = abap_true )
( itm_number = lines( lt_source ) * 10
applicatio = 'V' cond_value = o_rnd->get_next( ) )
( itm_number = lines( lt_source ) * 10
applicatio = 'V' cond_value = o_rnd->get_next( ) )
).
"OK
lt_target = CORRESPONDING #( lt_source ).
"OK
lt_target = VALUE #(
FOR wa IN lt_source (
CORRESPONDING #( wa )
)
).
"All inactive conditions should get a yellow icon! <<<
"This works pretty well usign "old" coding...
CLEAR: lt_target[].
LOOP AT lt_source ASSIGNING FIELD-SYMBOL(<fs_src>).
APPEND CORRESPONDING #( <fs_src> ) TO lt_target
REFERENCE INTO DATA(ref_tgt).
ref_tgt->icon = COND #(
WHEN <fs_src>-condisacti = abap_true
THEN icon_led_yellow
ELSE icon_space ).
ENDLOOP.
"How to do this way? Using FOR expression and CORRESPONDING
* lt_target = VALUE #(
* FOR wa IN lt_source (
* CORRESPONDING #( wa )
* ICON = COND icon_d( WHEN wa-condisacti = abap_true THEN icon_led_yellow ELSE icon_space )
* )
* ).
BREAK-POINT.
Starting from line 54 I tried to replace the coding from lines 44 up to 51, but I had no luck so far.
Any hints?
Thanks,
Michael
04-14-2016 3:00 PM
After some trying around, I got it!
lt_target = VALUE #(
FOR wa IN lt_source
( CORRESPONDING #(
BASE ( value #( icon = COND icon_d( WHEN wa-condisacti = abap_true
THEN icon_led_yellow
ELSE icon_space ) ) )
wa ) )
).
Message was edited by: Armin Junge Formatting
04-14-2016 1:36 PM
Hi Michael,
Something like that:
DATA(new) = REDUCE alv_t( INIT out = VALUE alv_t( )
FOR i IN itab
NEXT out = COND alv_t(
WHEN i-status = 'A'
THEN VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
icon = c_icon_green ) ) )
WHEN i-status = 'R'
THEN VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
icon = c_icon_red ) ) )
ELSE VALUE #( BASE out ( VALUE alv_y( BASE CORRESPONDING alv_y( i )
icon = c_icon_off ) ) ) ) ).
Where: alv_t is TYPE STANDARD of alv_y and the itab is internal table with values.
Regards,
Raphael Pacheco.
04-14-2016 3:00 PM
After some trying around, I got it!
lt_target = VALUE #(
FOR wa IN lt_source
( CORRESPONDING #(
BASE ( value #( icon = COND icon_d( WHEN wa-condisacti = abap_true
THEN icon_led_yellow
ELSE icon_space ) ) )
wa ) )
).
Message was edited by: Armin Junge Formatting
04-14-2016 4:14 PM
Super
Generally i add in-line comments when using a complex expression like this, so that the developer who's gonna maintain this code knows what is happening
04-14-2016 4:57 PM
How about a small ABAP Unit test with 1 positive and 1 negative condition ?
04-15-2016 6:54 AM
04-15-2016 7:57 AM
Naimesh Patel wrote:
How about a small ABAP Unit test with 1 positive and 1 negative condition ?
What would the AUnit test - the FOR function?
If the method is modular enough (e.g., BUILD_ALV_OUTPUT_TABLE), then i can check the contents of the table to see how many ICON types i have in the output table.
But how do i test for a negative case? If i wrote the code correctly, then there's no negative testcase.
BR,
Suhas
04-15-2016 7:58 AM
04-26-2016 1:40 PM