Virtual Elements in CDS Views 1731724717
Virtual Elements in CDS Views 1731724717
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
1. Requirement
Add a column named "Item Count" to represent the number of items contained in a purchase order
within the application.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Projection view for PO Head'
@Metadata.ignorePropagatedAnnotations: true
@Metadata.allowExtensions: true
@Search.searchable: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
@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'
// @ObjectModel.virtualElement: true
// cast ('' as abap.char( 4 )) as ItemCount,
virtual ItemCount : abap.char(4),
/* Associations */
_PO_items : redirected to composition child ZC_PO_ITEMS
PUBLIC SECTION.
* INTERFACES if_sadl_exit .
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.
@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'
},
{
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'
}
]
6. References
https://help.sap.com/docs/ABAP_PLATFORM_NEW/cc0c305d2fab47bd808adcad3ca7ee9d/a7fc007921
d44263b09ccc092392b05f.html