While researching the topic of the new data models introduced in SAP S/4HANA, I’ve noticed a limited availability of the technical information related to the “compatibility” ABAP CDS views, like the ones replacing the well-known MSEG and MKPF tables. And therefore, I would like to share some of my findings as a quick guide to creating your own “compatibility” ABAP CDS views.
For my own example I’ve taken an SFLIGHT and SBOOK tables, familiar to the most of you since your first steps in ABAP. Further in this article I’m going to work with an AA flight number 17 scheduled for 15-Dec-2016.
I’ve considered the following rules while creating the compatibility view for the SFLIGHT table:
- The number of components must match.
- The names of the components must match (except for the client column) and must be in the same order.
- The technical type properties predefined data type, length and number of decimal places must match for every component.
I’ve defined a view ZDP_V_SFLIGHT having the exact number of fields (14) with exact same names and technical properties as in the original SFLIGHT table. The following fields were replaced with the SBOOK table aggerates:
- SFLIGHT-PAYMENTSUM: Total amount of bookings in local currency (sum of SBOOK-LOCCURAM for the flight where SBOOK-CANCELLED is blank)
- SFLIGHT-SEATSOCC: Total seats occupied in economy class (count of SBOOK records for the flight where SBOOK-CANCELLED is blank and SBOOK-CLASS is “Y”)
- SFLIGHT-SEATSOCC_B: Total seats occupied in business class (count of SBOOK records for the flight where SBOOK-CANCELLED is blank and SBOOK-CLASS is “C”)
- SFLIGHT-SEATSOCC_F: Total seats occupied in first class (count of SBOOK records for the flight where SBOOK-CANCELLED is blank and SBOOK-CLASS is “F”)
On top of it, SFLIGHT-PAYMENTSUM and SBOOK-LOCCURAM fields have different technical properties – data type CURR of length 17 and CURR of length 15 accordingly. In order to make it compatible with the SFLIGHT-PAYMENTSUM field, I’ve converted it to S_SUM data element using the cast expression.
cast(sum(case sbook.cancelled when ” then sbook.loccuram else 0 end) as s_sum) as paymentsum
I’ve used similar approach for the generic fields with occupied seats values:
cast(sum(case sbook.cancelled when ” then case sbook.class when ‘Y’ then 1 else 0 end else 0 end) as s_seatsocc) as seatsocc
cast(sum(case sbook.cancelled when ” then case sbook.class when ‘C’ then 1 else 0 end else 0 end) as s_socc_b) as seatsocc_b
cast(sum(case sbook.cancelled when ” then case sbook.class when ‘F’ then 1 else 0 end else 0 end) as s_socc_f) as seatsocc_f
As you have noticed above, instead of using count expression, I’m using sum combined with the case distinction in order to consider the booking class and cancellations. You may also split such and other actions into a several CDS views and join them with the join expressions. In that case you will be also able to use the count expression together with the where condition.
In order to test the CDS view, I’ve cancelled a number of bookings in the SBOOK table by setting a cancellation flag into the SBOOK- CANCELLED field. After the SBOOK table changes have been saved, I’ve refreshed the SFLIGHT table and the ZDP_V_SFLIGHT table view outputs.
As expected, contents of the SFLIGHT table remain unchanged, while the ZDP_V_SFLIGHT view shows actual numbers.
The last step is assigning our newly created CDS view ZDP_V_SFLIGHT to the SFLIGHT table. I’m going to do it using a DD_SET_REDIRECT function module used by some of the SAP S/4HANA conversion programs. If there is a better/official way of doing this, you are welcome to share this information in the comments section.
When calling the function module, you have to fill the redirection map by specifying the following values:
- OBJECTNAME – Name of the ABAP Dictionary object
- OBJECTTYPE – ABAP Dictionary object type
- REPLACENAME – of the ABAP Dictionary replacement object
- REPLACETYPE – ABAP Dictionary replacement object type
- ACTION – Action, use “CRE” to create and “DEL” to remove the redirection to the replacement object
Leave a default value equal to “0” for the return code field. Depending from the result, different return code could be assigned by the function module.
Once the function module DD_SET_REDIRECT has been successfully executed, you will see the proxy object assignment in the ABAP Dictionary. When executing the SFLIGHT table queries, the aggregated values, calculated based on the SBOOK table contents, are displayed. Every time you update the SBOOK table, the change is also reflected when reading the SFLIGHT table.
To sum up, I would like to highlight several resources I’ve found useful while looking into understanding the way how the “compatibility” views and ABAP CDS views in general are made:
- Downloading and setting up Eclipse based ABAP Development Tools for SAP NetWeaver you would need to work with ABAP CDS views- https://tools.hana.ondemand.com/#abap
If you are new to Eclipse, I would mention that it utilizes your existing SAP connections and does not require any additional setup of your SAP system on top of it. Using Eclipse, you can also explore the code inside the existing compatibility views delivered by SAP as part of SAP S/4HANA package.
- Complete reference on ABAP CDS in ABAP Dictionary – https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/abencds.htm
- Set of rules and notes to consider when dealing with database table replacement objects – https://help.sap.com/http.svc/rc/abapdocu_750_index_htm/7.50/en-US/abenddic_replacement_objects.htm
- Collection of useful links to a various CDS related topics: https://wiki.scn.sap.com/wiki/display/ABAP/CDS+-+Core+Data+Services