Many a times we have to display a traffic light in ALV report. We can do that easily using "Exceptions" parameter provided by SAP. But it has one drawback, we cannot change the column header to something meaningful. It always set as "Exception" in case of ALV Grid and an icon in case of ALV List.
REPORT ztest_sourav_traffic_lights.TYPE-POOLS: slis,icon.TYPES: BEGIN OF x_sflight,carrid TYPE s_carr_id,connid TYPE s_conn_id,fldate TYPE s_date,price TYPE s_price,currency TYPE s_currcode,seatsmax TYPE s_seatsmax,seatsocc TYPE s_seatsocc,END OF x_sflight.TYPES:BEGIN OF x_final,icon TYPE char4.INCLUDE TYPE x_sflight.TYPES: END OF x_final.
DATA: i_sflight TYPE STANDARD TABLE OF x_sflight INITIAL SIZE 0,i_final TYPE STANDARD TABLE OF x_final INITIAL SIZE 0,i_fieldcat TYPE slis_t_fieldcat_alv,wa_layout TYPE slis_layout_alv.
CONSTANTS: program TYPE syrepid VALUE sy-repid.
START-OF-SELECTION.SELECT carrid
connid
fldate
price
currency
seatsmax
seatsocc
FROM sflight
INTO TABLE i_sflight.IF sy-subrc <> 0.MESSAGE i001(00) WITH 'No data found!'.LEAVE LIST-PROCESSING.ENDIF.
END-OF-SELECTION.
PERFORM populate_records.
PERFORM display_records.
FORM populate_records .
DATA:wa_sflight TYPE x_sflight,wa_final TYPE x_final,temp TYPE p DECIMALS 2.
LOOP AT i_sflight INTO wa_sflight.CLEAR temp.
temp = ( wa_sflight-seatsocc / wa_sflight-seatsmax ) .
IF temp > '0.9'.
wa_final-icon = icon_green_light.ELSEIF temp < '0.2'.
wa_final-icon = icon_red_light.ELSE.
wa_final-icon = icon_yellow_light.ENDIF.
wa_final-carrid = wa_sflight-carrid.wa_final-connid = wa_sflight-connid.wa_final-fldate = wa_sflight-fldate.wa_final-price = wa_sflight-price.wa_final-currency = wa_sflight-currency.wa_final-seatsmax = wa_sflight-seatsmax.wa_final-seatsocc = wa_sflight-seatsocc.
APPEND wa_final TO i_final.CLEAR wa_final.ENDLOOP.FREE i_sflight.ENDFORM.
FORM display_records .
PERFORM populate_field_catalog.
PERFORM populate_layout.
PERFORM trigger_alv.
ENDFORM.
FORM populate_field_catalog .DATA:wa_fieldcat TYPE slis_fieldcat_alv.DEFINE fcat_merge.
wa_fieldcat-col_pos = &1.wa_fieldcat-fieldname = &2.if wa_fieldcat-fieldname = 'ICON'.wa_fieldcat-icon = 'X'.
wa_fieldcat-seltext_m = 'Occupency Status'.
wa_fieldcat-inttype = 'C'.wa_fieldcat-outputlen = '4'.endif.
wa_fieldcat-ref_fieldname = &3.wa_fieldcat-ref_tabname = &4.
if wa_fieldcat-fieldname = 'PRICE'.wa_fieldcat-cfieldname = 'CURRENCY'.wa_fieldcat-ctabname = 'I_FINAL'.endif.
append wa_fieldcat to i_fieldcat.clear wa_fieldcat.END-OF-DEFINITION.
fcat_merge: '1' 'ICON' '' '',
'2' 'CARRID' 'CARRID' 'SFLIGHT',
'3' 'CONNID' 'CONNID' 'SFLIGHT',
'4' 'FLDATE' 'FLDATE' 'SFLIGHT',
'5' 'PRICE' 'PRICE' 'SFLIGHT',
'6' 'CURRENCY' 'CURRENCY' 'SFLIGHT',
'7' 'SEATSMAX' 'SEATSMAX' 'SFLIGHT',
'8' 'SEATSOCC' 'SEATSOCC' 'SFLIGHT'.
ENDFORM.
FORM populate_layout.wa_layout-zebra = 'X'.wa_layout-colwidth_optimize = 'X'.ENDFORM.
FORM trigger_alv .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = program
is_layout = wa_layout
it_fieldcat = i_fieldcat
TABLES
t_outtab = i_final
EXCEPTIONS
program_error = 1OTHERS = 2.IF sy-subrc <> 0.MESSAGE i001(00) WITH 'Error while displaing ALV'.LEAVE LIST-PROCESSING.ENDIF.ENDFORM.
Social Plugin