You want to extend CDS view entities with additional elements and associations. Instead of editing the data definitions directly, you create CDS view extensions.
Note
In this exercise, you extend your own CDS view entities Z##_R_Employee and Z##_C_EmployeeQueryP, where ## is your group number. This is done for simplicity. To motivate the use of extensions, you have to pretend that the view entities lie in a different name space and that you are not authorized to make direct changes to them.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.
Task 1: Enable Extensibility
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 extension of the element list with elements that come from your base view entity (suggested name was Z##_R_Employee).
Steps
Open the definition of your base view entity for employee data.
Press Ctrl + Shift + A, enter the name of the data definition and choose OK.
Adjust the view extension category to allow the extension of the element list.
Change the value of annotation @ABAPCatalog.viewEnhancementCategory from #NONE to #PROJECTION_LIST.
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).Add the @ABAPCatalog.extensibility.dataSources annotation as follows:
1234567
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: [#PROJECTION_LIST],
extensibility.dataSources: [ 'Employee' ]
}
Define the alias for the data source as follows:
1234
define view entity Z##_R_Employee
as select from z##employ as Employee
Define a mandatory suffix for the additional fields (suggested value: ZEM).
Add annotation @ABAPCatalog.extensibility.elementSuffix as follows:
12345678
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: [#PROJECTION_LIST],
extensibility.dataSources: [ 'Employee' ],
extensibility.elementSuffix: 'ZEM'
}
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Open the definition of your view entity with parameters for employee data.
Press Ctrl + Shift + A, enter the name of the data definition and choose OK.
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.Adjust the code as follows:
12345678
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: [#PROJECTION_LIST],
extensibility.dataSources: [ 'Employee' ],
extensibility.elementSuffix: 'ZEM'
}
Define the alias for the data source as follows:
123456789
define view entity Z##_C_EmployeeQueryP
with parameters
p_target_curr : /dmo/currency_code,
@EndUserText.label: 'Date of evaluation'
@Environment.systemField: #SYSTEM_DATE
p_date : abap.dats
as select from Z##_R_Employee as Employee
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Task 2: Extend the Base View Entity
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
Create a CDS view extension for your CDS view entity Z##_R_Employee.
In the Project Explorer, locate data definition Z##_R_EMPLOYEE, right-click it and choose New Data Definition.
Ensure that the Package field contains the name of your own package.
Enter Z##EXT_R_Employee in Name and Extension of Z##_R_Employee in Description. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.From the list of Templates, choose Extend View Entity and choose Finish.
Edit the element list of the view extension. Remove the placeholder and use code-completion to add the employee's title and country.
In the source code, replace base_data_source_code.element_name. with zz and press Ctrl + Space to invoke code-completion.
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.
Repeat to adjust the code as follows:
1234567
extend view entity Z##_R_Employee with
{
employ.zztitle_zem,
employ.zzcountry_zem
}
Define alias names for the new view elements (suggested names: ZZTitleZem and ZZCoutryZem).
Adjust the code as follows:
1234567
extend view entity Z##_R_Employee with
{
employ.zztitle_zem as ZZTitleZem,
employ.zzcountry_zem as ZZCountryZem
}
Activate the view extension.
Press Ctrl + F3 to activate the development object.
Display the Tooltip Description for the extended view entity to confirm that it contains the new elements.
In the code row that begins with extend view entity, place the cursor on z##_R_Employee and press F2.
Task 3: Extend the Query View Entity
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
Create a CDS view extension for your CDS view entity Z##_C_EmployeeQueryP.
In the Project Explorer, locate data definition Z##_C_EMPLOYEEQUERYP, right-click it and choose New Data Definition.
Ensure that the Package field contains the name of your own package.
Enter Z##EXT_C_Employee in Name and Extension of Z##_C_EmployeeQueryP in Description. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.From the list of Templates, choose Extend View Entity and choose Finish.
Edit the element list of the view extension. Remove the placeholder and use code-completion to add the employee's title and country.
In the source code, replace base_data_source_code.element_name. with zz and press Ctrl + Space to invoke code-completion.
From the suggestion list, choose ZZTitleZem - Z##_R_Employ as Employee (column) to add the extension field to the element list.
Repeat to adjust the code as follows:
1234567
extend view entity Z##_C_EmployeeQueryP with
{
Employee.ZZTitleZem,
Employee.ZZContryZem
}
Use the concat_with_space function to concatenate the employee's first name and last name, separated by a single blank.
Adjust the code as follows:
123456789
extend view entity Z##_C_EmployeeQueryP with
{
Employee.ZZTitleZem,
Employee.ZZContryZem,
concat_with_space( Employee.FirstName, Employee.LastName, 1 )
}
Add a name for this new element (suggested name: ZZFullNameZem) and use the relevant annotation to provide a label.
Adjust the code as follows:
12345678910
extend view entity Z##_C_EmployeeQueryP with
{
Employee.ZZTitleZem,
Employee.ZZContryZem,
@endUserText.label: '
concat_with_space( Employee.FirstName, Employee.LastName, 1 ) as ZZFullNameZem
}
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.Adjust the code as follows:
12345678910111213
extend view entity Z##_C_EmployeeQueryP with
association [1..1] to I_Country as _ZZCountryZem
on $projection.ZZCountryZem = _ZZCountryZem.Country
{
Employee.ZZTitleZem,
Employee.ZZContryZem,
concat_with_space( Employee.FirstName,
Employee.LastName,
1 ) as ZZFullNameZem
}
In the element list, add the IsEuropeanUnionMember element of the association target (suggested name: ZZEUBasedZem).
Adjust the code as follows:
123456789101112131415
extend view entity Z##_C_EmployeeQueryP with
association [1..1] to I_Country as _ZZCountryZem
on $projection.ZZCountryZem = _ZZCountryZem.Country
{
Employee.ZZTitleZem,
Employee.ZZContryZem,
concat_with_space( Employee.FirstName,
Employee.LastName,
1 ) as ZZFullNameZem,
_ZZCountryZem.IsEuropeanUnionMember as ZZEUBasedZem
}
Activate the view extension and display the Tooltip Description for the extended view entity to confirm that it contains the new elements.
Press Ctrl + F3 to activate the development object.
In the code row that begins with extend view entity, place the cursor on z##_C_EmployeeQueryP and press F2.