You are on page 1of 19

1)By Using the Below fields , Create the table ZPRODUCTDETAILS as show in the below screen shot

PRODUCT_ID Types SNWD_PRODUCT_ID


TYPE_CODE Types SNWD_PRODUCT_TYPE_CODE
CATEGORY Types SNWD_PRODUCT_CATEGORY
NAME Types SNWD_DESC
DESCRIPTION Types SNWD_DESC

2)Navigate to Tcode:SEGW and Create data service PROJECT

1
3) Create Entity Type

4)provide Entity Type name as ‘Product’. Press Enter

2
5)Expand it, Go to properties

Provide properties as

Click Enter and Save it

6) Now create entity set for this entity Type

Select entity set, right click and select create

7) Provide Entity Set name and Select entity type name

Click enter and Save it

Provide feature of ProductSet

8) Save it and generate it

3
9)Go to tcode: /IWFND/MAINT_SERVICE , Click on Add service button and enter the below information

System Alias: LOCAL


Technical Service Name: ZPRODUCTDETAIL_SRV and Press Enter

10) Select the Entry and click on ‘Add Selected Services’ button

Press Enter

11)Go to Tcode: /IWFND/MAINT_SERVICE and click on Filter button and Enter the below information
and Press Enter

4
12) Click on ‘SAP Gateway Client’ button

13)User url: /sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet and click on Execute button

As we have not implemented 'PRODUCTSET_GET_ENTITYSET'

Now go back to Service builder :SEGW Tcode

14) Go to Tcode SEGW and go to Service implementation->ProductSet->GetEntitySet(Query). Right click


and select Go to ABAP Workbench

5
15) It Prompt us and information us

16) Select the Method ‘PRODUCTSET_GET_ENTITYSET’ and click on Redefine Method

17) USE Below Code

  method PRODUCTSET_GET_ENTITYSET.

   data:ls_headerdata type ZPRODUCTDETAILS,
          lt_headerdata type TABLE OF ZPRODUCTDETAILS,
          ls_product LIKE LINE OF ET_ENTITYSET.

6
SELECT PRODUCT_ID TYPE_CODE CATEGORY NAME  DESCRIPTION
SUPPLIER_ID FROM ZPRODUCTDETAILS INTO  CORRESPONDING FIELDS OF TABLE lt_header
data.

 LOOP AT lt_headerdata INTO LS_HEADERDATA.
   ls_product-PRODUCT_ID =  LS_HEADERDATA-PRODUCT_ID.
   ls_product-TYPE_CODE =  LS_HEADERDATA-TYPE_CODE.
    ls_product-CATEGORY =  LS_HEADERDATA-CATEGORY.
    ls_product-NAME =  LS_HEADERDATA-NAME.
    ls_product-DESCRIPTION =  LS_HEADERDATA-DESCRIPTION.

ls_product-SUPPLIER_ID = LS_HEADERDATA-SUPPLIER_ID.
    APPEND LS_PRODUCT TO ET_ENTITYSET.

    ENDLOOP.
endmethod.

Go to GW Client and test with URL

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet?$format=json

You will get read results

18)

Now we will implement read method of product entity set

Go back to Data Services

7
USE CODE:

method PRODUCTSET_GET_ENTITY.

data:ls_key_tab TYPE /IWBEP/s_MGW_NAME_VALUE_PAIR,
          LS_PRODUCT_ID TYPE ZPRODUCTDETAILS-PRODUCT_ID,
         LS_HEADERDATA TYPE ZPRODUCTDETAILS.

          loop at it_key_tab into ls_key_tab.
           IF LS_KEY_TAB-NAME = 'PRODUCT_ID'.
             LS_PRODUCT_ID = LS_KEY_TAB-VALUE.
              ENDIF.

            ENDLOOP.

   SELECT SINGLE PRODUCT_ID TYPE_CODE CATEGORY NAME  DESCRIPTION supplier_id
     FROM ZPRODUCTDETAILS INTO CORRESPONDING FIELDS OF
     LS_HEADERDATA WHERE PRODUCT_ID = LS_PRODUCT_ID.

