cancel
Showing results for 
Search instead for 
Did you mean: 

How to calculate value for a virtual column using CDS annotations

former_member739728
Discoverer
0 Kudos

We have an entity TechnicalObjects as below:

entity TechnicalObjects {
...
virtual status : String(10);
...
isAtCustomer: Boolean;
isAvailable : Boolean;
isInWarehouse : Boolean;
isAssignedToDelivery : Boolean;
isMarkedForDeletion : Boolean;
isInstalled : Boolean;
isAllocToSuperiorEquip : Boolean;
isInactive : Boolean;
hasSubOrdinateEquipment : Boolean;
isDeleted : Boolean;
}
Only of the above boolean values will be TRUE. Value of status should be calculated based on which attr is true. For eg, if isAvailable = true, then status value should be AVAILABLE.

My question is:
Is this possible in ODATA v4? if yes, how do we write it using CDS annotations?

Accepted Solutions (0)

Answers (4)

Answers (4)

former_member739728
Discoverer
0 Kudos

Took below approach to resolve this issue. Thanks for the support guys!

https://github.wdf.sap.corp/fiori-elements/v4-consulting/issues/1173

maheshpalavalli
Active Contributor

That link will be accessible only to the sap employees, please share the solution by taking a screenshot or by copying the text.

maheshpalavalli
Active Contributor
0 Kudos

megha.kumari02 you can try creating a view like below using a case statement.

using my.bookshop as my from '../db/data-model';

service CatalogService {
    entity Books as select from my.Books {
     *,
     case when stock = 100 then true else false end as testfield : Boolean
  };
}

former_member194549
Contributor
0 Kudos

Hi megha.kumari02,

you can use a after read handler for calculating fields.
You can find an example in the CAP Community Repository.
dj.adams.sap has also written a blog post about it.

Regards
Simon

Patrick_vN
Active Contributor
0 Kudos

Not sure if you need annotations to do this, you could just use the CASE statement in your view?

CASE table.column_XXX
  WHEN 'X' THEN 'Positive label'
  ELSE 'Negative label'
END AS is_XXX

Or if only one of the columns can be true

CASE 
  WHEN table.column_01 = 'X' THEN 'Status 01'
  WHEN table.column_02 = 'X' THEN 'Status 02'
  WHEN table.column_03 = 'X' THEN 'Status 03'
  WHEN table.column_04 = 'X' THEN 'Status 04'
END AS overal_status