Skip to Content

Enhance the Handler Class With Filtering

0 %
Enhance the Handler Class With Filtering

Enhance the Handler Class With Filtering

November 26, 2020
Created by
August 2, 2020
Add filtering by three values - product ID, supplier name, or category

You will learn

  • How to filter by product id, supplier name, or product category
QR code


Step 1: Duplicate your class
  1. Select the class from the previous tutorial, Get Data from a Remote System Using a Custom Entity, and choose Duplicate… from the context menu.

  2. Enter a name for your duplicate class, ZCL_PRODUCT_W_FILTER_XXX, and choose Finish.

The duplicate class appears in a new editor.

Log on to answer question
Step 2: Define variables
  1. First, you will define the range tables. These tables will be filled with the filter value(s) the user enters - for example, all suppliers beginning with “T”. Each range table contains four columns: Sign (i.e. include or exclude), Operation (e.g. “Equals” or “Contains”), Low (either the first value in a range, or a single value), High (in ranges, the highest value; otherwise empty).

    For more information on range tables, see:
    - SAP Help Portal: SAP Gateway Foundation: Ranges Table and Structure
    - Third-Party Content: My Experiments with ABAP: SAP Range Table

    "select options
    DATA lt_filter_ranges_productid TYPE RANGE OF ZCE_product_001-productid.
    DATA ls_filter_ranges_productid LIKE LINE OF lt_filter_ranges_productid.
    DATA lt_filter_ranges_supplier  TYPE RANGE OF ZCE_product_001-suppliername.
    DATA ls_filter_ranges_supplier  LIKE LINE OF lt_filter_ranges_supplier.
    DATA lt_filter_ranges_category  TYPE RANGE OF ZCE_product_001-category.
    DATA ls_filter_ranges_category  LIKE LINE OF lt_filter_ranges_category.
  2. Next define the variable that will contain the selected columns, passed to it from the table, lt_fields. (See step 4-5).

    	DATA lv_select_string TYPE string.
Log on to answer question
Step 3: Define filter variable
  1. Define the variable that contains the filter value(s).

  2. Wrap it in a TRY ... CATCH block.

    	"get and add filter
    	DATA(lt_filter_cond) = io_request->get_filter( )->get_as_ranges( ).
    	"get_filter_conditions( ).
    		CATCH cx_rap_query_filter_no_range INTO DATA(lx_no_sel_option).
    		  "@todo :
    		  " raise an exception that the filter that has been provided
    		  " cannot be converted into select options
    		  " here we just continue
Log on to answer question
Step 4: Create a table for the selected columns

Get the names of the selected columns and store them in a table, lt_fields.

DATA(lt_fields)  = io_request->get_requested_elements( ).
Log on to answer question
Step 5: Specify which columns are to be retrieved

If the user has specified that only some columns should be displayed, then store these column names in a string.
Otherwise, retrieve all columns in the table from the database.

" $select handling
IF lt_fields IS NOT INITIAL.
CONCATENATE LINES OF lt_fields INTO lv_select_string  SEPARATED BY ','.
"check coding. If no columns are specified via $select retrieve all columns from the model instead?
lv_select_string = '*'.
Log on to answer question
Step 6: Pass the user's filter values for `productid` to the ranges table

If the user has specified a filter for productid, then fill the ranges table with the user-specified values

"a filter has been provided

"get filter for ProductID
READ TABLE lt_filter_cond WITH KEY name = 'PRODUCTID' INTO DATA(ls_productid_cond).
IF sy-subrc EQ 0.
  LOOP AT ls_productid_cond-range INTO DATA(ls_sel_opt_productid).
	MOVE-CORRESPONDING ls_sel_opt_productid TO ls_filter_ranges_productid.
	INSERT ls_filter_ranges_productid INTO TABLE lt_filter_ranges_productid.
Log on to answer question
Step 7: Repeat for Supplier Name or Category

Do the same for any filter for Category or Supplier Name.

"-get filter for SUPPLIERNAME
READ TABLE  lt_filter_cond WITH  KEY name = 'SUPPLIERNAME' INTO DATA(ls_suppliername_cond).
IF sy-subrc EQ 0.
  LOOP AT ls_suppliername_cond-range INTO DATA(ls_sel_opt_suppliername).
    MOVE-CORRESPONDING ls_sel_opt_suppliername TO ls_filter_ranges_supplier.
    INSERT ls_filter_ranges_supplier INTO TABLE lt_filter_ranges_supplier.

"-get filter for CATEGORY
READ TABLE  lt_filter_cond WITH  KEY name = 'CATEGORY' INTO DATA(ls_category_cond).
IF sy-subrc EQ 0.
  LOOP AT ls_category_cond-range INTO DATA(ls_sel_opt_category).
    MOVE-CORRESPONDING ls_sel_opt_category TO ls_filter_ranges_category.
    INSERT ls_filter_ranges_category INTO TABLE lt_filter_ranges_category.
Log on to answer question
Step 8: Add the parameters to the BAPI call

Add the table parameters for these ranges tables to the BAPI call.

CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST' DESTINATION lv_rfc_dest_name EXPORTING max_rows = lv_maxrows TABLES headerdata = lt_product selparamproductid = lt_filter_ranges_productid selparamsuppliernames = lt_filter_ranges_supplier selparamcategories = lt_filter_ranges_category.
Log on to answer question
Step 9: Specify this new class in your custom entity
  1. Open your custom entity, ZCE_PRODUCT_XXX.

  2. Change the following annotation to point to the new class:

    @ObjectModel.query.implementedBy: 'ABAP:ZCL_SHOW_PRODUCTS_W_FILTER'
  3. Save and activate ( Ctrl+S, Ctrl+F3 ) the custom entity.

Log on to answer question
Step 10: Save, activate, and test your class
  1. Save and activate ( Ctrl+S, Ctrl+F3 ) the class.

  2. Open your service binding, Z_BIND_PRODUCT_TEST_XXX , then choose Preview….

  3. In the Fiori Elements preview, enter a filter value, such as “Supplier Name = AVANTEL”, then choose Go.

The filtered list appears.

Log on to answer question
Step 11: Test yourself
Which of the following is NOT a valid option for a ranges table. (See the SAP and third-party links for help if necessary.)

Next Steps

Back to top