Skip to Content
Technical Articles
Author's profile photo Akshay Kumar

Create CDS views in SAP S/4HANA Cloud and consume them as APIs

In this blog, I would like to show the detailed steps for creating a Custom CDS View in SAP S/4HANA Cloud and consuming it as an API. We would be creating a Custom CDS View with the help of two pre-delivered  CDS views – ‘I_PurchaseContractAPI01’ and ‘I_PurchaseRequisition_Api01’ to have the combined data accessible from a single API, let’s name it as ‘PurchasingInfo’.

Before we start, let’s quickly recap what CDS Views are:

CDS (Core Data Services) views are built on existing database tables and views, to provide an efficient way of data modeling.

More details:

https://help.sap.com/viewer/f2e545608079437ab165c105649b89db/7.5.5/en-US/4ed2245c6e391014adc9fffe4e204223.html

There are several whitelisted CDS views pre-delivered with SAP S/4HANA Cloud which can be utilized to retrieve various kind of data from SAP S/4HANA Cloud tenant.

SAP S/4HANA Cloud also provides the flexibility to customers for creating their own Custom CDS Views combining various whitelisted CDS views to meet their specific requirement of retrieving data.

These Custom CDS Views can be easily exposed as OData Service(API) which can then be utilized to fetch data out from SAP S/4HANA Cloud.

You can watch this video explaining CDS view use case with a demo, to understand about creating a Custom CDS View in SAP S/4HANA Cloud, exposing it as OData API and consuming it in SAP UI5 Application:

Note: These APIs are always read-only.

Now let’s take the example scenario which I used in the above video, where we want to see how many Purchase Contracts are used as a source of supply to a Purchase Requisition and/or Purchase Order.

For this we have one whitelisted CDS view to provide data of Purchase Contracts and another whitelisted CDS view to give details on Purchase Requisitions.

But we want the combined information which can be derived by creating a Custom CDS View based on these two available CDS views.

We will achieve this in 3 Steps:

  1. Create Custom CDS View with oData
  2. Create Custom Communication Scenario for this CDS View
  3. Setup Communication Management for this Communication Scenario

1.  Create Custom CDS View with oData:

1.1  Login to SAP S/4HANA Cloud, select ‘Extensibility’ group and click ‘Custom CDS Views’  application:

1.2  Click ‘Create’ to create a new Custom CDS View:

1.3  Provide a ‘Name’, ‘Label’ and check the ‘oData’ checkbox. The general naming convention for Custom CDS Views starts with ‘YY1_’ which is prefixed automatically:

Note: Checking the ‘oData’ checkbox would enable this CDS View for consumption through OData API.

1.4  Click ‘Add’ and then click ‘Add Primary Data Source’:

Note: For our scenario, we will add ‘I_PurchaseContractAPI01’ CDS View as ‘Primary Data Source’ and  ‘I_PurchaseRequisition_Api01’ CDS View as ‘Associated Data Source’

1.5  Search for ‘Purchase Contract’ and select ‘I_PurchaseContractAPI01’, then click ‘Add’ to add it as Primary Data Source:

1.6  Click ‘Add’ and then click ‘Add Associated Data Source’:

1.7  Search for ‘Purchase Requisition’, select ‘I_PurchaseRequisition_Api01’ and click ‘Add’:

Note: We can have one CDS View as Primary Data Source and one or more related CDS Views as Associated Data Sources based on our scenario.

1.8  Click ‘Edit Association Properties’ to define the association between the selected CDS Views:

1.9 Here we will define the association condition. In our case, the logic is that we need multiple fields where Purchase Contract number in the ‘I_PurchaseContractAPI01’ CDS View equals Purchase Contract number in ‘I_PurchaseRequisition_Api01’ CDS View.

Click ‘Value help’ for ‘Associated Data Source Fields’ and select ‘PurchaseContract’:

1.10  Select the ‘Operator’ as ‘=’ and click ‘Value help’ for ‘Value’:

1.11  Select ‘Purchase Contract’:

1.12  Click ‘OK’ as we have finished defining our condition:

1.13  Click ‘Field Selection’ through which we will select all those fields which we want to be displayed:

Note: All the fields available from both ‘I_PurchaseContractAPI01’ and ‘I_PurchaseRequisition_Api01’ CDS Views would be available under “Available Fields and Associations”.

1.14  Check the ‘Select’ checkbox for the fields which we want to be displayed in result:

