Technical Articles
Extending SAP ODM model using SAP CDS Graphical Modeler
In this blog post, we will demonstrate how to extend SAP ODM model using the SAP CDS Graphical Modeler and SAP Business Application Studio, and also show you how to generate the OData services based on the extended model through CDS projection.
In order to use the CDS Graphical Modeler in SAP Business Application Studio, we would need to create a dev space in “Full Stack Cloud Application” category. For the introduction of CDS Graphical Modeler and how to setup Business Application Studio dev space as well as creating the CAP project, please refer to https://blogs.sap.com/2021/04/23/an-introduction-to-cds-graphical-modeler-for-sap-business-application-studio.
Prepare Your CAP Project
So assuming that your CAP project has been created successfully already in the “Full Stack Cloud Application” dev space, we need to include SAP ODM model by adding ODM dependencies to the package.json file in the project.
Save the file and let’s build this project. Open a terminal through “Terminal” -> “New Terminal” menu of the application studio, and type “npm install” in the command line console. This will setup the project dependencies including the ODM module we want to extend.
After the build is successful, open db/data-model.cds using the CDS Graphical Modeler:
Create an Extension for ODM WorkforcePerson Entity
In order to create the extension of ODM model, we need to first import the ODM cds file to our data-model.cds in the project. Click the “Import” toolbar button and select the WorkforcePerson.cds under “node_modules/@sap/odm/dist/workforce” folder:
Click the “Select CDS File” dialog button to dismiss the dialog and you will see the import CDS dialog:
Accept the default and click “OK” button to finish the import. You can verify the db/data-model.cds using the code editor:
namespace my.bookshop;
using sap.odm.workforce from '@sap/odm/dist/workforce/WorkforcePerson';
entity Books
{
key ID : Integer;
title : String;
stock : Integer;
}
Now you see the using statement for the ODM WorkforcePerson has been successfully generated by the CDS Graphical Modeler.
Then we can create an extension for ODM WorkforcePerson entity by clicking the “Add extension” toolbar menu item:
And in the “create extension” dialog select the WorkforcePerson as the base type:
and in the “New Property” dialog, add 3 extended properties to this extension.
- ext__newstring_property
- ext__newint_property
- ext__newdouble_property
Close the dialog and you will see the extension now contains the extended properties you just created.
Now you can check if the extension has taken effective on the original WorkforcePerson entity in the ODM namespace. You can navigate to the “sap.odm.workforce” namespace:
Then you can see the 3 newly added properties have been included in the ODM WorkforcePerson entity.
We can now create a projection for the ODM WorkforcePerson entity in the srv/cat-service.cds. Open the srv/cat-service.cds file:
You can see that the Books projection already exists in the CatalogService.
Now let’s create a new service called “AdminService” where we want to create the projection. Click “New Service” toolbar button:
And then enter “AdminService” in the “New Service” dialog and click “Create” button to dismiss the dailog. CDS Graphical Modeler will load the newly created “AdminService”:
Import ODM WorkforcePerson.cds file for srv/cat-service.cds as well, the same way as what we do for db/data-model.cds:
Click “Select CDS File” and finish the import. Now let’s create a projection for the extended WorkforcePerson entity:
In the “New Projection” dialog, select “sap.odm.workforce.WorkforcePerson” as the projection base type. Please ensure to exclude “personalDetail” and “profileDetail” while creating the projection to avoid an odata service generation error for this version of ODM.
Click “Create” and finish creating the projection. Now you should be able to see the newly created projection in AdminService.
Please also note that the 3 newly added properties have shown up in the projection. This is expected because cat-service.cds includes db/data-model.cds where we have already extended the ODM WorkforcePerson entity through a CDS extension.
Run a Local OData Service
Now let’s generate a local odata service and check its odata metadata to ensure everything is working properly. Open a terminal again and type “cds watch” and press enter.
After the service is generated you will see below popup dialog to indicate the exposed port information in the SAP Business Application Studio.
Click “Expose and Open” button to dismiss this dialog. Now you should be able to see a new browser tab is opened that allows you to check the odata metadata.
Click “/admin/$metadata” URL and check the OData metadata. Now locate the “WorkforcePerson” entity type in the OData metadata document, you should be able to see the 3 newly added properties in the “WorkforcePerson” entity type:
With this odata services, we have extended the original ODM WorkforcePerson entity successfully by adding 3 additional new properties, and the user can build the frontend application to consume this extended odata model now.
References
An introduction to CDS Graphical Modeler for SAP Business Application Studio
Creating CDS projections using the CDS Graphical Modeler
Importing CDS file using the CDS Graphical Modeler
Conclusion
As a summary, in this blog post we first show you how to import the ODM model into the CAP project and how to import the ODM WorkforcePerson entity into the data model for this project. And then we demonstrate how to create an extension for the ODM WorkforcePerson, and then create a projection on this extended ODM model in the newly created CDS service. Finally, we have demonstrated how to run a local OData service based on the CAP project through “cds watch” and show you the extended OData model in the OData metadata document.
this should go into the official help.sap.com under "odm best practices for s/4 extensions with CAP" 🙂
Thanks Voker. Appreciated your comments.
Hi ,
when i am using "@sap/odm": "2.0.0" in package.json, i am not getting some fields like "BusinessPartner".
and i didnt find odm 2.1.1 on NPM.
so is there any alternative to get "businesspartner" cds file.
thank you.
Hi Xiao-fei Song, nice blog!
There are not much info about hands-on guidance for development teams unfortunately.
We are trying to consume ODM entities in our CAP based project, but npm package isn't up-to-date on latest available version as of api hub content. Do you know how to retrive relevant version? We see a lot of changes since v.2
First I thought maybe it could be found in api hub directly, but there is only integrtion content ( So no acceleration to cds modelling.
The second questions do you have an idea about CodeLists content? Is it part of ODM or should be managed by developers?
I don't know if Rui Nogueira will join our discussion, but developers community needs some more info kicks-in to put our hands in dirt with ODM to create partners solutions rock-solid compatible with ODM. The idea is brilliant.
Thanks in advance!
Hi Aleksei,
It is nice to see your interest in ODM.
The teams are currently working on bringing the models and the APIs closer together, that they are both up-to-date. If you are working on CAP based projects, I am not sure if you are aware of the capabilities to import API definitions https://cap.cloud.sap/docs/guides/using-services#external-service-api, this might help you accelerate your cds modelling.
The code list topic is a bit more complicated, therefore there is no official statement I can share at the moment.
Hope that helps at least partially
Hi Aleksei, I've reached out to the colleagues from product management who are currently responsible for ODM. They'll respond in this thread.
Best,
Rui
Thanks, that would be great!
Dont wanna miss a strategic turn in our development project!
Regards,
Alexey
Hi Aleksei,Â
Thank you for reaching out to us.
Regarding your question about code list values, we do not deliver them with our NPM packages. We are assuming that customers have maintained different code list values in their system landscape which we cannot foresee. Therefore, we expect that you provide the values yourself based on what was maintained in your applications.
The development of further ODM versions is ongoing. However, I cannot give you any concrete release dates at the moment.
Regards
Nadine