Learning SAP Extend CDS Views Using Extend View Entity
Learning SAP Extend CDS Views Using Extend View Entity
Tags
Objectives
Note
The CDS view entity extension has to specify the data source name when addressing fields or
elements. In the example, it uses Agency which is defined in CDS view entity /DMO/E_Agency as an
alias name for database table /DMO/AGENCY.
In this example, the CDS view extension defines association _ZZTextZAG and adds it to the element
list of CDS view entity /DMO/E_Agency.
Note
It is not possible to distribute the quota share between customers and partners.
You can define an elementSuffix and not a fieldSuffix, because the element list of a CDS entity
can consist of more than just fields. Exposed associations are an example.
The most important difference are subannotations allowNewDatasources and dataSources, which
have no equivalent in the ABAP dictionary. With these annotations, the owner of a CDS view entity
can control from where the extensions can read their data. If allowNewDatasources is set to true,
the developer of an extension can read from any data source the original view reads from. If it is set
to false, the developer of the extension can only read from the data sources that are listed
under dataSources. This can be the direct data source(s) of the original view or the targets of its
associations.
SAP often uses this to enforce the use of extension includes, which are a best practice for CDS view
extensions.
On the figure, the CDS view definition on the top left contains an association to the extension include
view on the bottom left. Both CDS views are extendable, but to enforce the use of the E-view, the
main view restricts extensibility to elements of the association to the E-view. The E-view on the
other hand, allows extension with the elements of the main data source. As a consequence,
extension developers, have to add new elements to the E-view first, before they can add them to the
main view.
It is not possible to do the extension directly, because CDS view entity /DMO/R_AgencyTP restricts
the extension to association _Extension, which has CDS view entity /DMO/E_Agency as target
(bottom left).
Therefore, in a first step, the developer extends /DMO/E_Agency (bottom right) and only then
extends /DMO/R_AgencyTP addressing element ZZCategoryZAG through a path expression.
1. In the project explorer, locate the data definition of the CDS entity that you want to extend.
3. Adjust the package, enter a name and a description for the new object. Then choose Next.
Note
Usually, the data definition with the extension does not lie in the same package as the base
object. Therefore, it is important to adjust the suggested package.
5. From the list of templates, choose Extend View Entity and choose Finish.
There is also an Extend View template. It belongs to an older generation of CDS views. Only use
this template when you have to extend a view that is defined with define view and not with define
view entity .
Note
Template:
n.a.
Solution:
Prerequisites
For this exercise, you need the base view entity for employee data (suggested name
was: Z##_R_Employee) that developed in previous exercises. If you have not finished those
exercises, create a copy of data definition /LRN/R_EMPLOYEE_AUT.
In addition, you need the view entity with parameters for employee data (suggested name
was: Z##_C_EmployeeQueryP) . If you have not finished the related exercises, create a copy of data
definition /LRN/C_EMPLOYEE_AUT and let it read from your base view entity.
Adjust the extensibility settings of your base view entity for employee data (suggested name
was: Z##_R_Employee , where ## is your group number). Allow the extension of the element list
with fields that come from your database table for employee data (suggested name
was Z##_EMPLOY).
In the same way, adjust the extensibility settings of your view entity with parameters for employee
data (suggested name was: Z##_C_EmployeeQueryP , where ## is your group number). Allow the
Steps
1. Open the definition of your base view entity for employee data.
a. Press Ctrl + Shift + A, enter the name of the data definition and choose OK.
2. Adjust the view extension category to allow the extension of the element list.
3. Maintain the list of allowed data sources for the extension. Add the database table from which
the view entity reads to this list.
Note
To add the database table to the list, you need to define an alias name for it (suggested alias
name: Employee).
Copy code
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: []
}
Code snippet
Copy code
4. Define a mandatory suffix for the additional fields (suggested value: ZEM).
Code snippet
Copy code
6. Open the definition of your view entity with parameters for employee data.
a. Press Ctrl + Shift + A, enter the name of the data definition and choose OK.
7. As before, adjust the view extension category, maintain the allowed data sources, and define the
element suffix.
Note
Use the same data source alias and element suffix as before.
Code snippet
Copy code
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: []
}
Code snippet
Copy code
For your base view entity, create a CDS view extension (suggested name: Z##EXT_R_EMPLOYEE) to
add the fields for the employee's title and the country to the element list (suggested names
were: ZZTITLE_ZEM and ZZCOUNTRY_ZEM).
Steps
1. Create a CDS view extension for your CDS view entity Z##_R_Employee.
b. Ensure that the Package field contains the name of your own package.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
e. From the list of Templates, choose Extend View Entity and choose Finish.
2. Edit the element list of the view extension. Remove the placeholder and use code-completion to
add the employee's title and country.
b. From the suggestion list, choose zztitle_zem- z##employ as employee (column) to add the
append field to the element list of the CDS view extension.
Code snippet
Copy code
Code snippet
Copy code
5. Display the Tooltip Description for the extended view entity to confirm that it contains the new
elements.
a. In the code row that begins with extend view entity , place the cursor on z##_R_Employee and
press F2.
For your query view entity with parameters, create a CDS view extension (suggested
name: Z##EXT_C_EMPLOYEE) to add the elements that you added to the base view (suggested
elements names were: ZZTitleZem and ZZCountryZem). Also add an SQL function to concatenate
the employee's first name and last name, separated by a single blank (suggested element
name: ZZFullNameZem). Finally, extend the view with a flag that tells you if the employee is based
in the European Union or not (suggested element name: ZZEUBasedZem).
Hint
The country master data contain a flag IsEuropeanUnionMember. To access this element, you first
have to extend the view with an association to CDS view entity I_Country (suggested association
name: _ZZCountryZem). Then add a path expression to the element list of the view extension.
Steps
1. Create a CDS view extension for your CDS view entity Z##_C_EmployeeQueryP.
b. Ensure that the Package field contains the name of your own package.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
e. From the list of Templates, choose Extend View Entity and choose Finish.
2. Edit the element list of the view extension. Remove the placeholder and use code-completion to
add the employee's title and country.
b. From the suggestion list, choose ZZTitleZem - Z##_R_Employ as Employee (column) to add
the extension field to the element list.
Code snippet
Copy code
3. Use the concat_with_space function to concatenate the employee's first name and last name,
separated by a single blank.
Code snippet
Copy code
4. Add a name for this new element (suggested name: ZZFullNameZem) and use the relevant
annotation to provide a label.
Code snippet
Copy code
@
concat_with_space( Employee.FirstName, Employee.LastName, 1 )
}
5. Extend the view with an association to CDS view entity I_Country (suggested
name: _ZZCountryZem).
Hint
In the ON condition, use the country code from the element list and the key element of view
entity I_Country.
Code snippet
Copy code
concat_with_space( Employee.FirstName,
6. In the element list, add the IsEuropeanUnionMember element of the association target
(suggested name: ZZEUBasedZem).
Code snippet
Copy code
concat_with_space( Employee.FirstName,
Employee.LastName,
1 ) as ZZFullNameZem
}
7. Activate the view extension and display the Tooltip Description for the extended view entity to
confirm that it contains the new elements.
b. In the code row that begins with extend view entity , place the cursor
on z##_C_EmployeeQueryP and press F2.