Virtual Elements in CDS Views
Virtual Elements in CDS Views
Contents
Virtual Elements in CDS Views.................................................................................................................. 1
1. Requirement ................................................................................................................................ 1
2. Add the Virtual Element in Projection View................................................................................... 1
3. Implementation Class for Virtual Element ..................................................................................... 3
4. Add element in Metadata Extension ............................................................................................. 4
5. Preview the Application................................................................................................................ 6
6. References ................................................................................................................................... 7
Requirement
Add a column named "Item Count" to represent the number of items contained in a purchase order
within the application.
@UI:{ headerInfo: {
typeName: 'Purchase Order',
typeNamePlural: 'Purchase Orders',
title: {
type: #STANDARD,
value: 'PoNum'
}
} }
define root view entity ZC_PO_HEAD
provider contract transactional_query
as projection on ZI_PO_HEAD
{
@Search.defaultSearchElement: true
key PoNum,
DocCat,
Type,
CompCode,
Org,
Status,
@Consumption.valueHelpDefinition: [{entity: {name: 'ZI_VEND', element:
'Vendor'}}]
Vendor,
Plant,
CreateBy,
CreatedDateTime,
ChangedDateTime,
LocalLastChangedBy,
@ObjectModel.virtualElementCalculatedBy: 'ABAP:ZCL_ITEM_COUNT_VE'
/* Associations */
_PO_items : redirected to composition child ZC_PO_ITEMS
2|P ag e
Virtual Elements in CDS Views
PUBLIC SECTION.
INTERFACES if_sadl_exit_calc_element_read .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS .
METHOD if_sadl_exit_calc_element_read~calculate.
DATA: it_po_details TYPE TABLE OF zc_po_head,
it_count type tABLE of ztb_po_items.
it_count[] = it_po_items[].
DELETE it_count WHERE po_num NE <lfs_po>-ponum.
DATA(lv_count) = lines( it_count ).
<lfs_po>-ItemCount = lv_count.
clear lv_count.
ENDLOOP.
MOVE-CORRESPONDING it_po_details TO ct_calculated_data.
ENDIF.
ENDMETHOD.
METHOD if_sadl_exit_calc_element_read~get_calculation_info.
ENDMETHOD.
ENDCLASS.
3|P ag e
Virtual Elements in CDS Views
@Metadata.layer: #CUSTOMER
@UI:{ headerInfo: {
typeName: 'Purchase Order',
typeNamePlural: 'Purchase Orders',
title: {
type: #STANDARD,
value: 'PoNum'
}
} }
annotate entity ZC_PO_HEAD with
{
@UI.facet: [{
id: 'POHd',
purpose: #STANDARD,
position: 10,
//type:#IDENTIFICATION_REFERENCE,
type:#COLLECTION,
label: 'Purchase Order Header'
},
{
id: 'BasicInfo',
purpose:#STANDARD,
parentId: 'POHd',
position:10,
targetQualifier: 'BasicGroup',
type:#FIELDGROUP_REFERENCE,
label:'Basic Details'
},
{
id: 'OrgInfo',
purpose:#STANDARD,
parentId: 'POHd',
position:20,
targetQualifier: 'OrgGroup',
type:#FIELDGROUP_REFERENCE,
label:'Organization Details'
},
{
id: 'MorDet',
purpose:#STANDARD,
parentId: 'POHd',
position:30,
targetQualifier: 'MoreDet',
type:#FIELDGROUP_REFERENCE,
label:'More Details'
4|P ag e
Virtual Elements in CDS Views
{
id:'Header1',
purpose: #HEADER,
type:#DATAPOINT_REFERENCE,
targetQualifier:'HD',
position: 10
},
{
id: 'POItems',
purpose: #STANDARD,
type:#LINEITEM_REFERENCE,
position: 20,
targetElement: '_PO_items',
label: 'Purchase Order Items'
}
]
@UI.lineItem: [{ position: 10,label: 'Purchase Order'}]
@UI.selectionField: [{ position: 10 }]
@UI.fieldGroup: [{ position: 10, label: 'Purchase Order',qualifier: 'BasicGroup' }]
@UI.dataPoint:{ title:'Purchase Order', qualifier: 'HD' }
PoNum;
5|P ag e
Virtual Elements in CDS Views
}
Preview the Application
Preview the application to verify the data of the newly added element
References
https://help.sap.com/docs/ABAP_PLATFORM_NEW/cc0c305d2fab47bd808adcad3ca7ee9d/a7fc007921
d44263b09ccc092392b05f.html
6|P ag e