RFC: Enhance the Handler Class With Filtering
Add filtering by three values - product ID, supplier name, or category
Overview
You will learn
- How to filter by product
id, supplier name, or product category
Prerequisites
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.
Steps
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_000, and choose Finish.
The duplicate class appears in a new editor.
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
ABAP"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).ABAPDATA lv_select_string TYPE string.
Define the variable that contains the filter value(s).
Wrap it in a
TRY ... CATCHblock.ABAPTRY. "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.
Get the names of the selected columns and store them in a table, lt_fields.
DATA(lt_fields) = io_request->get_requested_elements( ).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.
```
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.
ENDLOOP.
ENDIF.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.
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.Add the table parameters for these ranges tables to the BAPI call.
CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
DESTINATION lv_destination
EXPORTING
max_rows = lv_maxrows
TABLES
headerdata = lt_product
selparamproductid = lt_filter_ranges_productid
selparamsuppliernames = lt_filter_ranges_supplier
selparamcategories = lt_filter_ranges_category.Open your custom entity,
ZCE_PRODUCT_000.Change the following annotation to point to the new class:
CDS@ObjectModel.query.implementedBy: 'ABAP:ZCL_SHOW_PRODUCTS_W_FILTER'
Save and activate ( Ctrl+S, Ctrl+F3 ) the custom entity.
Save and activate (
Ctrl+S, Ctrl+F3) the class.Open your service binding,
Z_BIND_PRODUCT_TEST_000, then choose Preview….In the Fiori Elements preview, enter a filter value, such as “Supplier Name = AVANTEL”, then choose Go.
The filtered list appears.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.