Entity Manipulation Language (EML)
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A RAP business object, such as an expense claim with its items, keeps its rules in one place: the behavior definition says
what may be done with the object, and the program that implements it checks the data and saves it. Other ABAP code, for
example a report, a test class or a job, must not write to the claim tables directly, because that would skip those rules.
Instead it talks to the business object in a small set of ABAP statements that together are called the Entity
Manipulation Language, EML. After this page you can read a claim with its items, create a claim with items in one
statement, change it, run an action on it, save the work, undo it, and understand what the three response structures
FAILED, REPORTED and MAPPED tell you. The page also explains what the word "local mode" means and why the order of
the phases, interaction first and saving later, decides which statement may be used where. It is written against Standard
ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608); EML is part of the ABAP language and is described in
the ABAP Keyword Documentation. The statements need a RAP business object that exists in an SAP system, so every listing
on this page requires an SAP system; not run here.
Before you read
-
CDS Associations and Compositions: the composition from the claim
ZI_ExpClaimto its itemsZI_ExpItem, which is the tree that EML statements walk along. -
Constructor Expressions: the
VALUE #( ... )expression that builds the input tables of every EML statement. -
ABAP SQL: Writing Data:
COMMIT WORKand the database LUW, becauseCOMMIT ENTITIESends with one. -
The behavior definition itself, with its validations and actions, has a page of its own, Behavior Definitions and Implementations; here only the lines that the listings need are shown.
Key terms
-
EML: the ABAP statements
READ ENTITIES,MODIFY ENTITIES,COMMIT ENTITIESandROLLBACK ENTITIESthat a program uses to work with the instances of a RAP business object. -
RAP business object: a business object (such as a claim with its items) built from CDS entities and a behavior definition, with the rules that apply to it.
-
Transactional buffer: the memory area in which the changes of one RAP transaction wait until they are saved or discarded.
-
Interaction phase: the part of a RAP transaction in which data is changed in the transactional buffer, before anything is saved.
-
Save sequence: the steps, early save, late save and cleanup, that write the buffer to the database after
COMMIT ENTITIES. -
Content ID: the temporary name
%cidthat you give a new instance inside one EML request so that later lines can refer to it. -
Local mode: the addition
IN LOCAL MODEthat a business object’s own code uses to skip feature control and authorization checks.
What EML is and where it runs
EML is the way ABAP code consumes a RAP business object. The Keyword Documentation separates two roles. The RAP business
object provider writes the behavior definition and the behavior implementation. The consumer is any other code: a
Fiori app reaches the business object through an OData service, and an ABAP class, a test or a job reaches it through EML.
Where the service layer is used, the framework does the saving and the undoing for you. When you call EML yourself, you
have to end the work yourself with COMMIT ENTITIES or ROLLBACK ENTITIES.
Four statements make up the language for ordinary work.
| Statement | What it does |
|---|---|
|
Reads instances, and the instances reached through an association, from the business object. |
|
Creates, updates or deletes instances, creates children by association, and runs actions. The changes go to the transactional buffer. |
|
Starts the save sequence, which writes the buffer to the database and ends the RAP transaction. |
|
Throws away the changes of the RAP transaction and releases its locks. |
ENTITY is the short form and works on one entity of the business object; ENTITIES OF is the long form and works on
several entities of the same business object in one statement. The long form starts with the root entity, the top entity
of the tree, and the entities after it must belong to the composition tree of that root.
The running example is the expense claim ZI_ExpClaim with the items ZI_ExpItem, from the CDS page, reached through the
composition _Items. In the Dictionary the claim is the table ZEXP_CLAIM of
Database Tables and Structures, and the items are
ZEXP_ITEM. The listings assume a behavior definition for ZI_ExpClaim that allows create, update and delete,
allows create in the association _Items, and defines an action submit; the next pages write that behavior
definition. If the behavior definition gives an entity an alias, you write the alias after ENTITY in the long form
instead of the CDS name.
The two phases behind every EML request
Before the statements, one idea decides everything else on this page: a RAP business object works in two phases. In the
interaction phase the program changes data in the transactional buffer, a memory area. Nothing is written to the
database, so the database stays consistent while the buffer may hold half-finished work. In the save sequence the
buffer is checked and written to the database. MODIFY belongs to the first phase and COMMIT ENTITIES starts the second.
The Keyword Documentation divides the save sequence into an early save phase, a late save phase and a cleanup phase,
entered in this order. The early save phase covers the saver methods finalize, check_before_save and
cleanup_finalize. The late save phase covers adjust_numbers and save (or save_modified), and the saver method
cleanup is called after a successful save. Two facts from the cheat sheet that the documentation pages echo explain why
the split exists. The early phase can still fail and send the program back to the interaction phase. The late phase is the
point of no return: either the data is written, or the transaction is rolled back. Database changes are allowed only in the
late phase.
Creating a claim with its items
The first listing creates a claim and two items in one statement, which is the example the page promised. The long form is
used because two entities are involved: the root ZI_ExpClaim and, through the association _Items, its children.
The statement has three parts. CREATE FIELDS ( ... ) WITH VALUE #( ... ) creates instances and lists the fields you
supply. Each new instance gets a content ID, %cid, which is a name you invent, valid only inside this one request.
CREATE BY \_Items FIELDS ( ... ) WITH VALUE #( ... ) creates children below a parent: %cid_ref points at the parent
through its content ID, and %target holds the new children, each with a content ID of its own. Finally
FAILED, REPORTED and MAPPED receive the answers, explained in the next section. In the listing the response
structures are declared inline with FINAL( ... ), the form the documentation uses.
" requires an SAP system; not run here
MODIFY ENTITIES OF zi_expclaim
ENTITY zi_expclaim
CREATE FIELDS ( ClaimId EmployeeId Status CurrencyCode ) WITH VALUE #(
( %cid = 'claim1'
ClaimId = 'C-1001'
EmployeeId = '00000042'
Status = 'N'
CurrencyCode = 'EUR' ) )
CREATE BY \_Items FIELDS ( ItemNo Category Amount CurrencyCode ) WITH VALUE #(
( %cid_ref = 'claim1'
%target = VALUE #(
( %cid = 'item1' ItemNo = 1 Category = 'TRAVEL' Amount = 120 CurrencyCode = 'EUR' )
( %cid = 'item2' ItemNo = 2 Category = 'MEALS' Amount = 35 CurrencyCode = 'EUR' ) ) ) )
FAILED FINAL(failed)
REPORTED FINAL(reported)
MAPPED FINAL(mapped).
IF failed IS NOT INITIAL.
" nothing was created for the failed instances: read REPORTED for the messages
RETURN.
ENDIF.
COMMIT ENTITIES.
IF sy-subrc <> 0.
" the save sequence did not complete: the claim was not written to the database
ENDIF.
The shape of the statement, CREATE FIELDS, CREATE BY \_assoc FIELDS, %cid, %cid_ref, %target and the three
response parameters, is the one used by the Keyword Documentation’s MODIFY ENTITIES example with a managed business
object, and the lines after it, the check of sy-subrc after COMMIT ENTITIES, follow the documentation’s rule that this
check should always be included. The claim and item field values, the status code N and the employee number are
invented for the running example. The status code N stands for "new" and has no meaning outside this page.
Source: MODIFY ENTITIES, Long Form and MODIFY ENTITY, Short Form and COMMIT ENTITIES
Three rules from the documentation are worth knowing now.
-
MODIFY ENTITIES OFnames the root entity with its full name; the entities afterENTITYshould use the aliases of the behavior definition when it defines them, and using the full name there causes a warning. -
The same operation can be given only once for the same entity in one statement, but different operations on the same entity, for example
CREATEandCREATE BY, can be listed one after the other, as the listing does. -
An EML statement should not be placed in a loop, because it can lead to many single database accesses. The advice is to read what is needed with one
READ, and to change it with oneMODIFY.
Source: MODIFY ENTITIES, Long Form and COMMIT ENTITIES
The three responses: FAILED, REPORTED and MAPPED
Every non-dynamic MODIFY and READ statement can return three deep structures. They are declared with the types
TYPE RESPONSE FOR FAILED, TYPE RESPONSE FOR MAPPED and TYPE RESPONSE FOR REPORTED, followed by the name of the
business object, or they are created inline with DATA or FINAL. A deep structure is a structure that contains tables;
here there is one table for each entity of the business object, so failed-zi_expclaim is the table of failed claims.
| Response | What it contains |
|---|---|
|
The instances for which an operation failed, identified by their keys. A table that is not initial means that something went wrong. |
|
Messages about specific instances or about the whole business object. It explains a |
|
For created instances, the link from your content ID |
The documentation adds three details. First, the default of these types is EARLY, which means that the answers describe
the interaction phase; the addition LATE applies only to a business object with late numbering and to the long form of
COMMIT ENTITIES, where the answers come from the save phase. Second, %cid is a temporary name valid in one request, and
afterwards the key is the only identifier. Third, when a non-dynamic MODIFY statement has no FAILED parameter and no
IN LOCAL MODE, the compiler gives a syntax warning; the documentation recommends always specifying FAILED and
handling the error. The pragma ##EML_FAILED_MISSING_OK suppresses the warning when you really do not need it.
The next listing reads the claim back and checks the FAILED answer before using the results. It reads the root with all
fields and then the items through the association _Items in the same statement. Reading is done from the transactional
buffer if the instance is already there, so the data created above is visible before it is saved.
" requires an SAP system; not run here
READ ENTITIES OF zi_expclaim
ENTITY zi_expclaim
ALL FIELDS WITH VALUE #( ( %key-ClaimId = 'C-1001' ) )
RESULT FINAL(claims)
ENTITY zi_expclaim
BY \_Items
ALL FIELDS WITH VALUE #( ( %key-ClaimId = 'C-1001' ) )
RESULT FINAL(items)
FAILED FINAL(read_failed).
IF read_failed-zi_expclaim IS NOT INITIAL.
RETURN.
ENDIF.
The shape, ALL FIELDS WITH VALUE #( ( %key-... ) ) RESULT FINAL( ... ), one entry per entity, BY \_assoc for the read by
association, and the check read_failed-<entity> IS NOT INITIAL, is the one used by the documentation’s READ ENTITIES
example. A read always needs RESULT; no entry in the behavior definition is needed for it, because reading is allowed by
default. The key components are written %key-ClaimId as in the documentation’s example.
Updating, deleting and running an action
Changes to a claim that already exists use the key instead of a content ID. The listing below changes the status field of
one claim and then runs the action submit on it. An action is a named operation that the behavior definition declares,
such as "submit the claim"; it is started in MODIFY with EXECUTE. Both operations can be put into one statement, because
the documentation allows the standard operations and actions in a single request.
" requires an SAP system; not run here
MODIFY ENTITIES OF zi_expclaim
ENTITY zi_expclaim
UPDATE FIELDS ( EmployeeId ) WITH VALUE #(
( ClaimId = 'C-1001' EmployeeId = '00000043' ) )
EXECUTE submit FROM VALUE #( ( ClaimId = 'C-1001' ) )
FAILED FINAL(failed)
REPORTED FINAL(reported).
UPDATE FIELDS ( ... ) WITH lists the fields to change and EXECUTE action FROM starts the action, both in the forms of
the Keyword Documentation. Deleting uses DELETE FROM VALUE #( ... ) with the keys. The action submit exists only if the
behavior definition declares it, which the next page shows; a name that is not declared is a syntax error. MAPPED is
omitted here because nothing is created, and the documentation does not offer MAPPED for an update.
Saving and undoing: COMMIT ENTITIES and ROLLBACK ENTITIES
Until COMMIT ENTITIES runs, all changes exist only in the transactional buffer. The documentation says that at the end of
an ABAP session the saving must be triggered with COMMIT ENTITIES; otherwise the changes are lost. The statement starts
the save sequence of the previous diagram. It follows an all-or-nothing approach: either all changed instances are valid and
saved, or none is. If the transaction contains modify operations, an implicit COMMIT WORK follows the save sequence.
The statement sets sy-subrc, and the table is the one the documentation gives.
sy-subrc |
Meaning |
|---|---|
|
The implicit |
|
A problem in the early phase ( |
|
A problem in the late phase ( |
There are four forms, and the short form is enough most of the time.
-
The short form
COMMIT ENTITIES [RESPONSES FAILED f REPORTED r].receives the answers of the save sequence in internal tables typedABP_BEHV_RESPONSE_TAB. -
The long form
COMMIT ENTITIES RESPONSE OF root1 ...gives one answer block per root entity, typed with the additionLATE. -
The dynamic form names the root entities in an internal table.
-
COMMIT ENTITIES BEGINandCOMMIT ENTITIES ENDenclose a block that is needed for late numbering, where the final keys are known only after the save, andCONVERT KEY OFturns a preliminary key into the final one.
With the addition IN SIMULATION MODE the save sequence runs without the final commit, so a program can ask whether a
save would succeed.
ROLLBACK ENTITIES. rolls back all changes of the current RAP transaction. It clears the transactional buffer by calling
the cleanup method, releases the locks, and calls ROLLBACK WORK when the transaction contains modify operations. It has
no response parameters. In an OData scenario the framework issues the COMMIT ENTITIES or the ROLLBACK ENTITIES for you;
in a manually written service or an ABAP class you issue it yourself. Both statements are not allowed in a RAP business
object’s own implementation: they belong to consumers only.
" requires an SAP system; not run here
MODIFY ENTITIES OF zi_expclaim
ENTITY zi_expclaim
UPDATE FIELDS ( Status ) WITH VALUE #( ( ClaimId = 'C-1001' Status = 'R' ) )
FAILED FINAL(failed).
IF failed IS NOT INITIAL.
ROLLBACK ENTITIES. " discard the buffer and release the locks
ELSE.
COMMIT ENTITIES.
IF sy-subrc <> 0.
ROLLBACK ENTITIES. " the save sequence did not complete
ENDIF.
ENDIF.
The pattern above joins the two statements: after a MODIFY that failed, drop the work; after a COMMIT ENTITIES that did
not return 0, drop it as well. The status code R is invented for this page. Since sy-subrc = 4 leaves the changes in
the buffer, calling ROLLBACK ENTITIES there is a choice, not a rule: a program that prefers to correct the data and call
COMMIT ENTITIES again may do so.
Source: COMMIT ENTITIES, COMMIT ENTITIES, Short Form and ROLLBACK ENTITIES
IN LOCAL MODE: EML inside the business object
The business object’s own code, the handler classes of its behavior implementation, also needs to read and change the
object. If that code went through the full public path, it would run feature control and the authorization checks again,
and could not change a field that the behavior definition marks as read-only for users. The addition IN LOCAL MODE solves
this. It excludes feature controls and authorization checks, and the handler methods for prechecks are not called.
The documentation’s own use case is a trip status shown on a Fiori screen. The field is read-only in the behavior
definition, so the user cannot type into it. A button runs an action that books the trip, and the action’s code uses
IN LOCAL MODE so that its change of the status is not stopped by the feature control.
The rules are strict. IN LOCAL MODE works only for the business object itself, in the classes of its behavior pool, or in
classes those classes call. Used from an ordinary program, or while another business object is involved, it ends in a short
dump. It is not allowed in BAdIs. COMMIT ENTITIES and ROLLBACK ENTITIES are not allowed in the business object
implementation at all, and MODIFY may be used there only in the interaction phase and in the saver method finalize; the
documentation forbids it in check_before_save, adjust_numbers, save, save_modified and cleanup.
" requires an SAP system; not run here: this statement belongs in a handler method of the behavior pool of ZI_ExpClaim
MODIFY ENTITIES OF zi_expclaim IN LOCAL MODE
ENTITY zi_expclaim
UPDATE FIELDS ( Status ) WITH VALUE #( ( ClaimId = 'C-1001' Status = 'S' ) )
FAILED FINAL(failed)
REPORTED FINAL(reported).
What older books show, and what has changed
-
EML is part of the ABAP RESTful Application Programming Model. A book written before RAP describes the older business object frameworks (BOPF and the SAP Fiori programming model) instead. Their business objects are not RAP business objects; a migration tool for CDS-based BOPF objects exists but its documentation says that the result is not a native RAP business object and is not recommended for ABAP Cloud.
-
Older RAP material, including early blog posts, uses statement forms that the Keyword Documentation now lists in a section for obsolete EML statements and obsolete derived types. For new code use the forms on this page, and read the obsolete section only when you maintain old code.
-
COMMIT WORKalso triggers the save sequence of a RAP business object, but the documentation prefersCOMMIT ENTITIESin RAP code because it returns the responses and offers key conversion. Older code that callsCOMMIT WORKafter EML still works, and it carries no responses.
Summary
-
EML is how ABAP code outside a RAP business object works with it:
READ ENTITIESreads,MODIFY ENTITIESchanges the transactional buffer,COMMIT ENTITIESsaves,ROLLBACK ENTITIESdiscards. -
A RAP transaction has an interaction phase, where
MODIFYworks on the buffer, and a save sequence with an early save phase, a late save phase and a cleanup, whichCOMMIT ENTITIESstarts. -
New instances get a temporary
%cid; children are created withCREATE BY \_assocand%target, and%cid_refpoints to the parent. -
FAILEDlists instances for which an operation failed,REPORTEDcarries the messages andMAPPEDlinks content IDs to real keys. Always specifyFAILEDand test it. -
After
COMMIT ENTITIEStestsy-subrc:0is saved,4an early-phase problem with the buffer kept,8a late-phase failure that needsROLLBACK ENTITIESbefore going on. -
IN LOCAL MODEskips feature control and authorization checks and is for the business object’s own code only.