It took me a long time to figure out, how to use exactly the connected fields annotation in ABAP CDS definition. In CAP CDS it was somehow clearer to unterstand, how to combine two fields in a template and then getting these values in a sub annotation.
There is an active SAP Help page to this topic, but unfortunately, it does not exactly describe, how to use it certainly.
The first thing, we must create is a data definition, where we can later add our metadata extension on it. To do this, we create a definition like this:
@AccessControl.authorizationCheck: #NOT_REQUIRED
@Metadata.allowExtensions: true
@EndUserText.label: 'Data definition for portfolio transactions'
define root view entity ZPORTFOLIO_TRANS_I as select from zportfolio_trans
association to ZPORTFOLIO_I as _Portfolio on $projection.Isin = _Portfolio.Isin
{
key id as Id,
@Consumption.valueHelpDefinition: [
{
entity: { name: 'ZPORTFOLIO_I', element: 'Isin'}
}
]
isin as Isin,
// We later want to connect these two fields together (Amount and AmountUnit)
amount as Amount,
amount_unit as AmountUnit,
stock_price as StockPrice,
commision_price as CommisionPrice,
tax_price as TaxPrice,
total_price as TotalPrice,
currency as Currency,
// Association to the stock information
_Portfolio as _StockInfo,
@Semantics.user.createdBy: true
local_created_by as LocalCreatedBy,
@Semantics.systemDateTime.createdAt: true
local_created_at as LocalCreatedAt,
@Semantics.user.localInstanceLastChangedBy: true
local_last_changed_by as LocalLastChangedBy,
@Semantics.systemDateTime.localInstanceLastChangedAt: true
local_last_changed_at as LocalLastChangedAt,
@Semantics.systemDateTime.lastChangedAt: true
last_changed_at as LastChangedAt
}
Now we attach a metadata extension for our identification UI.
@Metadata.layer: #CUSTOMER
annotate view ZPORTFOLIO_TRANS_I with
{
@UI.facet: [ {
id: 'idIdentification',
type: #IDENTIFICATION_REFERENCE,
label: 'Transaction',
position: 10
} ]
@UI.identification: [{ position: 10, label: 'ID' }]
Id;
@UI.identification: [{ position: 20, label: 'ISIN' }]
Isin;
// The connected field starts here
@UI.connectedFields: [{
importance: #HIGH,
name: 'Amount', // Defining the name for the template
// The common identifier with the other connected field
qualifier: 'ConnectedAmountUnit',
template: '{Amount} {Unit}', // Our template for the new field
groupLabel: 'Amount of shares / Unit'
}]
@UI.identification: [{
// We replace the Amount value with the connected field construct
type: #AS_CONNECTED_FIELDS,
valueQualifier: 'ConnectedAmountUnit',
position: 30
}]
Amount;
@UI.connectedFields: [{
name: 'Unit', // Defining the name for the template
// The common identifier with the other connected field
qualifier: 'ConnectedAmountUnit'
}]
@UI.identification: [{ position: 40, hidden: true }] // Additionally, we hide the field
AmountUnit;
@UI.identification: [{ position: 50, label: 'Share price' }]
StockPrice;
@UI.identification: [{ position: 60, label: 'Commision fee' }]
CommisionPrice;
@UI.identification: [{ position: 70, label: 'Tax' }]
TaxPrice;
@UI.identification: [{ position: 80, label: 'Total price' }]
TotalPrice;
@UI.identification: [{ position: 90, label: 'Currency' }]
Currency;
}
As a result, we get instead of just the amount in the amount field, also the amount unit, which is now placed directly behind the amount field:

I hope may this small “tutorial” show cased a real example of how to use this annotation. We can now also use the currency for the other price tags to combine here the fields as well.
Leave a Reply