cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a computed Field in a CAP

0 Kudos

Hi everyone,

I'm currently working on a very simple CAP Service for exam purposes next month and have a question regarding creating a Field which is calculated from values of other fields.

Here’s what I'm working with:

My schema is very simple (needs to be for this exam) --> I'm looking to have the field totalFree in entity "Place" calculated as the sum of all currentlyFree from the entity "Zone".

So if I have four Zone entities and each has the value 10 in "currentlyFree" "totalFree" in Place will be calculated and be 40.

I've done a bit of research and found this Blog Post regarding the issue. I'm just not sure how to do this in my case.

Here is my cat-service.cds file, where I presume the changes would have to be made.

Any help would be appreciated 🙂

Greetings

Jaden

Accepted Solutions (1)

Accepted Solutions (1)

hjb
Advisor
Advisor
0 Kudos

Hi, we do not support calculated elements in entities (yet). Please do your calculations in views.

This is basically a variation of DJs blogpost: Replace 'null' with your expression:

entity E { 
key id: Integer; Price: Integer; Quantity: Integer;
};
view V as select from E
{
Price,
Price * Quantity as PriceQuantity: Integer
};

For more complex expressions CASE may be your friend (but I don't want to spoil all the fun with your exam 😉

Regards, Hans-Joachim

Hey Hans-Joachim,

Figured it out works like a charm now.

Thanks for the help!

Answers (0)