In this Blog, I will explain creating Gateway Service with Read and Query operations using Date as an Input.  Before starting, I am expecting that you have basic idea of Gateway Service.

The reason for writing this Blog is, I had a requirement where I need to get the data from back-end system based on Date Field. I have searched Google but I was not able to find proper information on this requirement.

Scenario:

Search for flight details on given date and between the date ranges.

Procedure:


We have created ZSFLIGHT_DETAILS Gateway project to get the data based on Date Field as shown below.

/wp-content/uploads/2015/11/1_826225.png

Right Click on the Data Model folder and select Import DDIC structure and give the Entity Type Name as Sflight and select the required properties for the Entity Type.

In the Entity Type properties check Fldate as Key Field. Our Entity Type and its properties look as below.

/wp-content/uploads/2015/11/2_826232.png

Then click on Generate Runtime objects. It will display “Generated Objects Successfully”, all the required classes will be generated automatically.

1. Read Operation Using Date:


When user inputs date to find a flight on particular date. For this scenario we need to redefine SFLIGHTSET_GET_ENTITY method in the *DPC_EXT class and implement the below logic to read the data based on Date.

Code:

METHOD sflightset_get_entity.


*Data Declarations
DATA: ls_key LIKE LINE OF it_key_tab.

READ TABLE it_key_tab INTO ls_key WITH KEY name = ‘Fldate’.

IF sysubrc = 0.

SELECT SINGLE * FROM sflight INTO er_entity WHERE fldate = ls_keyvalue

  ENDIF.


ENDMETHOD.


2. Query Operation Using Date as filter:


When user wants to find flights between dates. For this scenario we need to redefine SFLIGHTSET_GET_ENTITYSET method in the *DPC_EXT class and implement the below logic to get the data based on Date Using Filter Query.

Code:


method SFLIGHTSET_GET_ENTITYSET.

*Data Declarations
DATA lo_filter                   TYPE  REF TO /iwbep/if_mgw_req_filter“Filter System Query Option Class
        lv_filter_str              
TYPE string,      “Filter Declaration
        lv_fldate                  
TYPE s_date,
        lt_filter_select_options   
TYPE /iwbep/t_mgw_select_option,
        r_date                     
TYPE RANGE OF sflightfldate,
        ls_date                    
LIKE LINE OF r_date,
        lt_select_options          
TYPE /iwbep/t_cod_select_options,
        ls_filter_select_options   
LIKE LINE OF lt_filter_select_options,
        ls_select_options          
TYPE /iwbep/s_cod_select_option.

*Read the filter Select options
READ TABLE IT_FILTER_SELECT_OPTIONS INTO ls_filter_select_options INDEX 1.

IF sysubrc = 0.
lt_select_options
= ls_filter_select_optionsselect_options.
ENDIF.


*Read the select options
READ TABLE lt_select_options INTO ls_select_options INDEX 1.

IF sysubrc = 0.

ls_datesign    = ‘I’ .
ls_date
option  = ‘BT’.
ls_date
low     = ls_select_optionslow.
ls_date
high    = ls_select_optionshigh.
APPEND ls_date TO r_date.
CLEAR ls_date.
ENDIF.


*To fetch the data from sflight based on Date Range

SELECT mandt carrid connid fldate FROM sflight INTO TABLE et_entityset
WHERE fldate IN r_date.
endmethod.

Testing Our Services:

Now we will test our service in Gateway Client transaction /IWFND/MAINT_SERVICE


/wp-content/uploads/2015/11/3_826244.png

/wp-content/uploads/2015/11/4_826246.png

In order to get the data for flight on given date use the following URI using HTTP GET Method.


URI: /sap/opu/odata/sap/ZSFLIGHT_DETAILS_SRV/SflightSet(Fldate=datetime’1995-02-28T00:00:00′)


Service Response:

Response.png

In order to get the flight details between the given date ranges, use the following URI using HTTP GET Method.

URI: /sap/opu/odata/sap/ZSFLIGHT_DETAILS_SRV/SflightSet?$filter=Fldate ge datetime’1995-02-28T00:00:00′ and Fldate le datetime’1997-08-30T00:00:00′

Service Response:

/wp-content/uploads/2015/11/6_826258.png

Thanks&Regards,

Gangadhar B

To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

  1. Petluru Karthikeya Bhagawath

    Good Presentation For Beginners Like Us Wish You To Present More Blogs For us .

          Thanks and Keep it up ,

            Karthikeya P.

    (0) 
  2. Siva prasad Ramani

    Hi Gangadhar,

    Thanks, looking so handy..!! 🙂 .

    Basically we are getting for date value like FLDATE = ‘1995-02-28’ right..!  Why can’t we get this way..?

    and Date is key field in your Entity Set there is no time field right..!

    Do we need to pass time format in our URI query..?? Is it necessary..?

    Regards,

    Siva R

    (0) 
    1. Gangadhar Rao Post author

      Hi Siva,

      In gateway passing date in URI, we need to pass date and time, passing only date is not supported. If you have tried passing only date, please share it with us, it will be very helpfull to all.

      Please check the screenshot for the reason to pass the date and time in URI.

      DateTime.png

      Thanks&Regards,

      Gangadhar

      (0) 

Leave a Reply