You want to extend your CDS view entity with SQL logic. You use SQL functions to concatenate first name and last name of the department assistant, convert the monthly salary to a unique currency and calculate the company affiliation, that is, the number of days an employee works for the company.
Prerequisites
For this exercise, you need the CDS view entity that you worked on in the previous exercise (suggested name was: Z##_C_EmployeeQuery, where ## is your group number). If you have not finished the previous exercise, create a copy of data definition /LRN/C_EMPLOYEE_EXP.
Task 1: Use a String Function
In you CDS view entity for evaluating employee data (suggested name was: Z##_C_EmployeeQuery, where ## is your group number), adjust the definition of the AssistantName view element. Use a suitable string function to concatenate the first name and the last name of the department assistant, separated by a single space.
Steps
Edit the element list of your view entity. Comment the view element that derives the last name of the department assistant.
Place the cursor anywhere in the respective code row and press Ctrl + < to add a comment sign (//) at the beginning of the row.
After the old view element, use code completion to add a call of SQL function CONCATENATE_WITH_SPACE as a new view element.
In the code row // _Department._Assistant.LastName as AssistantName,, place the cursor after the colon and press Enter to insert a new row.
Enter con and press Ctrl + Space to invoke code-completion.
From the suggestion list, choose concat_with_space(arg1, arg,2, space_cnt) (function) and press Shift + Enter to insert the full signature.
Result
This should insert the following code:
123
concat_with_space( arg1, arg2, space_cnt )
Replace the placeholders with suitable view elements and literals.
Adjust the code as follows:
12345
concat_with_space( _Department._Assistant.FirstName,
_Department._Assistant.LastName,
1 )
Note
We inserted additional line breaks to increase readability in the printed course material.
Provide the same view element name that was used for the removed view element (suggested name was: AssistantName).
Adjust the code as follows:
12345
concat_with_space( _Department._Assistant.FirstName,
_Department._Assistant.LastName,
1 ) as AssistantName,
Activate and test the CDS view entity.
Press Ctrl + F3 to activate the development object.
Press F8 to test the CDS view entity in the Data Preview tool.
Task 2: Use a Date Function
In a new view element (suggested name: CompanyAffiliation), use a suitable date function to calculate the employee's company affiliation in days, that is, the number of days between the entry date and today. Then divide the result by the number of days per year to calculate the employee's company affiliation in years, with a precision of 1 decimal place.
Hint
The current date is available in session variables $session.user_date and $session.system_date. For this exercise, use the current system date.
For simplicity, disregard the existence of leap years and assume a year has 365 days.
Steps
Edit the element list of your view entity. Before the exposed association, use code completion to add a call of SQL function DATS_DAYS_BETWEEN as a new view element.
Place the cursor at the beginning of code row /* Associations */ and press Enter to insert a new row.
In the new code row, enter dats and press Ctrl + Space to invoke code-completion.
From the suggestion list, choose dats_days_between( date1, date2 ) (function) and press Shift + Enter to insert the full signature.
Result
This should insert the following code:
123
dats_days_between( date1, date2 )
Replace the placeholders with suitable view elements and session variables and specify a view element name (suggested name: CompanyAffiliation).
Adjust the code as follows:
1234
dats_days_between( EntryDate,
$session.system_date ) as CompanyAffiliation,
Note
Again, we inserted additional line breaks to increase readability in printed course material.
Use the SQL function division to calculate the company affiliation in years, with a precision of one decimal place.
Adjust the code as follows:
123456
division( dats_days_between( EntryDate,
$session.system_date ),
365,
1 ) as CompanyAffiliation,
Activate and test the CDS view entity.
Press Ctrl + F3 to activate the development object.
Press F8 to test the CDS view entity in the Data Preview tool.
Task 3: Perform a Currency Conversion
In a new view element (suggested name: AnnualSalaryConverted), use a suitable SQL function to convert the content of view element AnnualSalary to currency US Dollar. Use the current system date as exchange rate date. Then adjust the calculation of view element MonthlySalary. Use the converted annual salary as input instead of the unconverted annual salary.
Hint
You can re-use calculated view elements of the same view entity by using the $projection prefix.Steps
Remove or comment view element CurrencyCode.
Place the cursor in the code row CurrencyCode, and press Ctrl + < to add a comment sign (//) at the begin of the row.
In the next code row, define a new view element (suggested name: CurrencyCodeUSD) in which you convert literal 'USD' to predefined type abap.cuky.
Adjust the code as follows:
1234
// CurrencyCode,
cast( 'USD' as /dmo/currency_code ) as CurrencyCodeUSD,
Before the MonthlySalary view element, use code completion to insert a call of the SQL function currency_conversion.
In a new code row, enter curr and press Ctrl + Space to invoke code-completion.
From the suggestion list, choose the entry starting with currency_conversion( amount => amount, ... and press Shift + Enter to insert the full signature.
The code should now look as follows:
123456789101112
currency_conversion( amount => amount,
source_currency => source_currency ,
target_currency => target_currency,
exchange_rate_date => exchange_rate_date
)
@EndUserText.label: 'Monthly Salary'
@Semantics.amount.currencyCode: 'CurrencyCode'
cast( AnnualSalary as abap.fltp ) / 12.0 as MonthlySalary,
Note
We inserted additional line breaks, again.
Replace the placeholders with values for the parameters. As input, use the annual salary and the currency code from the source view, own view element CurrencyCodeUSD, and system variable system_date .
Adjust the code as follows:
1234567
currency_conversion( amount => AnnualSalary,
source_currency => CurrencyCode ,
target_currency => $projection.CurrencyCodeUSD,
exchange_rate_date => $session.system_date
)
Add an element name (suggested name: AnnualSalaryConverted), a label, and the mandatory Semantics.amount.CurrencyCode annotation.
Adjust the code as follows:
123456789
@EndUserText.label: 'Annual Salary'
@Semantics.amount.currencyCode: 'CurrencyCodeUSD'
currency_conversion( amount => AnnualSalary,
source_currency => CurrencyCode ,
target_currency => $projection.CurrencyCodeUSD,
exchange_rate_date => $session.system_date
) as AnnualSalaryConverted,
Adjust the calculation of view element MonthlySalary. Instead of AnnualSalary, use the new view element AnnualSalaryConverted as input.
Adjust the code as follows:
123456
@EndUserText.label: 'Monthly Salary'
@Semantics.amount.currencyCode: 'CurrencyCode'
cast( $projection.AnnualSalaryConverted as abap.fltp )
/ 12.0 as MonthlySalary,
Adjust the view element name (suggested name: MonthlySalaryConverted) and adjust the mandatory Semantics.amount.CurrencyCode annotation.
Adjust the code as follows:
123456
@EndUserText.label: 'Monthly Salary'
@Semantics.amount.currencyCode: 'CurrencyCodeUSD'
cast( $projection.AnnualSalaryConverted as abap.fltp )
/ 12.0 as MonthlySalaryConverted,
Activate and test the CDS view entity.
Press Ctrl + F3 to activate the development object.
Press F8 to test the CDS view entity in the Data Preview tool.