Lets check out how we can add the additional fields on the ALV output and also populate the value for those added columns using Calculated fields.
By the time, you must be thinking how can I add additional fields in the output which are not in the table. Without this, the usage would be limited. To achieve this IDA framework has a provision to provide the extra field handler. This is called as Calculated Field. Thus the handler is the Calculated Field Handler.
If you have missed the Introduction to what is SAL IDA, Refer to article SALV IDA Introduction.
Calculated Field Handler
Here are the steps you would need to take in order to add the new fields on the ALV:
- Define a class which implements the interface IF_SALV_IDA_CALC_FIELD_HANDLER. The Interface has different methods which are being called during the SALV processing.
- Method GET_CALC_FIELD_STRUCTURE – Here you send the RTTS object for which contains the new fields. Create a type with all required additional fields. Use the RTTS to get the object and pass it back.
- Method CALCULATE_LINE – In this method, you populate the result based on the requested fields.
Move data from flat structure to DB structure so required columns can be easily accessible
Calculate the new fields
Pass the calculated fields back to the entire row
- Method GET_REQUESTED_FIELDS – Here you supply the fields which are used into your calculation to calculate the field
Provide the “source” fields names.
- Method START_PAGE – This method provides the access to the DB data for the current page. If the data is not required, processing can be cancelled as well
- Method END_PAGE – Method to know the page is ended. This method can be used for data cleanup.
Program Code Lines
Implementation of the local helper handler class
CLASS lcl_t100_new_fields DEFINITION. PUBLIC SECTION. INTERFACES: if_salv_ida_calc_field_handler. PRIVATE SECTION. TYPES: BEGIN OF ty_t100_new_fields, count_ph TYPE i, ucase_text TYPE t100-text, END OF ty_t100_new_fields. ENDCLASS. CLASS lcl_t100_new_fields IMPLEMENTATION. METHOD if_salv_ida_calc_field_handler~get_calc_field_structure. "RTTS ro_calc_field_structure ?= cl_abap_typedescr=>describe_by_name( 'TY_T100_NEW_FIELDS' ). ENDMETHOD. * METHOD if_salv_ida_calc_field_handler~get_requested_fields. "define the source fields used for calculation of this new fields DATA lv_field_name TYPE fieldname. READ TABLE its_calc_field_name TRANSPORTING NO FIELDS WITH KEY table_line = 'UCASE_TEXT'. IF sy-subrc EQ 0. lv_field_name = 'TEXT'. INSERT lv_field_name INTO TABLE rts_db_field_name. ENDIF. ENDMETHOD. * METHOD if_salv_ida_calc_field_handler~calculate_line. * DATA: ls_t100_new_fields TYPE ty_t100_new_fields. DATA: ls_t100 TYPE t100. * Transfer from "flat" to structured ls_t100 = is_data_base_line. * new field ls_t100_new_fields-ucase_text = to_upper( ls_t100-text ). FIND ALL OCCURRENCES OF '&' IN ls_t100-text MATCH COUNT ls_t100_new_fields-count_ph. * Send it back es_calculated_fields = ls_t100_new_fields. ENDMETHOD. METHOD if_salv_ida_calc_field_handler~start_page. * get access to the data for the given page ENDMETHOD. METHOD if_salv_ida_calc_field_handler~end_page. * Use for cleanup purpose ENDMETHOD. ENDCLASS.
Code lines to pass the instance of the object while calling the CREATE method
o_salv_ida = cl_salv_gui_table_ida=>create( iv_table_name = 'T100' io_calc_field_handler = NEW lcl_t100_new_fields( ) ).
The program generate this output now, Notice two additional fields on the output.
Method GET_REQUESTED_FIELDS purpose
Remember the IDA dynamically selects the data based on the “view port”. So, it might be possible that the required field for calculation is hidden via variant or not present in the current view port. But by providing it here, you make sure that the field is available in the method CALCULATE_LINE
As soon as you remove the field from the visible field in the Manage layout and the wooh the data in the new columns are gone.
This is the reason, you must always pass the required fields in the method GET_REQUESTED_FIELDS. Got it?