8
   ER_ENTITY-PRODUCT_ID =  LS_HEADERDATA-PRODUCT_ID.
    ER_ENTITY-TYPE_CODE =  LS_HEADERDATA-TYPE_CODE.
    ER_ENTITY-CATEGORY =  LS_HEADERDATA-CATEGORY.
    ER_ENTITY-NAME =  LS_HEADERDATA-NAME.
   ER_ENTITY-DESCRIPTION =  LS_HEADERDATA-DESCRIPTION.
   er_entity-supplier_id = ls_headerdata-supplier_id.

endmethod.

Go to GW Client and test with url

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet('HT-1000')?$format=xml

19) Now use the Filter criteria

method PRODUCTSET_GET_ENTITYSET.
DATA: ls_headerdata TYPE zproductdetails,
       lt_headerdata TYPE STANDARD TABLE OF zproductdetails,
       ls_product LIKE LINE OF et_entityset.

    DATA: ls_selparamproductid TYPE bapi_epm_product_id_range,
          lt_selparamproductid TYPE STANDARD TABLE OF
bapi_epm_product_id_range,

    ls_filter_select_options TYPE /iwbep/s_mgw_select_option,
         ls_select_option TYPE /iwbep/s_cod_select_option.

    LOOP AT it_filter_select_options INTO ls_filter_select_options.
      IF ls_filter_select_options-property EQ 'PRODUCT_ID'.

        LOOP AT ls_filter_select_options-select_options INTO
ls_select_option.
          ls_selparamproductid-sign = ls_select_option-sign.
          ls_selparamproductid-option = ls_select_option-option.

9
          ls_selparamproductid-low = ls_select_option-low.
          ls_selparamproductid-high = ls_select_option-high.
          APPEND ls_selparamproductid TO lt_selparamproductid.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

    SELECT product_id type_code category name  description SUPPLIER_ID
FROM zproductdetails INTO  CORRESPONDING FIELDS OF TABLE
lt_headerdata WHERE product_id IN lt_selparamproductid .

    LOOP AT lt_headerdata INTO ls_headerdata.
      ls_product-product_id =  ls_headerdata-product_id.
      ls_product-type_code =  ls_headerdata-type_code.
      ls_product-category =  ls_headerdata-category.
      ls_product-name =  ls_headerdata-name.
      ls_product-description =  ls_headerdata-description.

ls_product-SUPPLIER_ID = ls_headerdata-SUPPLIER_ID.
        APPEND ls_product TO et_entityset.

    ENDLOOP.
   endmethod.

URL

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet?$filter=PRODUCT_ID ge 'HT-1000' and


PRODUCT_ID le 'HT-1003'&$format=json

20) Navigation Property

First We need to create a Supplier Entity

First create Supplierid in product entity

10
Regenerate the Project

Do similar change in Entity_set and entity implementation as

Entity_set Code:

ls_product-supplier_id = ls_headerdata-supplier_id.

METHOD productset_get_entityset.

    DATA: ls_headerdata TYPE zproductdetails,
       lt_headerdata TYPE STANDARD TABLE OF zproductdetails,
       ls_product LIKE LINE OF et_entityset.

    DATA: ls_selparamproductid TYPE bapi_epm_product_id_range,
          lt_selparamproductid TYPE STANDARD TABLE OF
bapi_epm_product_id_range,

    ls_filter_select_options TYPE /iwbep/s_mgw_select_option,
         ls_select_option TYPE /iwbep/s_cod_select_option.

    LOOP AT it_filter_select_options INTO ls_filter_select_options.
      IF ls_filter_select_options-property EQ 'PRODUCT_ID'.

        LOOP AT ls_filter_select_options-select_options INTO
ls_select_option.
          ls_selparamproductid-sign = ls_select_option-sign.
          ls_selparamproductid-option = ls_select_option-option.
          ls_selparamproductid-low = ls_select_option-low.
          ls_selparamproductid-high = ls_select_option-high.
          APPEND ls_selparamproductid TO lt_selparamproductid.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

    SELECT product_id type_code category name  description
