on 2021 Nov 02 4:54 PM
Hi,
Based on the CAP application examples (incl. cap-sflight), I combined the book and order examples to try the following: place an order for a book if it is in stock and reject it otherwise. I defined the following schema:
type StockStatusCode: String enum {
InStock = 'I',
OutOfStock = 'O'
};
entity StockStatuses: sap.common.CodeList {
key code: StockStatusCode;
}
type StockStatus: Association to one StockStatus;
entity Books {
[...]
status: StockStatus;
}
I defined my service as following:
service BookOrderService {
entity Orders as projection of my.Orders;
entity Books as projection of my.Books;
action placeOrder(book: Books:ID, quantity : Integer) returning { order: Orders:ID };
}
Finally, in my implementation, I'm trying to do something like:
const cds = require('@sap/cds');
class BookOrderService extends cds.ApplicationService {
async init() {
this.on("placeOrder", (req) => {
const book = [...]; // retrieve the book entity with stock status
if (book.status_code == StockStatusCode.OutofStock) {
req.reject(412, "Book cannot be ordered as it is not in stock");
} else {
// Logic to create the order
}
});
await super.init();
}
}
module.exports = {BookOrderService};
But this code does not compile because it cannot resolve StockStatusCode. Since it's a type and not an entity, it cannot be looked up via the service or cds API.
I tried to go the other way around by defining constants in JavaScript and referring to these constants in the CDS schema but that does not work as it seems not to be possible to import JavaScript in CDS.
Is there a way to use enum human-readable values in the code?
Hi gregor.wolf,
Sorry for coming so late back to you. I asked the product team and apparently, it is their plans to deliver it one day. I checked recently the release notes and it does not look like it has been released yet. But I found a nice workaround documented in my blog post.
-Alex
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
71 | |
11 | |
10 | |
10 | |
10 | |
8 | |
7 | |
7 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.