Skip to Content

OData service development with SAP Gateway – code-based service development – Part II

Since the editor didn’t let me enter additional screen shots I am continuing my last blog post OData service development with SAP Gateway – code-based service development – Part I in a new one: 😉

Change Log

  • 24.11.2016 – Moved ABAP Code to code boxes

Overview

We will continue our service implementation by implementing

 

  1. the GET_ENTITY method for the SalesOrderSet entity set and
  2. by implementing navigation to the items of a sales order

 

Provision the GET_ENTITY method of the service using ABAP code

 

  • Open the Service Builder Project again
  • Expand the node Service Implementation and SalesOrderSet and choose the entry
    GetEntity (Read).
    Now right-click on
    GetEntity (Read) and choose
    Go to ABAP Workbench.

/wp-content/uploads/2016/05/image044_964984.png

  • Confirm the warning“Operation SALESORDERSET_GET_ENTITY has not yet been implemented”

 

  • Switch to edit mode, scroll down to the method SALESORDERSET_GET_ENTITY and make sure to select it.Click on the Redefine Method button.

    Please note that the method SALESORDERSET_GET_ENTITYSET has already been redefined and therefore appears in black color.

/wp-content/uploads/2016/05/image045_964997.png

  • Use Cut&Paste to copy the entire coding into the ABAP editor.Replace ‘<XX>’ by your group numberThe code first calls the method io_tech_request_context->get_converted_keys.

    The retrieved key value is used to select a single entry which is stored in the return structure
    er_entityof the method.

 

  method salesorderset_get_entity.

    data: lt_keys       type /iwbep/t_mgw_tech_pairs,
          ls_key        type /iwbep/s_mgw_tech_pair,
          ls_bp_key     type zcl_ze2e100_xx_mpc=>ts_salesorder-salesorder,
          ls_headerdata type zcl_ze2e100_xx_mpc=>ts_salesorder.

    call method io_tech_request_context->get_converted_keys
      importing
        es_key_values = ls_headerdata.

    ls_bp_key = ls_headerdata-salesorder.

    select single *
      into corresponding fields of @er_entity
      from sepm_i_salesorder_e
      where salesorder = @ls_headerdata-salesorder.


  endmethod.

   

 

 

  • Click on Activate
  • Confirm the Activation popup
  • Navigate back to the Service Builder main screen using the back button  multiple times.
  • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.
  • Alternatively click on the SAP Gateway Client button in the detail section.Enter the following URI
    /sap/opu/odata/SAP/ZE2E100_<XX>_SRV/SalesOrderSet('500000000')​

    Replace ‘<XX>’ by your group number and Execute.
    You will get the details of a single business partner as a response

 

/wp-content/uploads/2016/05/image046_964998.png

 

Add an additional entity set for Sales Order Items

  • Open the Service Builder Project again
  • We will now create a second entity type for sales order items and entity set.Right-click again on Data Model and select Import –> DDIC Structure.

 

  • In the first step of the wizard provide:In the field Name, enter SalesOrderItem.
    In the field ABAP Structure, enter SEPM_ISOIE.
    Click Next.

/wp-content/uploads/2016/05/image047_964999.png

  • In the second step of the wizard provide:Select the checkbox for SEPM_ISOIE.
    Deselect the checkbox for MANDT,Click Next.

/wp-content/uploads/2016/05/image048_965003.png

  • In the third screen of the wizard:
    Select the checkbox Is Key for the fields SALESORDER and SALESORDERITEM.
    Click Finish.

/wp-content/uploads/2016/05/image049_965004.png

  • Press Save

Add an association and navigation properties

  • Expand Data Model and Right-click on Association and select Create

/wp-content/uploads/2016/05/image050_965005.png

  • In the first step of the wizard provide:Association Name
    Assoc_SalesOrder_SalesOrderItem

 

Principal Entity

Entity Type Name

SalesOrder

Cardinality

1

 

Leave the checkbox Create related Navigation Property checked

 

Enter ToItems in the field Navigation Property

 

Dependent Entity

Entity Type Name

SalesOrderItem

