Managed, Unmanaged and Unmanaged Save
|
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 is a piece of data (an expense claim with its items, for example) plus the rules for creating, changing and deleting it. Somebody has to write the code that keeps the changes in memory while a user works, that checks them, and that finally writes them to the database. RAP lets you decide how much of that you write yourself. In the managed implementation type the framework does it for you and you add only your own business rules. In the unmanaged implementation type you write all of it, which is what you want when working code already exists and must be reused. Between the two sits a managed object whose last step, the save, is replaced or extended by your own code. This page explains the three choices, shows the behavior definition and the ABAP code that belong to each, and says how to pick one. It is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20 (2608) and applies to RAP in ABAP Cloud (SAP BTP ABAP environment, SAP S/4HANA Cloud Public Edition) and in SAP S/4HANA on premise. The unmanaged wrapping of old function modules is mostly a topic of systems that already have such modules; whether your code may call a given function module depends on the ABAP language version of the project (see ABAP Language Versions and Released APIs). Every listing on this page needs RAP and a behavior definition, so each one is labelled as requiring an SAP system and was not run here.
Before you read
-
CDS View Entities: a business object is built on a CDS data model whose top entity is the root view entity, such as
ZI_ExpClaim. -
Classes and Objects: the behavior is written in local classes that inherit from framework classes.
-
Function Modules and Function Groups: the old kind of reusable procedure that the unmanaged type wraps.
-
Lock Objects and the SAP Lock Concept:
lock masterand theFOR LOCKmethod, which an unmanaged object must implement itself. -
ABAP SQL: Writing Data: database LUWs and
COMMIT WORK. -
The behavior definition language itself (determinations, validations, actions), EML and draft handling have pages of their own (Behavior Definitions and Implementations, EML and Draft Handling); this page uses only the few keywords it explains.
Key terms
-
Managed RAP business object: a business object whose transactional buffer and standard operations (create, update, delete) are provided by the RAP framework.
-
Unmanaged RAP business object: a business object whose transactional buffer and operations you implement yourself in the behavior pool.
-
Transactional buffer: the memory area that holds the changes of the current transaction until they are saved or discarded.
-
Behavior pool: the global class, named in the behavior definition, whose local classes hold the handler and saver code.
-
Save sequence: the fixed series of steps that runs after at least one successful change, from final calculations to the write to the database and the clean-up.
-
Additional save: extra code that runs inside the managed save, for example to write change documents.
-
Unmanaged save: your own code that replaces the managed save of a managed business object.
One question decides it: who owns what
Every RAP business object has two phases. In the interaction phase a consumer (a Fiori app, a test class, another program) calls operations on the object: create a claim, change its total, read it. The object keeps these changes in the transactional buffer and nothing is written to the database yet. In the save sequence, which starts after at least one successful change and an explicit request to save, the buffer is checked and written to the database. The RAP guide describes the two standard implementation types by who provides the transactional buffer and the operations: in a managed object the managed RAP BO provider does, in an unmanaged object the application developer does.
| Managed | Managed with additional or unmanaged save | Unmanaged | |
|---|---|---|---|
Standard operations (create, update, delete) |
Named in the behavior definition; provided by the framework. |
Same as managed. |
Named in the behavior definition; written by you in handler methods. |
Transactional buffer |
Provided by the framework. |
Provided by the framework. |
Provided and handled by you. |
The save to the database |
Done by the framework into the table named by |
Additional save: the framework writes, and your code runs inside the save. Unmanaged save: your code writes instead. |
Written by you in the saver class. |
Reuse of existing code |
Hard to integrate, says the RAP guide. |
Possible for the save step only. |
The main purpose: existing function modules are called from the handler methods. |
Typical start |
A new application built from scratch (greenfield). |
A new application that must save through existing update function modules, or must also write change documents. |
An existing application with working business logic that gets a RAP service on top (brownfield). |
Source: RAP: Business Object Implementation Types and Keyword Documentation: RAP - Implementation Type
Both types start from the same data model, written in CDS view entities, and either can be exposed as an OData service for a user interface or as a Web API. Draft handling and the query features are additional options that can be added to either type. The managed type has a further option, draft, that the RAP guide manages through a draft provider; that is the subject of its own page.
The managed type
In a managed business object you list the operations in the behavior definition and the framework provides them. The RAP guide says that standard operations "must only be specified in the behavior definition to obtain a ready-to-run business object". The managed provider also handles the transactional buffer, and the interaction phase and the save sequence are implemented generically. You then add business logic with actions, validations and determinations, which later pages show. The type is recommended for new development without a large amount of existing code.
The following behavior definition for the claim of this section is derived from the Keyword Documentation’s example of a
managed business object. The names ZI_ExpClaim and zbp_i_expclaim follow the naming of the other pages of this section
and are not part of the SAP example.
managed implementation in class zbp_i_expclaim unique;
strict ( 2 );
define behavior for ZI_ExpClaim alias Claim
persistent table zexp_claim
lock master
authorization master ( none )
{
create;
update;
delete;
field ( readonly : update ) claim_id;
}
Source: Keyword Documentation: RAP - Implementation Type and Keyword Documentation: RAP - implementation in class unique
The listing requires an SAP system; not run here. Reading it line by line:
-
managedis the implementation type. It is written first, in the header of the behavior definition. -
implementation in class zbp_i_expclaim uniquenames the behavior pool, a special global class whose name usually starts withBP_(here the customer prefixzbp_). The behavior itself is not written in this global class but in local classes of it. The mandatory worduniquemeans that each operation can be implemented exactly once. In a managed object the pool is needed only for the parts the framework cannot do, such as authorization control. -
strict ( 2 )switches on the strict mode of the behavior definition language, at level 2. -
define behavior for ZI_ExpClaim alias Claimstarts the behavior of the root entity, with a short alias that code in the behavior pool uses. -
persistent table zexp_claimis the database table into which the managed save writes. By default the managed runtime saves all changed instances into the table named here. -
lock masterandauthorization master ( none )are explained on the pages about locking and authorization.authorization master ( none )switches authorization control off, as in the SAP example; a real claim object would use a real setting. -
create; update; delete;are the standard operations. Nothing more is needed to get a business object that a service can expose.field ( readonly : update ) claim_idmakes the key field read-only once the claim exists.
Older books and older SAP texts write the implementation type with the word implementation in front, as in
implementation managed;. The Keyword Documentation calls that syntax obsolete, says it should not be used, and says that it
must not be used in strict mode. New code writes managed implementation in class ... unique as above.
The save sequence
Before additional save and unmanaged save make sense, you need to know where they sit. The save sequence is part of the business object runtime. The RAP guide describes it as follows.
-
FINALIZE: final calculations that involve all business objects of the current transaction. These are determinations in a managed object, or your own method in an unmanaged one. It is the last chance to change data in the transactional buffer; after it, a modifying request raises a runtime error. -
CHECK_BEFORE_SAVE: the checks, which are validations in a managed object or your own method in an unmanaged one. If the checks are positive for every business object involved, the point of no return is reached and a successful save must be guaranteed. If they are negative, the save is refused, the sequence stops, and the consumer may change data and try again. -
ADJUST_NUMBERS: after the point of no return, only when the object uses late numbering, which means the final key value is assigned just before the save. -
SAVE: the buffer is written to the database. Here the options of this page apply. -
All change requests of the current LUW are committed; the actual save is finished by
COMMIT WORK. -
CLEANUP: clears the transactional buffer.CLEANUP_FINALIZEclears it whenFINALIZEorCHECK_BEFORE_SAVEfails.
The Keyword Documentation groups the methods into an early save phase (finalize, check_before_save and
cleanup_finalize) and a late save phase (adjust_numbers and save, or save_modified, with cleanup after a
successful save). The methods are redefined in a local saver class of the behavior pool.
Source: RAP: Save Sequence Runtime and RAP: Unmanaged Save and Keyword Documentation: RAP save sequence
The runtime guide adds that the save sequence is always triggered when a modify request on an object is executed, even when
the buffer is not changed and the request only passes work on to another object. With several business objects in one
transaction, FINALIZE and CHECK_BEFORE_SAVE run for all of them, even if one of them fails, and CLEANUP_FINALIZE then
clears all their buffers.
Additional save: managed, plus extra steps
Sometimes the managed save is right but something else must happen at the same moment. The RAP guide names reuse services: change
documents, which record who changed which business data and when, and the application log. These must be called during the
save sequence, after the managed runtime has gathered the changed data and before the final COMMIT WORK. That is an
additional save. The managed runtime still writes the persistent table, and your code runs besides it.
You switch it on in the behavior definition with with additional save, either in the header, which enables it for all entities
of the object, or for single entities. The Keyword Documentation’s example puts it in the header:
managed with additional save with full data
implementation in class bp_demo_managed_additional_sav unique;
strict(2);
define behavior for DEMO_MANAGED_ADDITIONAL_SAVE
persistent table DEMO_TAB_ROOT_3
lock master
authorization master ( none )
{
create;
update;
delete;
field(readonly:update) key_field;
}
The listing is the SAP demo object and requires an SAP system; not run here. Three details are worth knowing:
-
Both
with additional saveandwith unmanaged saveneed a local saver class that inherits fromcl_abap_behavior_saverand redefines its methodsave_modified. The method is mandatory in that case; the methodsavecannot be used then. -
By default only the values of the key fields and of the changed fields reach
save_modified. The additionwith full datahands over the full instance data, so all fields of the component group%dataare filled. -
The optional addition
and cleanupmeans that the saver methodcleanupmust be redefined too.
Unmanaged save: managed, but you do the writing
In some projects the managed save is exactly what you must not use. The RAP guide’s use case: you need the interaction phase
of a new application anyway, but the update task function modules that persist the data are already available. An update
task function module is a function module that the system runs in the update work process after the commit; a later page
explains how it works. With with unmanaged save the managed runtime keeps the buffer and runs the interaction phase, but it
does not save. You save instead, typically by calling those function modules.
Unmanaged save can be applied to the whole object or to one entity. The RAP guide’s example gives each entity of the flight
travel object a different save: the root entity an additional save, one child the default managed save with its
persistent table, and another child an unmanaged save. Rewritten for a claim and its items, the idea reads as follows:
managed implementation in class zbp_i_expclaim unique;
strict ( 2 );
define behavior for ZI_ExpClaim alias Claim
with additional save
lock master
authorization master ( none )
{
create;
update;
delete;
}
define behavior for ZI_ExpItem alias Item
implementation in class zbp_i_expitem unique
with unmanaged save
lock dependent by _Claim
{
update;
delete;
association _Claim;
}
Source: RAP: Defining Unmanaged Save in the Behavior Definition and Keyword Documentation: RAP - SavingOptions
The listing requires an SAP system; not run here. It is a sketch for the claim and its items; the full behavior definition has
more entries, and ZI_ExpItem is the subject of later pages. Only the saving options and the rule below come from the
sources; lock dependent by and the association line are explained on the locking page. Compare the root entity: it has no
persistent table line, because the Keyword Documentation says that in an unmanaged save a persistent table must not be
specified. (With an additional save the persistent table stays, as the earlier listing shows.)
The code goes into the saver class of the behavior pool. The base class cl_abap_behavior_saver provides the method
save_modified, which you redefine. Its implicit input parameters are create, update and delete. Each holds the instances
changed in the current transaction, grouped by entity, so the table of created items is create-item. For create and
update, the structure %control tells which fields the consumer provided; for delete there is only the key. The method is
called only if the earlier saver methods succeeded, and the RAP guide’s convention is that the local saver class sits in the
behavior pool of the root entity or in a separate global class.
The SAP flight example, adapted here for items, shows the three parts. The three function modules that do the writing are
stand-ins for the existing update functions: they are written by the SAP sample as plain INSERT, UPDATE and DELETE of
a table, and here they are assumed to exist with the same shape.
CLASS lcl_save DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.
METHODS save_modified REDEFINITION.
ENDCLASS.
CLASS lcl_save IMPLEMENTATION.
METHOD save_modified.
DATA items_db TYPE STANDARD TABLE OF zexp_item.
" (1) instances created during the transaction
IF create-item IS NOT INITIAL.
items_db = CORRESPONDING #( create-item ).
CALL FUNCTION 'Z_EXP_ITEM_C' EXPORTING values = items_db.
ENDIF.
" (2) instances updated during the transaction
items_db = CORRESPONDING #( update-item ).
IF items_db IS NOT INITIAL.
" read the full rows first, so unchanged fields keep their database values
SELECT * FROM zexp_item FOR ALL ENTRIES IN @items_db
WHERE claim_id = @items_db-claim_id AND item_no = @items_db-item_no
INTO TABLE @items_db.
LOOP AT update-item ASSIGNING FIELD-SYMBOL(<changed>).
ASSIGN items_db[ claim_id = <changed>-claim_id
item_no = <changed>-item_no ] TO FIELD-SYMBOL(<row>).
IF <changed>-%control-amount = if_abap_behv=>mk-on.
<row>-amount = <changed>-amount.
ENDIF.
ENDLOOP.
CALL FUNCTION 'Z_EXP_ITEM_U' EXPORTING values = items_db.
ENDIF.
" (3) keys of instances deleted during the transaction
IF delete-item IS NOT INITIAL.
items_db = CORRESPONDING #( delete-item ).
CALL FUNCTION 'Z_EXP_ITEM_D' EXPORTING values = items_db.
ENDIF.
ENDMETHOD.
ENDCLASS.
Source: RAP: Implementing the Save Handler Method and Keyword Documentation: save_modified, RAP Saver Method
The listing requires an SAP system; not run here. The table zexp_item, its fields claim_id, item_no and amount, and the
function modules Z_EXP_ITEM_C, Z_EXP_ITEM_U and Z_EXP_ITEM_D are invented for this page; the item table is not
defined anywhere else in this section yet, so treat it as an assumption. The structure comes from the SAP booking supplement
example. Why the SELECT in part 2: the update table carries only the key and the fields the consumer changed, and the
other fields hold initial values. Writing that table back would overwrite the unchanged fields with blanks, so the
example first reads the current database row and copies only the fields whose %control flag is set to
if_abap_behv=>mk-on.
The Keyword Documentation gives one more rule that is easy to miss: after writing to the database, clear the transactional buffer
(through the saver method cleanup, where it exists and can be implemented), because the same ABAP session can serve more
than one RAP transaction and remaining changes in the buffer could cause inconsistencies. The addition and cleanup, described
above, is how a behavior definition asks for that method to be redefined.
The difference between the two options, in one sentence each: additional save adds to what the framework writes, so it fits reuse services such as change documents and the application log; unmanaged save replaces what the framework writes, so it fits update function modules that already know how to persist the data.
The unmanaged type: wrapping existing code
The unmanaged type goes further: the framework gives only the frame of a business object, "the REST contract" that you must adhere to. The behavior is specified in the behavior definition but implemented by hand in the behavior pool. All existing business logic can be integrated, including create, update and delete, and you provide and handle the transactional buffer yourself. The RAP guide aims it at developers whose logic is already encapsulated in function modules: you call those function modules from the behavior methods, reuse the logic, and still get the standard RAP runtime and service enablement on top.
A behavior definition is short, because it only names what the pool will implement. This one is derived from the RAP guide’s unmanaged example (a root entity with an update operation, an ETag and a lock master), with the operations extended for the claim:
unmanaged implementation in class zbp_i_expclaim unique;
strict ( 2 );
define behavior for ZI_ExpClaim alias Claim
lock master
authorization master ( instance )
{
create;
update;
delete;
}
Source: RAP: Defining a Behavior for the Business Object and Keyword Documentation: RAP - Implementation Type
The listing requires an SAP system; not run here. Two things are different from the managed definition. There is no
persistent table, because the framework never writes. And the guide’s own example leaves create and delete out because
that scenario only updates; here they are listed, so the pool must implement them.
Every operation named in the behavior definition needs a handler method in a local handler class, which inherits from
cl_abap_behavior_handler and is created by the framework on its own. The RAP guide implements the operations as follows:
-
Changes use a method declared
FOR MODIFY, with a parameter per operation, such asentities FOR UPDATE Claim. The Keyword Documentation lists the operations aFOR MODIFYmethod can take: create, create by association, update, delete and actions. -
Reads use
FOR READ, which receives the keys and fills aRESULTtable. The method must also read from the buffer, because the ETag comparison needs the buffered data. -
Locking uses
FOR LOCK, which receives the keys of the lock master and calls a lock object, as the locking page shows. -
The save sequence is implemented in a saver class that inherits from
cl_abap_behavior_saver. The template declares redefinitions offinalize,check_before_save,save,cleanupandcleanup_finalize, and the guide’s consumption example implementssaveto write the buffer to the database.
The skeleton, after the guide’s templates:
CLASS lhc_claim DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.
METHODS create FOR MODIFY
IMPORTING entities FOR CREATE Claim.
METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE Claim.
METHODS delete FOR MODIFY
IMPORTING keys FOR DELETE Claim.
METHODS read FOR READ
IMPORTING keys FOR READ Claim RESULT result.
METHODS lock FOR LOCK
IMPORTING keys FOR LOCK Claim.
ENDCLASS.
CLASS lsc_claim DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.
METHODS finalize REDEFINITION.
METHODS check_before_save REDEFINITION.
METHODS save REDEFINITION.
METHODS cleanup REDEFINITION.
METHODS cleanup_finalize REDEFINITION.
ENDCLASS.
Source: Keyword Documentation: FOR MODIFY, RAP Handler Method, FOR READ, RAP Handler Method, RAP: Implementing the LOCK Operation and RAP: Implementing the SAVE
The listing requires an SAP system; not run here. It declares the methods only and is not complete. The implementations are
where the old code comes in. This is the RAP guide’s update method for the flight travel, which loops over the requested
changes, calls the legacy function module and reports problems, shortened:
METHOD update.
DATA: messages TYPE /dmo/t_message,
travel TYPE /dmo/travel,
travelx TYPE /dmo/s_travel_inx. "refers to x structure (> BAPIs)
LOOP AT entities ASSIGNING FIELD-SYMBOL(<travel_update>).
travel = CORRESPONDING #( <travel_update> MAPPING FROM ENTITY ).
travelx-travel_id = <travel_update>-TravelID.
travelx-_intx = CORRESPONDING #( <travel_update> MAPPING FROM ENTITY ).
CALL FUNCTION '/DMO/FLIGHT_TRAVEL_UPDATE'
EXPORTING
is_travel = CORRESPONDING /dmo/s_travel_in( travel )
is_travelx = travelx
IMPORTING
et_messages = messages.
map_messages(
EXPORTING cid = <travel_update>-%cid_ref travel_id = <travel_update>-travelid messages = messages
CHANGING failed = failed-travel reported = reported-travel ).
ENDLOOP.
ENDMETHOD.
The listing is the SAP demo code (the namespace /DMO/ is reserved for the demo content) and requires an SAP system;
not run here. The helper map_messages is a private method of the SAP sample class that turns the legacy messages into the
RAP response tables failed and reported; it is not shown in the excerpt. Read it as a pattern with four parts: map the
fields of the entity (the CDS names) to the fields of the legacy interface, call the old function module, collect its
messages, and put failures into failed and reported so the consumer sees them. The comment of the SAP source,
"refers to x structure (> BAPIs)", points at a convention of the old interfaces: next to a data structure, a second
structure of flags says which fields the caller really wants changed. The operator MAPPING FROM ENTITY USING CONTROL in the
guide’s create method does the same job with the %control structure, taking over only the flagged fields.
Wrapping a BAPI or other function module
A BAPI, in the common usage, is an old standardized function module that works on a business object of the classic SAP world, and it is the kind of module the unmanaged type is meant to wrap; the RAP guide itself talks about "function modules" and mentions BAPIs only in that code comment. The wrapping pattern is the one above and has the same shape for any module:
-
In the handler method for each operation, translate the entity fields into the module’s parameters.
-
Call the module, and collect its return messages.
-
Translate the messages into
failedandreportedentries for the consumer. -
Let the old module keep its own buffer and write to the database as it always did; the saver methods then only trigger its commit and clean-up. In the flight sample the saver methods
adjust_numbers,saveandcleanupeach call one legacy function module (for the final numbers, the commit, and the release of the legacy buffer), and nothing else.
That last point is the main thing to remember. In an unmanaged object the RAP runtime still orchestrates the interaction
phase and the save sequence: the framework calls your finalize, check_before_save, save and cleanup in the fixed
order, whatever the legacy code does inside.
Numbering in an unmanaged object
A new claim needs a key. The RAP guide’s unmanaged example uses late numbering: while the consumer works, a new instance is
identified by a temporary key, the content ID %cid, and the final value is assigned just before the save. Late numbering needs a
redefinition of the saver method adjust_numbers, which returns the mapping from the preliminary to the final keys; the
flight create method writes the content ID and the new key into the mapped response table. The guide says late numbering is
the choice when gap-free numbers are needed, because everything is checked before a number is assigned. Late numbering exists
for managed and unmanaged objects alike. The numbering options are covered on the behavior definition page and not repeated here.
Choosing between the three
The following summarizes the reasons the sources give, in the order you can use them:
-
No code exists yet: use managed. You get the buffer, the operations and the save, and you add rules with determinations, validations and actions.
-
Code for saving exists as update function modules, or the save must go to another table: use managed with unmanaged save. The interaction phase stays managed, and the RAP guide names saving to a different database table as a reason.
-
Managed is right but something extra must happen at save time, such as writing a change document or an application log entry: use managed with additional save.
-
Existing logic must be reused for the whole behavior, including create, update and delete: use unmanaged and call the existing function modules from the handler and saver methods. Expect more code, since the buffer and the operations are yours.
A managed object can be turned into an unmanaged-save object entity by entity, as the three-entity example above shows, because both options are properties of the entity, not just of the whole object. An unmanaged business object cannot use the two options: the Keyword Documentation says they are available in managed objects only, and in unmanaged objects the saving strategy is user-defined anyway.
Summary
-
Managed and unmanaged are the two implementation types of a RAP business object. They differ in who provides the transactional buffer and the standard operations: the framework (managed) or you (unmanaged).
-
Managed is for new development. You name create, update and delete in the behavior definition, and the framework writes the table given by
persistent table. -
In a managed object,
with additional saveruns your code during the save sequence besides the managed write, andwith unmanaged savereplaces the managed write. Both need a saver class that redefinessave_modified, and an unmanaged save must not name a persistent table. -
The save sequence runs
FINALIZE,CHECK_BEFORE_SAVE, optionallyADJUST_NUMBERS, then the save step, thenCOMMIT WORKandCLEANUP. The point of no return lies after the checks. -
Unmanaged is for brownfield work: you write handler methods for modify, read and lock, a buffer, and saver methods, and you call the existing function modules from them. The runtime still decides when each method is called.
-
Older text writes
implementation managed;; current syntax ismanaged implementation in class ... unique.