Virtual Elements in CDS


In CDS View/Entity, the data source for the selection is either some persistent data model( may be DB table/ CDS view). Virtual element is some field you want to add to the consumption/projection model whose value can’t provided by the persistence layer but calculated by the ABAP.

A simple may be hypothetical use case- The sales order header view need to show the no.of line items it has.

The CDS view build on VBAK table.

Data Preview-

Add a virtual element and provide the necessary annotation and ABAP EXIT Class name.

Create a class and implement the interface- IF_SADL_EXIT_CALC_ELEMENT_READ

The two methods with signatures-

Do the preview, the class/method won;t trigger as it is executed on DB level.

Let’s make the view as a consumption view- make oData publish and register the service in gateway.

In tx- /IWFND/MAINT_SERVICE register the service-

Test the service in gateway client-

Service is working fine-

Provide the entity and execute GET request-

Here the SADL(query framework) calls the exit class of the virtual element-

So all record noofitems = 0 as we don’t have implemented any logic.

Let’s build our logic. Prepare a range table of sales order header and make selection on VBAP and then for each VBELN, get the no of lines and then we calculate our noofitems .

We got the output-

Debug instance-

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s