supplier_id FROM zproductdetails INTO  CORRESPONDING FIELDS OF TABLE
lt_headerdata WHERE product_id IN lt_selparamproductid .

    LOOP AT lt_headerdata INTO ls_headerdata.

11
      ls_product-product_id =  ls_headerdata-product_id.
      ls_product-type_code =  ls_headerdata-type_code.
      ls_product-category =  ls_headerdata-category.
      ls_product-name =  ls_headerdata-name.
      ls_product-description =  ls_headerdata-description.
      ls_product-supplier_id = ls_headerdata-supplier_id.
        APPEND ls_product TO et_entityset.

    ENDLOOP.

Save & Activate

21) Entity(Read)

 er_entity-supplier_id = ls_headerdata-supplier_id.

Use Code:

 method PRODUCTSET_GET_ENTITY.

    data:ls_key_tab TYPE /IWBEP/s_MGW_NAME_VALUE_PAIR,
          LS_PRODUCT_ID TYPE ZPRODUCTDETAILS-PRODUCT_ID,
         LS_HEADERDATA TYPE ZPRODUCTDETAILS.

          loop at it_key_tab into ls_key_tab.
           IF LS_KEY_TAB-NAME = 'PRODUCT_ID'.
             LS_PRODUCT_ID = LS_KEY_TAB-VALUE.
              ENDIF.

            ENDLOOP.

   SELECT SINGLE PRODUCT_ID TYPE_CODE CATEGORY NAME  DESCRIPTION
     SUPPLIER_ID FROM ZPRODUCTDETAILS INTO CORRESPONDING FIELDS OF
     LS_HEADERDATA WHERE PRODUCT_ID = LS_PRODUCT_ID.

   ER_ENTITY-PRODUCT_ID =  LS_HEADERDATA-PRODUCT_ID.
    ER_ENTITY-TYPE_CODE =  LS_HEADERDATA-TYPE_CODE.
    ER_ENTITY-CATEGORY =  LS_HEADERDATA-CATEGORY.
    ER_ENTITY-NAME =  LS_HEADERDATA-NAME.
   ER_ENTITY-DESCRIPTION =  LS_HEADERDATA-DESCRIPTION.
   er_entity-supplier_id = ls_headerdata-supplier_id.

12
 endmethod.

22) Now Lets Create new Custom Table as shown below

BP_ID SNWD_PARTNER_ID

COMPANY_NAME SNWD_COMPANY_NAME

23) Lets create Supplier Entity and Entityset

Go to Entity

13
Create properties Supplierid & Supplier Name

24) Lets Implement code for SupplierSet

 method SUPPLIERSET_GET_ENTITY.

DATA: ls_key_tab TYPE /iwbep/s_mgw_name_value_pair,
 ls_bp_id TYPE ZPARTNERDETAIL ,
ls_headerdata TYPE ZPARTNERDETAIL .
    LOOP AT it_key_tab INTO ls_key_tab.
      IF ls_key_tab-name EQ 'BP_ID'.
       ls_bp_id-bp_id = ls_key_tab-value.
     ENDIF.
 ENDLOOP.

 select  single * from ZPARTNERDETAIL into CORRESPONDING FIELDS OF ls_headerda
ta where BP_ID = ls_bp_id-bp_id.

    er_entity-BP_ID = ls_headerdata-bp_id.
   er_entity-COMPANY_NAME = ls_headerdata-company_name.

  endmethod.

Save check and activate

Check details at GW_client

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/SupplierSet('0100000001')

25)

METHOD supplierset_get_entityset.

          DATA: ls_bpheaderdata TYPE ZPARTNERDETAIL,
      lt_bpheaderdata TYPE STANDARD TABLE OF ZPARTNERDETAIL,
        ls_supplier LIKE LINE OF et_entityset.

 select * from ZPARTNERDETAIL into CORRESPONDING FIELDS OF TABLE  lt_bpheaderd
ata.

14
    LOOP AT lt_bpheaderdata INTO ls_bpheaderdata.
    ls_supplier-BP_ID = ls_bpheaderdata-bp_id.
      ls_supplier-COMPANY_NAME = ls_bpheaderdata-company_name.
    APPEND ls_supplier TO et_entityset.
  ENDLOOP.

 ENDMETHOD.