1.15  Click ‘Publish’ which will publish our CDS View:

Note: We would need at least 1 key field to be present in the “Selected Fields and Associations” and the order for display (which field to be displayed first, second and so on) can be set by us. The fields are to be selected based on the requirement we have for showing data.

1.16  Click ‘Preview’ to see the display result of our published CDS View:

1.17  For example, here the result shows us the Purchase Contracts with or without Purchase Requisition and Purchase Order assigned to them:

 

2.  Create Custom Communication Scenario for this CDS View:

2.1  Click ‘Custom Communication Scenarios’ application under ‘Extensibility’ group:

2.2  Click ‘New’ to create a new Custom Communication Scenario:

2.3  Provide ‘Communication Scenario ID’ and ‘Description’ for your Communication Scenario and click ‘New’:

2.4  Click on ‘Inbound Services’ tab and then click ‘Add’:

2.5  Search for the CDS View we published as a result of Step 1, select it via checkbox and click ‘OK’:

2.6  Now click ‘Publish’:

Once the CDS View is published successfully, it can be verified by looking at status as shown in the below snapshot:

3. Setup Communication Management for this Communication Scenario:

In this step we will create a Communication User, Communication System and a Communication Arrangement with our Communication Scenario created in Step 2.

To follow the step by step approach for Communication management setup, you may follow my earlier blog: https://blogs.sap.com/2017/11/09/setting-up-communication-management-in-sap-s4hana-cloud/

In above-mentioned blog while performing Step 3.3 we need to select the Communication scenario which we have created in the current blog (YY1_PURCHASINGINFO):

As soon as we assign our Communication Scenario, we can see the Service URL to call our CDS View via oData.

Activate this Communication Arrangement and the status can be seen as ‘Active’:

This Service URL link can be accessed directly by entering it into the browser and the authentication would be done with the help of our Communication User created as part of Communication Management setup:

After successful authentication we would be able to see the entity present in it corresponding to our Communication Scenario:

Copy this entity name and append it in the Service URL to see the data inside it as shown below:

We are now able to see all the data which we exposed through API created from our Custom CDS View.

This API can now be used to create UI application on top of it and multiple other operations like filters can then be placed on UI to make it user-friendly.

Disclaimer: This blog is based on SAP S/4HANA Cloud 1802 release.

Note: In order to get fast answers and allow better tracking, please post your questions as a separate thread/post.

 

 

 

 

 

