
RAP - EML Variants
If you use EML to interact with the ABAP RESTful Application Programming Model, several variants are currently available. Let's take a closer look at them.
Table of contents
In this article, we'll look at different EML variants, how you can use them, and which one you should choose.
Introduction
The Entity Manipulation Language, or EML for short, is used to interact with a RAP object in ABAP. If you're new to RAP, the new syntax will initially overwhelm you. You should approach it step by step to gain a better understanding. You can find two articles on this topic on our blog if you'd like to read more:
Structure
Let's take a look at an EML example and break it down into its various components. Basically, the same things are reused in many EML statements and appear frequently. Here's a typical example.
- Action - Which action should be performed? A READ to read entries via key, a MODIFY to change data in the object.
- RAP Object - This is the actual RAP object or the root node of the object. You will also find a behavior definition for this name in the system, where the options and settings are described.
- Entity - The name of an entity from a RAP object. If an ALIAS has been assigned to an entity, you can access the entity via it.
- Additional - Additional properties such as the field list, the new data, or restrictions in the form of keys.
In the following chapters, we would separate the variants according to WITH and FROM to better highlight the differences. We will use the RAP object from the simple RAP example with an entity on the partner.
Creation
This chapter is about creating a simple data record. We only want to fill a few fields of the data record and look at the differences.
WITH
In this variant, we create a new data record in the system. With the FIELDS addition, we define the fields that should be filled. The key is filled by the framework. After WITH, we specify a table and fill the two fields we have defined.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerName = 'TEST: WITH'
Country = 'US' ) ).
FROM
In this variant, the FIELD addition is omitted; instead, we have to work with the CONTROL structure to tell the framework which fields should be considered. This essentially makes the statement smaller, but the VALUE statement much larger. In this variant, we address each CONTROL field individually, but you can also nest them with a VALUE. In our opinion, this variant is somewhat easier to read.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE
FROM VALUE #( ( PartnerName = 'TEST: FROM'
Country = 'US'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
Result
After inserting the two operations, we get the following result.
Creation (More)
In the second variant, we again insert a new data record into the database. Here, however, we provide one more piece of information by filling in the Street field.
WITH
Basically, this statement is the same as before; we just additionally fill in the Street field. However, since we do not specify the field in the FIELDS clause, it is ignored during creation.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerName = 'TEST: WITH'
Country = 'DE'
Street = 'With-Street' ) ).
FROM
Here, too, we have similar behavior as with the WITH addition. Since we haven't set the CONTROL structure, the field is ignored during processing.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE
FROM VALUE #( ( PartnerName = 'TEST: FROM'
Country = 'DE'
Street = 'From-Street'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
Result
Here again is the result of the two new data records; the street remains empty.
Update
In this example, we want to update the data records that have already been created. We pass the partner's number, an empty field for the partner's name, and the street again.
WITH
In this case, we perform an update and thus specify the key information. We don't need to define the key as a field for it to be used. We leave the name blank and fill in the street again. Since we have the name in the FIELDS clause, it is cleared from the database.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner UPDATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerNumber = '1000000018'
PartnerName = ''
Street = 'TEST: WITH2'
Country = 'CH' ) ).
FROM
Basically the same scenario as in the previous case. We specify the key, but we don't need to activate it in the CONTROL structure. In this example, we also clear the name from the database and update the country.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner UPDATE
FROM VALUE #( ( PartnerNumber = '1000000019'
PartnerName = ''
Street = 'TEST: FROM2'
Country = 'CH'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
Result
Finally, the result. In this case, we updated both records, emptied the database fields, and changed the country.
Full Example
The full code from today's example can be found here. When you run the class and view the example, you should do use case after use case to see the changes in the data.
CLASS zcl_bs_demo_eml_fields DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
METHODS create_with.
METHODS create_from.
METHODS create_with_more.
METHODS create_from_more.
METHODS update_with_less_fields.
METHODS update_from_less_fields.
ENDCLASS.
CLASS zcl_bs_demo_eml_fields IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
create_with( ).
create_from( ).
create_with_more( ).
create_from_more( ).
update_with_less_fields( ).
update_from_less_fields( ).
COMMIT ENTITIES.
ENDMETHOD.
METHOD create_with.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerName = 'TEST: WITH'
Country = 'US' ) ).
ENDMETHOD.
METHOD create_from.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE
FROM VALUE #( ( PartnerName = 'TEST: FROM'
Country = 'US'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
ENDMETHOD.
METHOD create_with_more.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerName = 'TEST: WITH'
Country = 'DE'
Street = 'With-Street' ) ).
ENDMETHOD.
METHOD create_from_more.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner CREATE
FROM VALUE #( ( PartnerName = 'TEST: FROM'
Country = 'DE'
Street = 'From-Street'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
ENDMETHOD.
METHOD update_with_less_fields.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner UPDATE FIELDS ( PartnerName Country )
WITH VALUE #( ( PartnerNumber = '1000000018'
PartnerName = ''
Street = 'TEST: WITH2'
Country = 'CH' ) ).
ENDMETHOD.
METHOD update_from_less_fields.
MODIFY ENTITIES OF ZBS_I_RAPPartner
ENTITY Partner UPDATE
FROM VALUE #( ( PartnerNumber = '1000000019'
PartnerName = ''
Street = 'TEST: FROM2'
Country = 'CH'
%control-PartnerName = if_abap_behv=>mk-on
%control-Country = if_abap_behv=>mk-on ) ).
ENDMETHOD.
ENDCLASS.
Conclusion
Basically, two variants are available in EML, and you can decide which one you prefer based on your own preferences. Would you prefer to work more with the Fields addition and specify the fields explicitly, or do you prefer the variant with the CONTROL structure to control the behavior? However, it is important that you use a variant.
Source:
SAP Help - MODIFY ENTITY