Enhance the Handler Class With Filtering
- How to filter by product
id
, supplier name, or product category
Prerequisites
- IMPORTANT: This tutorial cannot be completed on a trial account
- IMPORTANT: This tutorial is part of the mission Connect Your On-Premise System with SAP Cloud Platform, ABAP Environment. Please work through the previous tutorials in the mission first; otherwise this tutorial may not work.
- Step 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.
-
Enter a name for your duplicate class,
ZCL_PRODUCT_W_FILTER_XXX
, and choose Finish.
The duplicate class appears in a new editor.
-
- Step 2
-
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 TableABAPCopy"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.
-
Next define the variable that will contain the selected columns, passed to it from the table,
lt_fields
. (See step 4-5).ABAPCopyDATA lv_select_string TYPE string.
-
- Step 3
-
Define the variable that contains the filter value(s).
-
Wrap it in a
TRY ... CATCH
block.ABAPCopyTRY. "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 ENDTRY.
-
- Step 4
Get the names of the selected columns and store them in a table,
lt_fields
.ABAPCopyDATA(lt_fields) = io_request->get_requested_elements( ).
- Step 5
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.```ABAP " $select handling IF lt_fields IS NOT INITIAL. CONCATENATE LINES OF lt_fields INTO lv_select_string SEPARATED BY ','. ELSE. "check coding. If no columns are specified via $select retrieve all columns from the model instead? lv_select_string = '*'. ENDIF. ```
- Step 6
If the user has specified a filter for
productid
, then fill the ranges table with the user-specified valuesABAPCopy"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. ENDLOOP. ENDIF.
- Step 7
Do the same for any filter for
Category
orSupplier Name
.ABAPCopy"-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. ENDLOOP. ENDIF. "-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. ENDLOOP. ENDIF.
- Step 8
Add the table parameters for these ranges tables to the
BAPI
call.ABAPCopy
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. - Step 9
-
Open your custom entity,
ZCE_PRODUCT_XXX
. -
Change the following annotation to point to the new class:
CDSCopy@ObjectModel.query.implementedBy: 'ABAP:ZCL_SHOW_PRODUCTS_W_FILTER'
-
Save and activate (
Ctrl+S, Ctrl+F3
) the custom entity.
-
- Step 10
-
Save and activate (
Ctrl+S, Ctrl+F3
) the class. -
Open your service binding,
Z_BIND_PRODUCT_TEST_XXX
, then choose Preview…. -
In the Fiori Elements preview, enter a filter value, such as “Supplier Name = AVANTEL”, then choose Go.
The filtered list appears.
-
- Step 11
Which of the following is NOT a valid option for a ranges table. (See the SAP and third-party links for help if necessary.)
- Duplicate your class
- Define variables
- Define filter variable
- Create a table for the selected columns
- Specify which columns are to be retrieved
- Pass the user's filter values for `productid` to the ranges table
- Repeat for Supplier Name or Category
- Add the parameters to the BAPI call
- Specify this new class in your custom entity
- Save, activate, and test your class
- Test yourself