0% found this document useful (0 votes)
37 views

Virtual Elements in CDS Views

Virtual Elements in CDS ,how they are derived from class at runtime

Uploaded by

anand2201206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Virtual Elements in CDS Views

Virtual Elements in CDS ,how they are derived from class at runtime

Uploaded by

anand2201206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Virtual Elements in CDS Views

Sap Gateway: Rap Services or OData. Publish

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

Virtual Elements in CDS Views


 Virtual elements are defined in projection views as additional fields that do not exist in the
database.
 These fields are calculated dynamically at runtime, and their values are not provided by the
database layer. Instead, they are used to display data in the user interface through
calculations, without being retrieved from the database.
 To implement this functionality, the interface IF_SADL_EXIT_CALC_ELEMENT_READ needs
to be added to the implementation class.
 The interface contains two methods, GET_CALCULATION_INFO and CALCULATE. Include the
following annotation in the CDS view for the virtual element
@ObjectModel.virtualElementCalculatedBy: 'ABAP:<Class_Name>'.
 This Abap class data will not be appeared in CDS , this can be Seen in run time.

 Requirement
Add a column named "Item Count" to represent the number of items contained in a purchase order
within the application.

 Add the Virtual Element in Projection View


Add the 'Item Count' element in the projection view and define it as a virtual element. Additionally, add
1|P ag e
Virtual Elements in CDS Views

Sap Gateway: Rap Services or OData. Publish


Implementation class for the virtual element to calculate its value.
@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'

// This Syntax or below Syntax


@ObjectModel.virtualElement: truecan be used
// // as @ObjectModel.virtualElement:
cast ('' abap.char( 4 )) as ItemCount,true
// cast ('' as abap.char( 4 )) as ItemCount,

virtual ItemCount : abap.char(4),

/* Associations */
_PO_items : redirected to composition child ZC_PO_ITEMS

2|P ag e
Virtual Elements in CDS Views

Sap Gateway: Rap Services or OData. Publish


 Implementation Class for Virtual Element
Implement the CALCULATE method to determine the number of items associated with a purchase
order.

CLASS zcl_item_count_ve DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.

INTERFACES if_sadl_exit_calc_element_read .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS .

CLASS zcl_item_count_ve IMPLEMENTATION.

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.

CHECK NOT it_original_data IS INITIAL.


MOVE-CORRESPONDING it_original_data TO it_po_details.
SELECT * FROM ztb_po_items
FOR ALL ENTRIES IN @it_po_details WHERE po_num EQ @it_po_details-PoNum INTO
TABLE @DATA(it_po_items).
IF sy-subrc EQ 0.
LOOP AT it_po_details ASSIGNING FIELD-SYMBOL(<lfs_po>).

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

Sap Gateway: Rap Services or OData. Publish


 Add element in Metadata Extension
Add the element in the metadata extension to make it available for display on the UI

@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

Sap Gateway: Rap Services or OData. Publish


},

{
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;

@UI.lineItem: [{ position: 30,label: 'Order Type', importance: #HIGH }]


@UI.fieldGroup: [{ position: 30, label: 'Order Type',qualifier: 'BasicGroup' }]
Type;

@UI.lineItem: [{ position: 40,label: 'Company Code', importance: #HIGH }]


@UI.fieldGroup: [{ position: 40, label: 'Company Code',qualifier: 'OrgGroup' }]
CompCode;

@UI.lineItem: [{ position: 50,label: 'Organization', importance: #HIGH }]


@UI.fieldGroup: [{ position: 50, label: 'Organization',qualifier: 'OrgGroup' }]
Org;

@UI.lineItem: [{ position: 60, label: 'Vendor' }]


@UI.selectionField: [{ position: 30 }]
@UI.fieldGroup: [{ position: 60, label: 'Vendor',qualifier: 'OrgGroup' }]
Vendor;

@UI.lineItem: [{ position: 70, label: 'Plant' }]


@UI.selectionField: [{ position: 70 }]

@UI.fieldGroup: [{ position: 70, label: 'Plant',qualifier:


'OrgGroup' }] Plant;

@UI.lineItem: [{ position: 80,label: 'Status'},{ type: #FOR_ACTION, dataAction:


'Change_status' ,
label: 'Change
Status'}]
@UI.fieldGroup: [{ position: 80, label: 'Purchase Status',qualifier: 'BasicGroup'
}]

5|P ag e
Virtual Elements in CDS Views

Sap Gateway: Rap Services or OData. Publish


Status;

@UI.fieldGroup: [{ position: 90, label: 'Creation Date',qualifier: 'MoreDet' }]


CreatedDateTime;

@UI.lineItem: [{ position: 100,label: 'No of Items'}]


@UI.identification: [{ position: 100,label: 'No of Items' }]
ItemCount;

}
 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

You might also like