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.
Introduction
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.
- Instantiate the object for this new class and pass to the parameter IO_CALC_FIELD_HANDLER of the method call CL_SALV_GUI_TABLE_IDA=>CREATE
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( ) ).
Output
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?
Hi,
I’m follower of your posts on ABAP. I have a doubt like, I’m clear with the above requirement and output shown. But coding part is not fully given I believe like declarations of some internal tables (its_calc_field_name).
Can you please provide me the full code which can be executed?
Hello Venky,
Those details are part of the interface if_salv_ida_calc_field_handler.
See the template in the first article of the series .. http://zevolving.com/2016/07/salv-ida-integrated-data-access-introduction/
Thanks
Naimesh Patel
Ok, got it. So they are all parameters from that interface method right?
And “is_data_base_line” contains the data already selected from the select query?
Method CALCULATE_LINE gets called for each row in the page. So the is_data_base_line would contain the row which is being processed.
Thanks.
Hi Naimesh, great article!
The example originates from having a standard database (T100) as a starting point and adding custom fields later on.
What if the output is very customized? Can we refer on an internal table instead?
Hi Akira,
You can create the CDS view to have most of the calculation performed at the DB level and try to call that using the SALV IDA. I haven’t yet covered the CDS view but stay tuned, I will post them soon.
Thanks.