Cardinality

1..n

Select the checkbox Create related Navigation Property checked

Enter ToSalesOrder in the field Navigation Property

/wp-content/uploads/2016/05/image051_965015.png

  • Click Next
  • In the second step of the wizard in the field Dependent Property, enter SalesOrder by using the value help.
    Click
    Next

/wp-content/uploads/2016/05/image052_965016.png

  • In the third step of the wizard press Finish.
  • Press the Check Project Consistency button.
  • Verify that no errors were found.
  • Press Generate Runtime Objects

Provision the GET_ENTITY_SET method of the entity set SalesOrderItemSet

 

  • Open the Service Builder Project again
  • Expand the node Service Implementation and SalesOrderItemSet and choose the entry GetEntitySet (Query).
    Now right-click on GetEntitySet (Query) and choose Go to ABAP Workbench.

/wp-content/uploads/2016/05/image057_965017.png

  • Confirm the warning“Operation SALESORDERITEMSE_GET_ENTITYSET has not yet been implemented”
  • Switch to edit mode, scroll down to the method SALESORDERITEMSE_GET_ENTITYSET and make sure to select it.Click on the Redefine Method button.

/wp-content/uploads/2016/05/image059_965024.png

  • Use Cut&Paste to copy the entire code shown below.Replace ‘<XX>’ by your group numberThe code first calls the method
    io_tech_request_context->get_navigation_path

    If the entity set is accessed via the navigation property ‘TOITEMS’ the sales order id is retrieved from the URI.

    If the entity set is accessed directly the where clause is retrieved from io_tech_request_context.

    Please note that the methods SALESORDERSET_GET_ENTITY and SALESORDERSET_GET_ENTITYSET have already been redefined and therefore appear in black color.

 

 method salesorderitemse_get_entityset.
    data: lt_nav_path   type /iwbep/t_mgw_tech_navi,
          ls_nav_path   type /iwbep/s_mgw_tech_navi,
          lt_keys       type /iwbep/t_mgw_tech_pairs,
          ls_key        type /iwbep/s_mgw_tech_pair,
          ls_so_key     type zcl_ze2e100_xx_mpc=>ts_salesorder-salesorder,
          ls_headerdata type zcl_ze2e100_xx_mpc=>ts_salesorder.


    data: lv_osql_where_clause type string.

    lt_nav_path = io_tech_request_context->get_navigation_path( ).

    read table lt_nav_path into ls_nav_path with key nav_prop = 'TOITEMS'.

    if sy-subrc = 0.

      call method io_tech_request_context->get_converted_source_keys
        importing
          es_key_values = ls_headerdata.

      ls_so_key = ls_headerdata-salesorder.

      select * from sepm_i_salesorderitem_e
      into corresponding fields of table @et_entityset
      where salesorder = @ls_so_key.

    else.

      lv_osql_where_clause = io_tech_request_context->get_osql_where_clause_convert( ).

      select * from sepm_i_salesorderitem_e
     into corresponding fields of table @et_entityset
     where (lv_osql_where_clause).

    endif.

  endmethod.

    

      

 

     

 

 

  • Click on Activate
  • Confirm the Activation popup
  • Navigate back to the Service Builder main screen using the back button  multiple times.
  • In the navigation tree right-click on GW_HUB and select SAP Gateway Client.Alternatively click on the SAP Gateway Client button in the detail section.

 

  • Enter the following URI

    /sap/opu/odata/SAP/ZE2E100_<XX>_SRV/SalesOrderSet('500000000')/ToItems

    Replace <XX> by your group number and Execute.
    You will get the items of the sales order
    500000000 as a response.

 

  • Enter the following URI
    /sap/opu/odata/SAP/ZE2E100_<XX>_SRV/SalesOrderItemSet?$filter=Salesorder eq '500000000'​

    Replace <XX> by your group number and Execute.

    Also here you will get the items of the sales order
    500000000 as a response

 

If you now want to annotate this service so that you are able to use the list report template from SAP Web IDE please check out my following blog:

How to add annotations to an OData service using code based implementation

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