Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Andre_Fischer
Product and Topic Expert
Product and Topic Expert

Updates



  • 02.06.2016 Added link to blog that describes how to implement updates

  • 09.06.2016 Added section how to create the CDS view used for generating an OData service via Referenced Data Sources

  • 26.04.2017 Replaced screen shots that now show the latest version of ADT tools


 

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 are using a consumption view ZSEPM_C_SALESORDER_<#> 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.


How to create this consumption view ZSEPM_C_SALESORDER_<#> is shown in the following.


 


If you are using a system for a SAP CodeJam you can skip this step or create your own CDS view by replacing the hash tag <#> with your group number.


 




Creating the consumption view


 

Please note:

The creation of a consumption view is only necessary if you want to follow this how-to-guide on your own system or if you want to use your own CDS view.

For a SAP CodeJam or other SAP event we will use a system where this view has already been created.

 

 

 

  • Start the ABAP Development Tools


 

From the menu choose New --> ABAP Project



Select the system (here A4H) from the list of system connections and choose Next




  • In the "Connection Settings" screen press Next

  • Enter your credentials

  • Right click on your system connection and select New --> Other ...




Select Data Definition and press Next



  • Enter the following details for the new DDL source and press Finish

    Package: $TMP
    Name: ZSEPM_C_SALESORDER_<#>
    Description: SalesOrders - EPM Demo Data




Cut and paste the source code and press the Activate button. This will create the CDS view that we will use in this how to guide

You will have to adapt the name of the SQL view, the name of the CDS view and you should maintain the annotation for @EndUserText.label so that it contains your group number.

 



 


DDL source code


@AbapCatalog.sqlViewName: 'ZSEPM_ISOE_<#>'

@AbapCatalog.compiler.compareFilter: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: 'SalesOrders – EPM Demo Data'

define view Zsepm_C_Salesorder_<#>
as select from SEPM_I_SalesOrder_E
{

@ObjectModel.text.association: '_Text'

key SalesOrder,

@ObjectModel.readOnly: true

CreationDateTime,

@ObjectModel.readOnly: true

LastChangedDateTime,

@ObjectModel.readOnly: true

IsCreatedByBusinessPartner,

@ObjectModel.readOnly: true

IsLastChangedByBusinessPartner,

@ObjectModel.readOnly: true

Customer,

@ObjectModel.readOnly: true

TransactionCurrency,

@ObjectModel.readOnly: true

GrossAmountInTransacCurrency,

@ObjectModel.readOnly: true

NetAmountInTransactionCurrency,

@ObjectModel.readOnly: true

TaxAmountInTransactionCurrency,

@ObjectModel.readOnly: true

SalesOrderLifeCycleStatus,

@ObjectModel.readOnly: true

SalesOrderBillingStatus,

@ObjectModel.readOnly: true

SalesOrderDeliveryStatus,

@ObjectModel.readOnly: true

SalesOrderOverallStatus,

@ObjectModel.readOnly: true

Opportunity,

/* Associations */

_Customer,

_Item,

_Text


}




 

 

 

 

Generating an OData service via Referenced Data Source


 

  • We first have to start with creating a new Service Builder project called ZE2E100_<#>_2 since the project in the previous blog mentioned above was called ZE2E100_<#>

    Note:
    Replace <#> 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_<#> 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:




    1. Zsepm_C_Salesorder_<#>

    2. SEPM_I_SalesOrderItem_E

    3. 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 ZSEPM_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__2_SRV/Zsepm_C_Salesorder_?$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__2_SRV/Zsepm_C_Salesorder_?$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_<#>_2_SRV/Zsepm_C_Salesorder_<#>('5000000<X>')?$format=jsonPlease 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_<#>_2_SRV/Zsepm_C_Salesorder_<#>('5000000<#>')/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

 

OData service development with SAP Gateway using CDS via Referenced Data Sources - How to implement ...

 

 

27 Comments