OData: Fetch records based on Search – using Wildcard Character (*)

lightOData Wildcard Character Search.

The post describes how to fetch records using wildcard characters.

Records of Table: SCARR

scarr-table

Goto T Code: SEGW and Create new project with desired name:

2

Created Project will appear in left panel of SEGW TCode.

Now Right click on Data model → Import → DDIC structure:

1

Provide the Entity name and Structure name as shown below:4

Select the desired field and click on Next:5

As at least one field should be Key field, selecting CARRID here as Key:3.jpg

You can check the created Entity in sub nodes of Data ModelData Model.jpg

Now Generate Runtime Objects by clicking below button,

you’ll get a popup → Next → Savegenerate-runtime-artifacts

In Runtime Artifacts node you can see the generated Classes and Service name. Here service name is ZMTEST4_WILD_CHARS_SRV

1.jpg

We need to write our logic in GET_ENTITYSET method of Data Provider Extension class (DPC_EXT) 1.jpg

Double click on DPC_EXT class and navigate to GET_ENTITYSET method (In our case it is: SCARRSET_GET_ENTITYSET). We need to redefine this method  using 2 button.

Importing parameter: IO_TECH_REQUEST_CONTEXT of GET_ENTITYSET method captures input search string from front end.

Table parameter: ET_ENTITYSET send output records to frontend.

Code written in GET_ENTITYSET method:


  TYPES BEGIN OF ty_scarr,
                  carrid    TYPE s_carr_id,
                  carrname  TYPE  s_carrname,
                  currcode  TYPE s_currcode,
                  url        TYPE s_carrurl,
               END OF ty_scarr.

  DATA lt_scarr TYPE STANDARD TABLE OF ty_scarr,
                 lv_str   TYPE string,
                lw_eset  TYPE zcl_zmtest4_wild_chars_mpc=>ts_scarr.

  FIELD-SYMBOLS <fs_scarr> TYPE ty_scarr.

* Get the InputValues from frontend
  CLEAR lv_str.
  lv_str io_tech_request_context->get_search_string).

 We need to replace ‘‘ with ‘%’
  REPLACE ALL OCCURRENCES OF ‘*’ IN lv_str WITH ‘%’.

* Fetch records from table SCARR
  REFRESH lt_scarr[].
  SELECT carrid
         carrname
         currcode
         url
    FROM scarr
    INTO TABLE lt_scarr
    WHERE carrid LIKE lv_str.
  IF sysubrc IS INITIAL.
    UNASSIGN <fs_scarr>.
    LOOP AT lt_scarr ASSIGNING <fs_scarr>.
      MOVE-CORRESPONDING <fs_scarr> TO lw_eset.
      APPEND lw_eset TO et_entityset.
      CLEAR lw_eset.
    ENDLOOP.
    UNASSIGN <fs_scarr>.
  ENDIF.


Now we need to Add the service: TCode: /IWFND/MAINT_SERVICE

Provide the System Alias name and service name and Add the service:addservice

After adding the service → Click on SAP Gateway Client button or GoTo TCode: /IWFND/GW_CLIENT2.jpg

Select your EntitySet

1.jpg

After selecting the EntitySet, we can filter the record by search keyword:

URL: /sap/opu/odata/sap/ZMTEST4_WILD_CHARS_SRV/SCARRSet?search=*

Output:

1.jpg

URL: /sap/opu/odata/sap/ZMTEST4_WILD_CHARS_SRV/SCARRSet?search=L*

Output:

1.jpg


 

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s