Sap Odata Service Part 3 Filtering

Kaan Can Calkan
7 min readMay 12, 2023

--

We create a new project from SEGW T-Code.

We check the table structure of the ABAP we will use from SE37.

In the SEGW transaction code, I right-click on the Entity Types under my project and click ‘Create’

If we check this box, it will automatically create the relevant EntitySet

Then, we create another Entity Type and provide the required ABAP Structure

As you can see here, the properties in the ABAP Structure did not appear.

To bring them, we right-click and select Import > Properties.

In this screen, we select the desired properties and click on ‘Next’

In the next screen, we will make a key selection (you can think of it as Primary Key in MSSQL). We select at least one key and click on ‘Finish’.

As you can see, our properties have appeared.

We do the same process for the Po_Item Property.

Then, we linked our Entity Sets to Entity Items.

After doing our operations, we click on the ‘Generate’ button.

Ardından projemin içindeki Runtime Artifacts klasörünün altındaki DPC_Extensionun üzerine çift tıklıyoruz.

Then, we double-click on the DPC_Extension under the Runtime Artifacts folder of my project.

A folder named ‘Redefinitions’ has been opened.

Then, we click on the ‘Activate’ button to activate the DPCs we use for our Entity Sets.

Then, we go to the transaction code /n/IWFND/MAINT_SERVICE. Here, we click on ‘Add Service’

Here, after entering our filters at the top, we select our service in the box and click “Add selected services”.

After selecting the local object here, we click on the tick and proceed.

Service has been created successfully.

You can filter and select the newly created service by going back to the previous screen and applying the filter.

Adding an asterisk (*) before and after a term in SAP means to search for any value that contains that term.

Then, I click on the SAP Gateway client.

Here, we click on Execute.

The response code 200 indicates success.

I’m setting an external breakpoint on PO_HeaderSet in the EGW transaction code to debug it. (If you don’t set it to external, it won’t stop at the breakpoint when we execute it on the Web Service screen.)

Then, we execute the Header Entity Set from the Entity Sets.

You saw the variables that we will use for filtering on the debug screen.

If we execute the URL /sap/opu/odata/sap/ZMEDIUM_PO_DETAILS_SRV_01/PO_HeaderSet?$filter=(PoNumber eq ‘200’)

In the Debug screen, we can see our Filter Parameter in Filtering Options. Additionally, a table has been created in the Filter Select Options.

If we click on the “Filter Options” in this step, we can see the created table.

Now we are moving on to the coding part.

First, we defined a variable named lv_ebeln of type ebeln.

Then, we defined wa_filter_options of type /IWBEP/S_MGW_SELECT_OPTION, wa_header of type BAPIEKKOL, it_filter_value of type /IWBEP/T_COD_SELECT_OPTIONS and it_filter_values of type /IWBEP/T_COD_SELECT_OPTIONS.

We can determine which type to use from here.

We read the it_filter_select_options table to get the filter options for selecting a purchase order. Then, we checked if the filter option for the purchase order number exists in the table using PROPERTY = ‘PoNumber’ as the key. If subrc is 0 (meaning no error), we saved the purchase order number in the lv_ebeln variable.

We read the Filter Select Options and its Key in the DPC Extension in the Filter section. If we had sent more than 1 filter, we would have needed to change INDEX 1 to that number accordingly.

After that, we called the BAPI_PO_GETDETAIL function module to retrieve the details of the purchase order header based on the given purchase order number. We stored the header details in the wa_header variable.

Here, we press the Pattern button to call the BAPI.

In the popup box that appears, we select “Call Function” and type “BAPI_PO_GETDETAIL” to call the corresponding function module.

Finally, we added the header data to ET_ENTITYSET and cleared the wa_header variable.

method PO_ITEMSET_GET_ENTITYSET.
data: lv_ebeln type ebeln.

data: wa_filter_options type /IWBEP/S_MGW_SELECT_OPTION.
data: wa_header type BAPIEKKOL.
data : it_filter_value type /IWBEP/T_COD_SELECT_OPTIONS.
data: it_filter_values type /IWBEP/T_COD_SELECT_OPTIONS.
read TABLE it_filter_select_options into wa_filter_options with key PROPERTY ='PoNumber'.

IF sy-subrc eq 0.
it_filter_values = wa_filter_options-select_options[].
READ TABLE it_filter_values into data(wa_filter_values) index 1.
IF sy-subrc eq 0 .
lv_ebeln = wa_filter_values-low.
ENDIF.

CALL FUNCTION 'BAPI_PO_GETDETAIL'
EXPORTING
purchaseorder = lv_ebeln

TABLES
po_items = et_entityset.

ENDIF.
endmethod.

To test the code, you can go to the SE16n transaction and access the EKKO table.

I can use any of these data.

If we execute /sap/opu/odata/sap/ZMEDIUM_PO_DETAILS_SRV_01/PO_HeaderSet?$filter=(PoNumber eq ‘4500000009’) on SAP Gateway, we will get the PO header details of the purchase order with number 4500000009.

Our data is here.

Now we will do the same for Itemset.

Here, the only difference is that we removed the Importing parameters and defined the po_item to Tables.

method PO_ITEMSET_GET_ENTITYSET.
data: lv_ebeln type ebeln.

data: wa_filter_options type /IWBEP/S_MGW_SELECT_OPTION.
data: wa_header type BAPIEKKOL.
data : it_filter_value type /IWBEP/T_COD_SELECT_OPTIONS.
data: it_filter_values type /IWBEP/T_COD_SELECT_OPTIONS.
read TABLE it_filter_select_options into wa_filter_options with key PROPERTY ='PoNumber'.

IF sy-subrc eq 0.
it_filter_values = wa_filter_options-select_options[].
READ TABLE it_filter_values into data(wa_filter_values) index 1.
IF sy-subrc eq 0 .
lv_ebeln = wa_filter_values-low.
ENDIF.

CALL FUNCTION 'BAPI_PO_GETDETAIL'
EXPORTING
purchaseorder = lv_ebeln

TABLES
po_items = et_entityset.


ENDIF.
endmethod.

Itemset Filtering Working without problems too.

--

--

Kaan Can Calkan
Kaan Can Calkan

Written by Kaan Can Calkan

Former Web Developer , SAP HCM and Oracle Technical Consultant . Currently working as a SAP HCM Full Stack Dev .Sharing my learning path with you.

No responses yet