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:
We will continue our service implementation by implementing
- the GET_ENTITY method for the SalesOrderSet entity set and
- 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.
- 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.
- Use Cut&Paste to copy the entire coding into the ABAP editor.
Replace ‘<XX>’ by your group number
The 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.
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_keysimporting
es_key_values = ls_headerdata.
ls_bp_key = ls_headerdata-salesorder.
select single *into corresponding fields of @er_entityfrom sepm_i_salesorder_ewhere salesorder = @ls_headerdata-salesorder.
- 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
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.
- In the second step of the wizard provide:
Select the checkbox for SEPM_ISOIE.
Deselect the checkbox for MANDT,Click Next.
- In the third screen of the wizard:
Select the checkbox Is Key for the fields SALESORDER and SALESORDERITEM.
Click Finish.
- Press Save
Add an association and navigation properties
- Expand Data Model and Right-click on Association and select Create
- 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
- Click Next
- In the second step of the wizard in the field Dependent Property, enter SalesOrder by using the value help.
Click Next
- 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.
- 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.
- Use Cut&Paste to copy the entire code shown below.
Replace ‘<XX>’ by your group number
The 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.
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_keysimporting
es_key_values = ls_headerdata.
ls_so_key = ls_headerdata-salesorder.
select * from sepm_i_salesorderitem_einto corresponding fields of table @et_entitysetwhere salesorder = @ls_so_key.
else.
lv_osql_where_clause = io_tech_request_context->get_osql_where_clause_convert( ).
select * from sepm_i_salesorderitem_einto corresponding fields of table @et_entitysetwhere (lv_osql_where_clause).
endif.
- 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