I recently had a problem to solve, where I had a to get more details about a specific sales order, where I just had the corresponding ID. In the standard view, there was no following association to this sales order (I_SalesOrder) so I had to find out, how to solve this problem.
What we have is a basic CDS View using the I_DeliveryDocumentItem as a base. We defining new names for it and also identify the sales order id, which is shown in line 15 – 16 below.
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Delivery Items'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define view entity ZC_Delivery as select from I_DeliveryDocumentItem
{
key DeliveryDocument as Doc,
key DeliveryDocumentItem as DocItem,
// Creating here the association from the field to an alias.
I_DeliveryDocumentItem._PurchaseOrderItem._PurOrdAcctAssignment.SalesOrder as SalesOrderPO
}
Now since we have the base view with the necessary sales order id renamed with SalesOrderPO, we can use a second CDS view to process this field further. We cannot do a same assocation inside the same view, since it has to process first the data for the first field and can only then calculate further, when the this CDS view has been completed.
In our second CDS (ZI_Delivery), we can now define the assocation to the I_SalesOrder and therefore use the SalesOrderPO from the previous CDS view.
Note that we have to reference to this field as well (shown in line 17 – 18) and then can later use it with the new alias _SalesOrderPO to use the sales order fields.
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Delivery Items completed association'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define view entity ZI_Delivery as select from ZC_Delivery
// Defining the association to the previous CDS view with the I_SalesOrder.
association [1..1] to I_SalesOrder as _SalesOrderPO on $projection.SalesOrderPO = _SalesOrderPO.SalesOrder
{
key Doc,
key DocItem,
// Embedding the field for the $protection keyword above.
SalesOrderPO,
// Now use the I_SalesOrder fields here.
_SalesOrderPO._Partner[ PartnerFunction = 'AG' ].Customer as Customer
}
Conclusion
So first of all, we see again that CDS views are very powerful when it comes to data structuring and definition. If a view cannot reference directly to an attribute in the same view, we can use the feature of inheritance.
Also note that this example is based on an example, where we had an Z-table in our base view (ZC_Delivery) and had an association to the delivery document items already. So since then an association to an association on the same view is not possible, we can still do it with this example.
Leave a Reply