Updates
- 02.06.2016 Added link to blog that describes how to implement updates
Introduction
In a previous blog OData service development with SAP Gateway - code-based service development - Part I I described how to implement as basic OData Service using code based implementation.
Since the recommended development approach as of SAP NetWeaver 750 is to use CDS views I would like to show how a service having the same capabilities as the service in the blog mentioned above can be generated based on CDS views.
It will turn out that from a development perspective in the ABAP stack this is much less effort if appropriate CDS views are in place and if your backend system is based on SAP NetWeaver 750.
If however no appropriate CDS view is in place instead of having the effort of developing an OData service via code based implementation you would have the effort to develop the appropriate DDL source code.
Prerequisites
As a prerequisite we have to create a consumption view ZSEPM_C_SALESORDER_TPL since it is not good practice to access core CDS views directly. This consumption view is then also not annotated as an analytical view as the underlying core CDS view SEPM_I_SALESORDER_E.
The DDL source code of the consumption view ZSEPM_C_SALESORDER_TPL is shown at the end of this blog.
Generating an OData service via Referenced Data Source
- We first have to start with creating a new Service Builder project calledZE2E100_<XX>_2 since the project in the previous blog mentioned above was calledZE2E100_<XX>
Note: Replace <xx> with your group number - Right click on the folder Data Model.
Choose Reference --> Modeled Data Source Reference from the context menu
- The Reference Data Source Wizard opens.
On the first screen choose:
CDS Core Data Services for the field Modeled Data Source Type.
ZSEPM_C_SALESORDER_TPL for the field Modeled Data Source Name
- In the second window of the wizard select the following associations’
_CUSTOMER and _ITEM.
Please note that the cds views SEPM_I_BUSINESSPATNER_E and SEPM_I_SALESORDERITEM_E are automatically selected as well.
Press Finish
- Press the Generate Runtime Objects button
- In the Model and Service Definition screen leave the default values unchanged and press Continue.
- In the Create Object Directory Entry dialogue press Local Object or enter $TMP.
- Expand the folder Service Maintenance right click on the entry GW_HUB and choose Register.
- In the Add Service dialogue enter $TMP for the package assignment or press Local Object.
- Expand the folder Service Maintenance right click on the entry GW_HUB and choose SAP Gateway Client.
Press <Execute>
- Check the Service Document
It shall contain three entity sets:
- Zsepm_C_Salesorder_Tpl
- SEPM_I_SalesOrderItem_E
- SEPM_I_BusinessPartner_E
- Check the metadata document by selecting <Add URI option> and select $metadata
Check the Metadata Document
Please note that the entity type SEPM_I_SalesOrder_EType contains:
- Two navigation properties to_Customer and to_Item
- A property SalesOrder_Text that has been generated by the SADL framework based on the annotation @ObjectModel.text.association: '_Text'.
Please note this property is annotated as sap:updatable=”false”;
- Now we can test the service and we will see that it supports various query options, $expand and selecting single entities out of the box.
To do so execute the following URI’s in the SAP Gateway Client (transaction /n/IWFND/GW_CLIENT):- /sap/opu/odata/SAP/ZE2E100_XX_2_SRV/Zsepm_C_Salesorder_Tpl?$filter=GrossAmountInTransacCurrency ge 100000&$select=SalesOrder,GrossAmountInTransacCurrency,TransactionCurrency&$format=json
- $skip and $top together with $inlinecount work out of the box as well
/sap/opu/odata/SAP/ZE2E100_XX_2_SRV/Zsepm_C_Salesorder_Tpl?$filter=GrossAmountInTransacCurrency ge 100000&$select=SalesOrder,GrossAmountInTransacCurrency,TransactionCurrency&$top=2&$skip=1&$inlinecount=allpages&$format=json
Please note that via &$inlinecount=allpages we retrieve the number of entries that would be returned without using $skip and $top
- Read a single entity from the list of the sales order
/sap/opu/odata/SAP/ZE2E100_XX_2_SRV/Zsepm_C_Salesorder_Tpl('5000000<XX>')?$format=json
Please note that the weird looking key stems from the fact that the CDS view is annotated as an analytical view. - Read the list of items of a single sales order via the navigation property to_Item
/sap/opu/odata/SAP/ZE2E100_XX_2_SRV/Zsepm_C_Salesorder_Tpl('5000000<XX>')/to_Item?$format=json
How to implement updates in a service that has been generated using referenced data sources will be described in the following blog
DDL source code
define view Zsepm_C_Salesorder_Tpl as select from SEPM_I_SalesOrder_E {
@ObjectModel.text.association: '_Text'
key SEPM_I_SalesOrder_E.SalesOrder,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.CreationDateTime,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.LastChangedDateTime,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.IsCreatedByBusinessPartner,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.IsLastChangedByBusinessPartner,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.Customer,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.TransactionCurrency,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.GrossAmountInTransacCurrency,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.NetAmountInTransactionCurrency,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.TaxAmountInTransactionCurrency,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.SalesOrderLifeCycleStatus,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.SalesOrderBillingStatus,
@ObjectModel.readOnly: false
SEPM_I_SalesOrder_E.SalesOrderDeliveryStatus,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.SalesOrderOverallStatus,
@ObjectModel.readOnly: true
SEPM_I_SalesOrder_E.Opportunity,
/* Associations */
SEPM_I_SalesOrder_E._BillingStatus,
SEPM_I_SalesOrder_E._Customer,
SEPM_I_SalesOrder_E._DeliveryStatus,
SEPM_I_SalesOrder_E._Item,
SEPM_I_SalesOrder_E._LifeCycleStatus,
SEPM_I_SalesOrder_E._OverallStatus,
SEPM_I_SalesOrder_E._Text,
SEPM_I_SalesOrder_E._TransactionCurrency
}