CAA361 Compound
CAA361 Compound
Exercises / Solutions
Shyam Balachandran, Simon Collet, Volker Drees, Thomas
Gauweiler, Gopalakrishnan Ramachandran / SAP SE
CAA361
TABLE OF CONTENTS
2
CAA361
We’ll build the application step by step starting with exposing an OData service based on a
prepared Core Data Services model with annotations and display it in a List Report based on
Fiori Elements. Then we’ll enhance this model to a Business Object with CRUD (Create,
Read, Update, Delete) operations and business specific functionality.
Further reading:
Link: Developing New Managed Transactional Apps
System Information
Before you start, here is some technical information that you will need during the exercise.
You’ll work on an ABAP system on the SAP Cloud Platform, which is called SAP Cloud
Platform ABAP Environment.
The screenshots for this exercise have been prepared using group number 931 and system
Y08. The explanation in the following exercises will remind you to use your group number
(indicated by ###) on system T06. Moreover, you will create ABAP artifacts in package
ZCAA361_EX_###, while all screenshots show package ZCAA361_EX_931 for illustration
purposes.
You can find suggested solutions to the exercises in package ZCAA361_EX_SOL. As this is a
hands-on exercise, some of the exercises require a certain amount of coding. As we want to
focus on the “getting to know the end-to-end story”, you won’t need to type all that coding
yourself but rather copying the given prepared coding from this document or your user share.
Further reading:
Link: SAP Cloud Platform ABAP Environment
Business Scenario
3
CAA361
The Fiori App you are going to implement is based on the new sample scenario: The Flight
Reference Scenario for the ABAP RESTful Programming Model.
To set the business context the scenario is the following: The department responsible for
managing worldwide Travels for multiple Agencies is requesting you to build a new SAP Fiori
app for processing (i.e. creating, updating and deleting) Travels.
Data Model
4
CAA361
Further reading:
Link: Reference Business Scenario
Architecture Overview
The figure below illustrates the architecture components of the ABAP RESTful Programming
Model. It shows the main technologies needed to build an SAP Fiori Elements Application
based on the ABAP RESTful Programming Model. Afterwards you’ll find a description of
these components.
Further reading:
Link: ABAP RESTful Programming Model
Link: Concepts
5
CAA361
6
CAA361
Code Snippets
All the code blocks mentioned in this document are stored on the network share. To save
time and prevent typos, feel free to use them.
Connect to the system
An ABAP cloud project mediates between the ABAP backend for developing ABAP
applications in the context of ABAP Environment and ADT. Projects like these provide a
framework that enables you to create, process, and test development objects for SAP Cloud
Platform products.
Before you can start using ABAP Development Tools (ADT), you first need to create an
ABAP cloud project which represents the system connection to your ABAP backend.
Further reading:
Link: Working with ABAP Cloud Projects
Link: ABAP Cloud Projects
Exercise Description
• You'll be guided to the code snippets which are also mentioned in this document.
• Set up your ABAP Development Tool for Eclipse
Code Snippets
Explanation Screenshot
Student (Share) to
open it.
7
CAA361
Explanation Screenshot
Explanation Screenshot
8
CAA361
Explanation Screenshot
9
CAA361
Explanation Screenshot
5. Click Go to the
workbench .
10
CAA361
Explanation Screenshot
8. Click Open.
11
CAA361
Explanation Screenshot
11. Click .
12
CAA361
Explanation Screenshot
13. Go to studentshare...
13
CAA361
Explanation Screenshot
"clientid": "sb-xs-
40fc02fc-57b0-4eb9-
83e0-
2076807bb848!b17744|xsu
aa-abapcp-prod-
eu10!b4584",
"verificationkey": "-
----BEGIN PUBLIC KEY---
--
MIICIjANBgkqhkiG9w0BAQE
FAAOCAg8AMIICCgKCAgEAwT
hn6OO9kj0bchkOGkqYBnV1d
Q3zU/xtj7Kj7nDd8nyRMcEW
CtVzrzjzhiisRhlrzlRIEY8
2wRAZNGKMnw7cvCwNixcfcD
Jnjzgr2pJ+5/yDZUc0IXXyI
WPZD+XdL+0EogC3d4+fqyvg
/BF/F0t2hKHWr/UTXE6zrGh
BKaL0d8rKfYd6olGWigFd+3
+24CKI14zWVxUBtC+P9Fhng
c9DRzkXqhxOK/EKn0HzSgot
f5duq6Tmk9DCNM4sLW4+ERc
6xzrgbeEexakabvax/Az9WZ
4qhwgw+fwIhKIC7WLwCEJaR
sW4m7NKkv+eJR2LKYesuQ9S
VAJ3EXV86RwdnH4uAv7lQHs
KURPVAQBlranSqyQu0EXs2N
9OlWTxe+FyNkIvyZvoLrZl/
CdlYc8AKxRm5rn2/88nkrYQ
0XZSrnICM5FRWgVF2hn5KfZ
GwtBN85/D4Yck6B3ocMfyX7
e4URUm9lRPQFUJGTXaZnEIg
e0R159HUwhTN1HvyXrs6uT1
ZZmW+c3p47dw1+LmUf/hIf8
zd+uvHQjIeHEJqxjqfyA8yq
AFKRHKVFrwnwdMHIsRap2EK
BhHMfeVf0P2th5C9MggYoGC
vdIaIUgMBX3TtCdvGrcWML7
hnyS2zkrlA8SoKJnRcRF2Kx
WKs355FhpHpzqyZflO5l98+
O8wOsFjGpL9d0ECAwEAAQ==
-----END PUBLIC KEY----
-",
"apiurl":
"https://api.authentica
tion.eu10.hana.ondemand
.com",
"xsappname": "xs-
40fc02fc-57b0-4eb9-
83e0-
2076807bb848!b17744|xsu
aa-abapcp-prod-
eu10!b4584",
"identityzone":
14
CAA361
Explanation Screenshot
"caa361-teched2019",
"identityzoneid":
"ff0c7f17-6c0a-4cb2-
ac65-b4c5d76b84d4",
"clientsecret":
"0G2NHaYSWjbAgimKGb/+eT
sCVEI=",
"tenantid":
"ff0c7f17-6c0a-4cb2-
ac65-b4c5d76b84d4",
"url":
"https://caa361-
teched2019.authenticati
on.eu10.hana.ondemand.c
om"
},
"endpoints": {
"abap":
"https://40fc02fc-57b0-
4eb9-83e0-
2076807bb848.abap.eu10.
hana.ondemand.com"
},
"catalogs": {
"abap": {
"path":
"/sap/opu/odata/IWFND/C
ATALOGSERVICE;v=2",
"type":
"sap_abap_catalog_v1"
}
},
"binding": {
"env": "cf",
"version": "1.0.1.1",
"type": "oauth",
"id":
"h7BxbIXlaUXqmf0RyRtvvS
4snJnmjr9WDvoE"
}
}
15
CAA361
Explanation Screenshot
16
CAA361
Explanation Screenshot
17
CAA361
Explanation Screenshot
Summary
You have completed the exercise!
You are now able to:
• Access the prepared code snippets
• Connect your ABAP Development Tools in Eclipse to an ABAP Cloud Project
18
CAA361
BUSINESS SERVICES
Overview
Estimated time: 20 minutes
Definition
The ABAP development platform can act in the roles of service provider and service
consumer, such as SAP Fiori UI client.
In the context of the ABAP RESTful Programming Model, a business service is a RESTful
service which can be called by a consumer. It is defined by exposing data models and
behavior models. It consists of a Service Definition and a Service Binding.
Further reading:
Link: Business Service
Link: Creating an OData Service
19
CAA361
SERVICE DEFINITION
A Business Service Definition (short form: Service Definition) is an ABAP Repository object
that describes the consumer-specific perspective on a data model. Its transport type is
SRVD.
Use
A Service Definition represents the service model that is generically derived from the
underlying CDS-based data model. You use a Service Definition to define which data is to be
exposed (with the required granularity) as a Business Service. A Service Definition is
independent from the version or type of the protocol that is used for the Business Service;
the protocol is specified in a Business Service Binding (short form: Service Binding).
Further reading:
Link: Service Definition
Link: Creating a Service Definition
20
CAA361
SERVICE BINDING
The business service binding (short form: service binding) is an ABAP Repository object
used to bind a service definition to a client-server communication protocol such as OData.
Like any other repository object, the service binding uses the proven infrastructure of the
ABAP Workbench, including the transport functions. The transport type of a service binding
is SRVB.
Use
As shown in the figure below, a service binding relies directly on a service definition that is
derived from the underlying CDS-based data model. Based on an individual service
definition, a plurality of service bindings can be created. The separation between the service
definition and the service binding enables a service to integrate a variety of service protocols
without any kind of re-implementation. The services implemented in this way are based on a
separation of the service protocol from the actual business logic.
Further reading:
Link: Service Binding
Link: Creating a Service Binding
21
CAA361
Further reading:
Link: Previewing the Resulting UI Service
Link: UI Annotations
Exercise Description
• Create a Service Definition and expose the main entities of your service
• Create a Service Binding via a wizard
• Publish the service
• Have a look at the metadata document of the service
• Open the Fiori Elements App Preview and browse through your data
Explanation Screenshot
22
CAA361
Explanation Screenshot
3. Select it
4. Click OK
23
CAA361
Explanation Screenshot
24
CAA361
Explanation Screenshot
9. Click Next.
25
CAA361
Explanation Screenshot
expose
ZCAA361_C_Booking_###
as Booking;
26
CAA361
Explanation Screenshot
Explanation Screenshot
27
CAA361
Explanation Screenshot
2. Enter
ZCAA361_UI_TRAVEL_###_
O2 in the Name box.
6. Click Next.
8. Click Finish.
28
CAA361
Explanation Screenshot
29
CAA361
Explanation Screenshot
Explanation Screenshot
30
CAA361
Explanation Screenshot
3. Click Go.
Summary
You have completed the exercise!
31
CAA361
• Consume your service in a Fiori Elements via Fiori Elements App Preview
32
CAA361
TRANSACTIONAL SCENARIO
Overview
Estimated time: 60 minutes ( 95 minutes with optional track )
Introduction
A Business Object (BO) is a common term to represent a real-world artifact in enterprise
application development such as the Product, the Travel, or the SalesOrder. In general, a
business object contains several nodes such as Items and ScheduleLines and common
transactional operations such as for creating, updating and deleting data and additional
application-specific operations, such as Approve in a SalesOrder business object. All
changing operations for all scenario-related business objects form the transactional
behavior in a development scenario.
Further reading:
Link: Developing New Managed Transactional Apps
Link: Data Modeling and Behavior
Composition Model
From a structural aspect, a business object consists of a tree of nodes where the nodes are
linked by means of a special kind of associations, the compositions. A composition is a
specialized association that defines a whole-part relationship. A composite part only exists
together with its parent entity (whole).
Each node of this composition tree is an element that is modeled with a CDS entity. The root
entity is of particular importance: It defines the top node in a business object's structure and
serves as a representation of the business object. The root entity is marked with the keyword
ROOT in the CDS data definition’s source code.
Further reading:
Link: Providing Business Object Structure
Link: Developing a Projection Layer for Flexible Service Consumption
Link: Business Object
Behavior Definition
33
CAA361
A Business Object Behavior Definition (Behavior Definition for short) is an ABAP Repository
object that describes the behavior of a business object in the context of the ABAP RESTful
programming model. A behavior definition is defined using the Behavior Definition Language
(BDL) and comprises capabilities and modelling aspects of the business object node or
nodes, for example the supported operations (such as create, update, and delete actions) or
the definition of lock dependencies between the parent and child nodes.
A behavior definition always refers to a CDS data model. This reference results from the
name equality with the root entity. This means that a CDS data model must always exist
before the behavior definition is created.
As shown in the figure below, a behavior definition relies directly on the CDS root entity. One
behavior definition refers exactly to one root entity and one CDS root entity has a maximum
of one behavior definition (0..1 relationship), which also handles all associated (child)
entities. The implementation of a behavior definition can be done in a single ABAP class
(behavior pool) or can be split between an arbitrary set of ABAP classes (behavior pools).
You can assign any number of behavior pools to a behavior definition (1: n relationship).
Relationship Between the Data Model, the Behavior Definition, and the Behavior
Implementation.
Further reading:
Link: Defining Elementary Behavior for Ready-to-Run Business Object
Link: Working with Behavior Definitions
Validation
A validation is an implicitly executed function intended to check the data consistency of an
existing instance of an entity (consistency validation). It is implicitly invoked by the business
object’s framework as soon as a trigger condition at a predefined point in time is fulfilled.
Validation can return messages to the consumer and reject inconsistent instance data from
being saved.
Further reading:
Link: Developing Validations
34
CAA361
As an application developer, you may want to determine, which fields of your business
object’s entities should be read-only or mandatory. If this property is valid for all instances of
your business object then Static Field Control is the answer.
Further reading:
Link: Adding Static and Dynamic Feature Control
Determination
A determination is an implicitly executed function that handles side effects of modified entity
instances. It is invoked by the business object’s framework as soon as a determination’s
trigger condition at a predefined point in time, the determination time, is fulfilled.
Determinations are triggered internally based on changes made to the entity instance of a
business object. The trigger conditions are checked by business object framework at
different points during the transaction cycle, depending on the determination time and the
changing operations on the relevant entity instances. For each determination, it is necessary
to specify both the determination time and the changes that form the trigger condition. A
trigger condition consists of a list of fields belonging to the same entity the determination is
assigned to and the changing operations that include creating or updating entity instances.
We call this operations trigger operations.
In case a field is changed (after creation or update), the condition is fulfilled. The framework
evaluates the triggering condition of all determinations at certain points in time (determination
time). For determinations, this is today either after each modification or during the save
phase.
You can use a determination primarily to compute data that is derived from the values of
other fields. The determined fields and the determining fields either belong to the same entity
or to different entities of a business object.
As a result, determination can modify entity instances and return transition messages (error,
warning, information, success).
Further reading:
Link: Developing Determinations
Action
An action is assigned to an individual entity of a business object and is used to implement a
modifying non-standard operation as part of the business logic.
Further reading:
Link: Developing Actions
Testing
As an application developer you may want to write integration tests to verify the behavior as
a whole.
Further reading:
Link: Testing the Business Object
Link: Ensuring Quality of ABAP Code
Objective
In the following exercises you will learn how to setup a transactional scenario. The
following topics will be covered:
Exercise Description
35
CAA361
• Composition Model
• See the composition Travel with root and Booking as child
• See Projection Views
• Behavior Definition
• Create a Behavior Definition via wizard
• Enable Create, Update and Delete on Travel entity
• Enable Create by Association, Update and Delete for the sub node
Booking
• Entity Manipulation Language
• Create an executable class which returns the output in the console of
your ABAP Development Tools
• Perform a Read operation on the Travel entity
• Validation
• Define and implement a Validation to check that the Begin Date is before
the End Date
• Static Field Control
• Define Static Field Control by keeping the Status as read only
• Determination and Actions
• Define and implement a Determination to set the Status for new Travel to
New
• Define and implement an Action to set the Status to Booked
• Dynamic Feature Control
• Set the Travel_ID to read-only if it’s filled.
• Enable the action bookTravel and the Update operation depending on the
Status of the Travel.
• Testing the application
• Create a new ABAP Unit Test Class.
• Get in touch with the ABAP Unit Test Methods class_setup, setup and
teardown.
• Use the OpenSQL Test Environment to mock the related database tables.
• Implement ABAP Unit Test using Entity Manipulation Language and the
ABAP Unit Test environment.
Composition Model
Explanation Screenshot
36
CAA361
Explanation Screenshot
2. Select it.
3. Click OK.
37
CAA361
Explanation Screenshot
9. Select it.
Behavior Definition
Explanation Screenshot
38
CAA361
Explanation Screenshot
4. Click Next.
39
CAA361
Explanation Screenshot
7. A skeleton of a Behavior
Definition is generated.
40
CAA361
Explanation Screenshot
10. Change the
<field_name> at etag to
Last_Changed_At for the
Travel entity. This enables
OData etag-handling for the
entity.
etag Last_Changed_At
41
CAA361
Explanation Screenshot
42
CAA361
Explanation Screenshot
43
CAA361
Explanation Screenshot
25. Provide an alias for Travel.
alias Travel
44
CAA361
Explanation Screenshot
Explanation Screenshot
4. Click Next.
45
CAA361
Explanation Screenshot
6. Click Finish.
46
CAA361
Explanation Screenshot
10. Method
if_oo_adt_classrun~ma
in is generated.
47
CAA361
Explanation Screenshot
11. We start by reading all
Bookings related to its Travel.
To do so use the EML
statement READ. In this
particular case it’s a Read-by-
Association.
READ ENTITIES OF
zcaa361_i_travel_###
ENTITY travel
BY \_Booking
FROM VALUE #( (
Travel_ID = '1337' ) )
RESULT
DATA(lt_bookings).
lt_bookings contains now all
Bookings of Travel 1337. You’ll
use these Bookings later.
48
CAA361
Explanation Screenshot
49
CAA361
Explanation Screenshot
50
CAA361
Explanation Screenshot
51
CAA361
Explanation Screenshot
52
CAA361
Explanation Screenshot
flight_date
= ls_flight-
flight_date
Customer_ID
= '4'
%control
= VALUE #( booking_id
= if_abap_behv=>mk-on
booking_date
= if_abap_behv=>mk-on
carrier_id
= if_abap_behv=>mk-on
connection_id
= if_abap_behv=>mk-on
flight_date
= if_abap_behv=>mk-on
Customer_ID
= if_abap_behv=>mk-on
)
) )
) ).
Validation
Explanation Screenshot
53
CAA361
Explanation Screenshot
54
CAA361
Explanation Screenshot
9. Click Next.
55
CAA361
Explanation Screenshot
13. Let’s start with reading the
date fields of the incoming
Travel keys.
READ ENTITY
zcaa361_i_travel_###\\t
ravel FROM VALUE #(
FOR <root_key> IN
keys
( %key =
<root_key>
%control = VALUE
#(
begin_date =
if_abap_behv=>mk-on
end_date =
if_abap_behv=>mk-on ) )
)
RESULT
DATA(lt_travel_result).
56
CAA361
Explanation Screenshot
number =
/dmo/cx_flight_legacy=>
end_date_before_begin_d
ate-msgno
v1
= ls_travel_result-
begin_date
v2
= ls_travel_result-
end_date
v3
= ls_travel_result-
travel_id
severity =
if_abap_behv_message=>s
everity-error )
%element-begin_date
= if_abap_behv=>mk-on
%element-end_date
= if_abap_behv=>mk-on
) TO reported.
57
CAA361
Explanation Screenshot
18. Save and activate:
• Ctrl + S
• Ctrl + F3
Explanation Screenshot
58
CAA361
Explanation Screenshot
5. Click Edit.
Determination
Explanation Screenshot
59
CAA361
Explanation Screenshot
2. Define a Determination
which sets the field Status to
N(new) if none is set. It’ll be
triggered when a new Travel
will be created.
determination
setInitialStatus on
modify { create; }
60
CAA361
Explanation Screenshot
61
CAA361
Explanation Screenshot
Action
Explanation Screenshot
62
CAA361
Explanation Screenshot
63
CAA361
Explanation Screenshot
64
CAA361
Explanation Screenshot
65
CAA361
Explanation Screenshot
if_abap_behv=>mk-on
) )
RESULT
DATA(lt_travel_result).
%param = CORRESPONDING
#( ls_travel )
) ).
66
CAA361
Explanation Screenshot
67
CAA361
Explanation Screenshot
Explanation Screenshot
68
CAA361
Explanation Screenshot
69
CAA361
Explanation Screenshot
8. Declare a new method which
will implement the Dynamic
Feature Control.
METHODS get_features
FOR FEATURES IMPORTING
keys
REQUEST
requested_features FOR
travel RESULT result.
70
CAA361
Explanation Screenshot
12. Set the fields to read only
according the Status of the
Travel.
result = VALUE #(
FOR ls_travel IN
lt_travel_result
LET
lv_is_not_booked =
COND #( WHEN
ls_travel-status = 'B'
THEN
if_abap_behv=>fc-o-
disabled
ELSE
if_abap_behv=>fc-o-
enabled )
lv_is_travel_initial =
COND #( WHEN ls_travel-
travel_id IS INITIAL
THEN
if_abap_behv=>fc-f-
mandatory
ELSE
if_abap_behv=>fc-f-
read_only ) IN
( %key
=
ls_travel-%key
%update
=
lv_is_not_booked
%field-
Travel_ID =
lv_is_travel_initial
%features-
%action-bookTravel =
lv_is_not_booked
) ).
71
CAA361
Explanation Screenshot
72
CAA361
Optional: Testing
Explanation Screenshot
73
CAA361
Explanation Screenshot
duration short
risk level harmless.
private section.
methods:
det_status for
testing raising
cx_static_check.
endclass.
class
ltcl_integration_test
implementation.
method det_status.
cl_abap_unit_assert=>fa
il( 'Implement your
first test here' ).
endmethod.
endclass.
74
CAA361
Explanation Screenshot
8. In class_setup create the
mockup environment for the
CDS view
ZCAA361_I_TRAVEL_###.
go_environment =
cl_cds_test_environment
=>create_for_multiple_c
ds(
i_for_entities =
VALUE #( ( i_for_entity
=
'ZCAA361_I_TRAVEL_###'
) )
).
75
CAA361
Explanation Screenshot
13. Create a new Travel.
MODIFY ENTITIES OF
zcaa361_i_travel_###
ENTITY Travel
CREATE FROM VALUE
#( (
Travel_ID = '1'
%control-
Travel_ID =
if_abap_behv=>mk-on
) )
FAILED
DATA(ls_failed)
REPORTED
DATA(ls_reported).
76
CAA361
Explanation Screenshot
cl_abap_unit_assert=>as
sert_not_initial(
VALUE #( lt_travel[
KEY entity COMPONENTS
Travel_ID = '1' ]
OPTIONAL )
).
cl_abap_unit_assert=>as
sert_equals(
exp = 'N'
act = lt_travel[
KEY entity COMPONENTS
Travel_ID = '1' ]-
Status
).
77
CAA361
Explanation Screenshot
Summary
You have completed the exercise!
You are now able to:
• Define a Composition Model.
• Define the Business Object.
78
CAA361
Further Reading:
Link: Developing New Managed Transactional Apps
Link: Data Modeling and Behavior
Thanks for attending today's Build a Transactional SAP Fiori App with the ABAP
RESTful Programming Model session!
You are now able to build a transactional app using the managed scenario on the SAP Cloud
Platform ABAP Environment.
79
CAA361
80
CAA361
COPYRIGHT
© 2019 SAP SE or an SAP affiliate company. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.
The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other
software vendors. National product specifications may vary.
These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated companies
shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the
express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.
In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any
functionality mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or
platforms, directions, and functionality are all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in
this document is not a commitment, promise, or legal obligation to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties
that could cause actual results to differ materially from expectations. Readers are cautioned not to place undue reliance on these forward-looking statements, and they should not be
relied upon in making purchasing decisions.
SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in
Germany and other countries. All other product and service names mentioned are the trademarks of their respective companies.
See https://www.sap.com/copyright for additional trademark information and notices.
81