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 efficient way of data modelling.
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.
Note: These APIs are always read-only.
Now let’s take an example scenario where we want to see how many Purchase Contracts are used as 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’ check box 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 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.
In above mentioned blog while performing Step 3.3 we need to select the Communicaton 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 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.