Assigned Tags

      19 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi,Akshay,do you know about the calculation of data type DATS in CDS View of cloud? When I add a field to do this operation ,the systom offers me an error 'Data type DATS is currently not supported in an arithmetic expression'.

       

      Author's profile photo Akshay Kumar
      Akshay Kumar
      Blog Post Author

      Hi Ellis,

      The details of calculation functions for Dates can be found here.

      You should be able to use them without any errors:

      Best Regards,

      Akshay

      Author's profile photo Former Member
      Former Member

      Thanks a lot

      Author's profile photo Faisal Khan
      Faisal Khan

      Hi Akshay,

      Very informative document indeed. My query is that  in step 1.4  we are adding the Primary Data source which are pre-delivered data sources. I would like to know how can i create a customized primary Data Source which then appears in the Primary Data source list?

      For your information: I did create a Consumption view which was available for me to create a Tile, Evaluation, Drill down on it in the KPI Design apps. I want that the same data source is available to me under the Primary Data Source in the Custlm CDS App tile.

      Thanks

      Faisal

      Author's profile photo Faisal Khan
      Faisal Khan

      Hi,

      In continuation of last message the code i am using is:

      @AbapCatalog.sqlViewName: 'Zfhk1First'
      @AbapCatalog.compiler.compareFilter: true
      @AccessControl.authorizationCheck: #NOT_REQUIRED
      @VDM.viewType: #CONSUMPTION
      @Analytics.query : true

      define view Zfhk1_First as select from viqmel {
      key viqmel.qmnum as NotifNo,
      @DefaultAggregation:#SUM
      viqmel.priok
      }

      where viqmel.qmnum between '000011' and '000099' ;

      I get the error:
      Error reading InfoProvider for CDS view VIQMEL [Analytics]

      Help will be highly appreciated.

      Regards

      Faisal

      Author's profile photo A. M. Fikri Paramadina Unggul Yudha
      A. M. Fikri Paramadina Unggul Yudha

      Hi Akshay Kumar,

      I have some problem on using Custom CDS, when added several field from another CDS.

      I used 4 standard CDS, 2 of them are header CDS and the rest are transactional (the rows quite a lots rather than the header CDS).

      Regarding of that needs, I took the fields from 4 CDS into 1 custom CDS which give impact to the value of the latest CDS. The value were duplicated into all of the rows, so I didnt get the actual value.

      This is the actual value in original CDS

      This is the value when i've joined the field from the 4 CDS

      Could you please explain me the way to get the actual value on the latest CDS

      Thank you

      Author's profile photo Ramesh Narala
      Ramesh Narala

      Hi Unggul Yudha,

      Were you able to resolve the duplicates issue? If so, could you please share the solution?

      Thanks in advance

      Ramesh N

       

      Author's profile photo Andreas Riehl
      Andreas Riehl

      Hello Ramesh,

      I did not look into this in detail yet, but duplicate values often come from modelling e.g. in a Custom CDS view. Do you have a Custom CDS View involved as well? Then you are most likely adding a field from a "to many" association. This will result in expanding your result set. See https://blogs.sap.com/2020/03/06/custom-cds-views-using-multiple-data-sources/

      Best Regards,
      Andreas

      Author's profile photo Ramesh Narala
      Ramesh Narala

      Hi Andreas,

      Sorry for delayed reply.

      Yes, I am working on quite a few custom CDS Views( Analytical & external API),

      Thanks for your valuable inputs.

      Author's profile photo Simon Kingston
      Simon Kingston

      Thanks for the really useful information.

       

      Can I ask one question?

       

      When I try to execute the API via Chrome, it sends me to the IDP for authentication. I cannot login via the IDP as it requires email address. Have I missed a piece of configuration somewhere?

      Kind regards

      Simon

      Author's profile photo Johannes Bacher
      Johannes Bacher

      Hello Simon, i have the same problem. Did you solve it?

      thank you,

      Johannes

      Author's profile photo Johannes Bacher
      Johannes Bacher

      Hello Akshay,

      I am having trhe same problem as Simon Kingston reported. When I try to use the API URL in the browser, i am redirected to the IDP, to the login screen which i normally get when accessing S/4HANA Cloud. And here I cannot login with the technical comm user.

      What could be wrong here?

      thank you,

      Johannes

      Author's profile photo Johannes Bacher
      Johannes Bacher

      I solved it. If you add ?saml2=disabled to the URL for calling the API, you are not redirected to the IDP but get the popup to enter user and pwd. Great!

      Author's profile photo Swastik Mukherjee
      Swastik Mukherjee

      HI Akshay,

      Thanks for the nice blog. I have an use case where i need to fetch some service entry sheet data in my SCP application. Till now I created an Node application and used sap open business api to get the data. I want to know is there any way i can call these exposed API from my node application like I am doing for open business API. it will solve my a lot of problems

       

      Thanks

      Swastik

      Author's profile photo Andreas Riehl
      Andreas Riehl

      Hello Akshay,

      Thanks for that detailed blog post!
      There is one thing, I would like to add: When such an external OData API is consumed, there is no server side paging. This is usually not a problem, but when the result set is large it may happen that the response times out. In this case the client that consumes the API has to take action and use appropriate filter and/or paging mechanisms ($skip, $top, $filter). For details, there is a very good documentation at odata.org on the URI conventions in OData version 2.0 (which is used here).

      Best Regards,
      Andreas

      Author's profile photo Atul A. Maslekar
      Atul A. Maslekar

      Hi Akshay,

       

      Can we use SAP HANA Studio to develop Custom CDS View for S4HANA Cloud system?

      If possible please guide me on same.

       

      Thank you.

       

      Author's profile photo Francisco Amores
      Francisco Amores

      Hello,

      great post

      is everything posted valid for S/4HANA Cloud Multi-tenant?

      Thanks!

      Author's profile photo Kapil Jain
      Kapil Jain

      How we can call this CDS with Filter values or this API with multiple filter?

      Author's profile photo Varaprasad Mummaleti
      Varaprasad Mummaleti

      Hello Akshay,

      i created CDS views for supplier

      I_ Supplier as primary data and _I_ Supplier Company as Associated Data source in this i want pan  number in preview  which element should i pick  can u explain it helps to me 

      ex: for GST i pick TAX Number3

      regards

      vara prasad