ABAP Lesser Known Heroes Series – TYPE RANGE OF : Part 3
Part 1: https://blogs.sap.com/2021/07/09/abap-lesser-known-heroes-series-group-column-part-1/
Part 2: https://blogs.sap.com/2021/07/10/abap-lesser-known-heroes-series-value-operator-part-2/
It’s been some time since I wrote the first two blogs in the series and I think it is high time to come up with more since there are many more unearthed gems in the ABAP world.
Here I am going to explain about the keyword TYPE RANGE OF. Maybe you may wonder , how it is going to be an unsung hero since most of you will be aware of this statement and its usage. The most common usage of TYPE RANGE OF is to get the SELECT-OPTIONS values to the internal table format or you want an internal table which mimics the select-options which can later be catered to the where clause of a ‘SELECT’ statement.
But have you ever thought of using TYPE RANGE OF inside the TYPES statement ?
One of the biggest confusions that come to our mind when using PARAMETERS/SELECT-OPTIONS fields is how to make use/represent them in the object oriented world. For the select options this is far more important since it deals with multiple data. Here let us see how ‘TYPE RANGE OF’ could make use of select-opions values in a more neater way. I got inspiration about this idea from Kerem Koseoglu . https://people.sap.com/kerem.koseoglu
and from his book, ‘Design Patterns in ABAP Objects’
When you have multiple select-options in a program it is neater to embed it into one structure like below and later to give the same structure as the import parameter for a constructor method. So when the object is created for a class we already have all the selection values which we need in the form of global variables. The class could also be created as a global class with this concept and could cater to as a model class in an MVC architecture.
"---§ Type Declarations TYPES: BEGIN OF t_sel_options, saldoc TYPE RANGE OF vbak-vbeln, " Sales Documents sold TYPE RANGE OF vbak-kunnr, " Sold To Party mat TYPE RANGE OF vbap-matnr, " Material salorg TYPE RANGE OF vbak-vkorg, " Sales Organization END OF t_sel_options. "---§ Global Variables DATA: gv_vbeln TYPE vbak-vbeln, gv_sold TYPE vbak-kunnr, gv_mat TYPE vbap-matnr, gv_salorg TYPE vbak-vkorg. CLASS : lcl_sales_report DEFINITION DEFERRED . "---§ Global Reference Variables DATA: go_sales_report TYPE REF TO lcl_sales_report . DATA: gs_sel_opt TYPE t_sel_options. SELECT-OPTIONS: s_saldoc FOR gv_vbeln , s_sold FOR gv_sold , s_mat FOR gv_mat , s_salorg FOR gv_salorg . "---§ Class Definition CLASS lcl_sales_report DEFINITION . PUBLIC SECTION . DATA: mt_sales_docs TYPE RANGE OF vbak-vbeln, " Sales Documents mt_sold_to TYPE RANGE OF vbak-kunnr, " Sold To Party mt_materials TYPE RANGE OF vbap-matnr, " Material mt_salesorgs TYPE RANGE OF vbak-vkorg . " Sales Organizations METHODS: constructor IMPORTING is_sel_opt TYPE t_sel_options . ENDCLASS. "---§ Class Implementation CLASS lcl_sales_report IMPLEMENTATION . METHOD constructor . mt_sales_docs = is_sel_opt-saldoc . mt_sold_to = is_sel_opt-sold . mt_materials = is_sel_opt-mat . mt_salesorgs = is_sel_opt-salorg . ENDMETHOD . ENDCLASS. START-OF-SELECTION . gs_sel_opt-saldoc = s_saldoc . gs_sel_opt-sold = s_sold . gs_sel_opt-mat = s_mat . gs_sel_opt-salorg = s_salorg . CREATE OBJECT go_sales_report EXPORTING is_sel_opt = gs_sel_opt .
This way we can keep the importing parameter of the constructor neat. Even if there are more than 10 select options the importing parameter is not going to be complicated. Inside the constructor we can assign the importing values into a global variable.
Footnote: There is also a class called CL_SALV_RANGE_TAB_COLLECTOR which functions in a similar way with dynamic input parameters. One can add select options values to the input parameter and returns with a collected internal table with the select options values.
DATA(lo_sel) = NEW cl_salv_range_tab_collector( ). lo_sel->add_ranges_for_name( iv_name = 'MATNR' it_ranges = s_matnr ) . lo_sel->add_ranges_for_name( iv_name = 'MATKL' it_ranges = s_matkl ). lo_sel->get_collected_ranges( IMPORTING et_named_ranges = DATA(lt_named_ranges) ).