Save check and activate

Check details at GW_client

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/SupplierSet?$format=json

26)

Now, for navigation between two entities we have to create association between to entities

Now, for navigation between two entities we have to create


association between two entities
Steps to create Navigation

A) Go to Association

15
B) You will get a pop up, provide details as

If we give cardinality 1 to 1 then get_entity method will trigger and if I give cardinality 0.n then
get_entityset method will trigger.
Click on next
C) Click next and Select the Dependent Property as SupplierID and click Next

D) Click on Finish

Save, check, and re-generate.

27) Now lets test it

First metadata

16
/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/$metadata

Now lets Examine ProductSet


/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet?$format=json

28)Replace code of SUPPLIERSET_GET_ENTITY

 METHOD supplierset_get_entity.

DATA: ls_key_tab    TYPE /iwbep/s_mgw_name_value_pair,
          ls_bp_id      TYPE zpartnerdetail,
          ls_headerdata TYPE zpartnerdetail.
    DATA: ls_product TYPE zcl_zproductdetail_mpc=>ts_product.

    IF iv_entity_set_name EQ 'SupplierSet'.
      CALL METHOD productset_get_entity
        EXPORTING
          iv_entity_name          = iv_entity_name
          iv_entity_set_name      = iv_entity_set_name
          iv_source_name          = iv_source_name
          it_key_tab              = it_key_tab
          io_request_object       = io_request_object
          io_tech_request_context = io_tech_request_context
          it_navigation_path      = it_navigation_path
        IMPORTING
          er_entity               = ls_product.

      ls_bp_id-bp_id = ls_product-supplier_id.

  if ls_product is INITIAL.
           LOOP AT it_key_tab INTO ls_key_tab.
        IF ls_key_tab-name EQ 'BP_ID'.
          ls_bp_id-bp_id = ls_key_tab-value.
        ENDIF.
      ENDLOOP.
        endif.
    ELSE.
      LOOP AT it_key_tab INTO ls_key_tab.
        IF ls_key_tab-name EQ 'SupplierID'.
          ls_bp_id-bp_id = ls_key_tab-value.

        ENDIF.
      ENDLOOP.
    ENDIF.

17
    SELECT  SINGLE * FROM zpartnerdetail INTO CORRESPONDING FIELDS OF ls_heade
rdata WHERE bp_id = ls_bp_id-bp_id.

    er_entity-BP_ID = ls_headerdata-bp_id.
er_entity-COMPANY_NAME = ls_headerdata-company_name.

 ENDMETHOD.

29)

Now,
Replace code of PRODUCTSET_GET_ENTITY

 METHOD productset_get_entity.

 DATA: ls_key_tab    TYPE /iwbep/s_mgw_name_value_pair,
          ls_product_id TYPE zproductdetails-product_id,
          ls_headerdata TYPE zproductdetails.

    DATA: lr_mr_api TYPE REF TO if_mr_api.

    lr_mr_api = cl_mime_repository_api=>if_mr_api~get_api( ).

    LOOP AT it_key_tab INTO ls_key_tab.
      IF ls_key_tab-name EQ 'PRODUCT_ID'.
        ls_product_id = ls_key_tab-value.
      ENDIF.
    ENDLOOP.

    SELECT SINGLE product_id type_code category name  description
         supplier_id FROM zproductdetails INTO CORRESPONDING FIELDS OF
         ls_headerdata WHERE product_id = ls_product_id.

    er_entity-product_id =  ls_headerdata-product_id.
    er_entity-type_code =  ls_headerdata-type_code.
    er_entity-category =  ls_headerdata-category.
    er_entity-name =  ls_headerdata-name.
    er_entity-description =  ls_headerdata-description.
    er_entity-supplier_id = ls_headerdata-supplier_id.

ENDMETHOD.

18
Now use URL

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet('HT-1000')/$links/ToSupplier

Will give all the URL of Tosupplier for suppler Data

30)Use the URL

/sap/opu/odata/sap/ZPRODUCTDETAIL_SRV/ProductSet('HT-1000')/ToSupplier

19

You might also like