ABAP for Cloud Development
AS ABAP Release 914, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP RAP Business Objects → RAP - Behavior Definitions → RAP - BDL for Behavior Definitions → RAP - Projection Behavior Definitions → RAP - BehaviorDefinitionHeader, Projection BDEF →
RAP - with managed instance filter
Syntax
...
with managed instance filter;
...
Description
Optional addition for projection BDEFs and interface BDEFs. It is specified in the behavior definition header and it is valid for all RAP BO entities that are part of the current RAP BO.
The effect is that the WHERE
condition of the underlying CDS transactional query or CDS transactional interface is evaluated when the BDEF is accessed with ABAP EML or OData requests from Web clients, both for active and for draft instances. If an entity within the BDEF does not have a WHERE
clause, this addition does not have any effect, but as soon as a WHERE
clause is added, the checks are activated.
If this addition is not specified, the WHERE
clause is ignored when the BDEF is accessed with ABAP EML or OData requests from Web clients. This means that it is possible to read and modify entity instances that are not part of the result set of the projection BDEF or interface BDEF in question.
The instances for the following operations are checked before every EML or OData call:
- Read operation
- Update operation
- Delete operation
- Executing instance RAP BO operations
- Read by association operation
- EML permissions (
GET PERMISSIONS
)
If an entity instance does not fulfill the WHERE
clause, it is returned in the response parameter FAILED
. It is not passed to the RAP handler method of the operation in question.
The WHERE
clause is evaluated and the instance check takes place before the RAP handler method is called but after the RAP BO augmentation, so the RAP BO provider can modify incoming requests with the operation augmentation in such a way that they fulfill the WHERE
clause, for example, if fields are defined as readonly
in the projection.
During an update request, if instances are modified in a way that they no longer fulfill the WHERE
clause of the underlying CDS entity, then no further operations on these instances are possible via the current projection or interface BDEF.
For the operations create and create-by-association, the RAP BO provider must ensure via RAP handler methods and the operation augmentation that only valid entity instances are created. The managed RAP BO provider does not evaluate the WHERE
clause for these operations. Therefore, creating RAP BO entity instances that do not fulfill the WHERE
condition is possible, but no further operations on these instances are possible.
In the case of the read-by-association operation, the requested RAP BO entity instances of the association target instances are checked after every EML or OData call. If an entity instance does not fulfill the WHERE
clause of the child entity, it is removed from the RESULT
and LINK
set, but it is not added to the response parameter FAILED
.
If a projection BDEF is based on an interface BDEF and the underlying projection views both have a WHERE
clause, then always the requested entity decides whether the instance filter is active or not. For example, if only the projection BDEF specifies with managed instance checks
and a RAP BO consumer requests this projection BDEF, then both WHERE
clauses (the one for the interface and the one for the projection) are evaluated and instances are filtered out.
Hints
- The addition
with managed instance checks
can considerably slow down read and modify requests. Therefore, it should be used only after careful consideration. - When using
IN LOCAL MODE
,with managed instance checks
does not have any effect.
Example
The following example demonstrates the effect of the addition with managed instance filter
in a projection BDEF.
First of all, it demonstrates that the WHERE
clause of a CDS transactional interface is ignored in an ABAP EML call if the addition with managed instance filter
is not specified.
CDS transactional interface that uses a WHERE
clause to filter the result set to include only instances with that have the value AA in the field carrid
.
@EndUserText.label: 'CDS transactional query'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define root view entity DEMO_RAP_INSTANCE_FILTER_TQ
provider contract transactional_query
as projection on DEMO_RAP_INSTANCE_FILTER
{
key carrid,
key connid,
countryfr
}
where carrid = 'AA'
Projection BDEF without managed instance filter:
projection;
strict ( 2 );
with managed instance filter;
define behavior for DEMO_RAP_INSTANCE_FILTER_TQ alias Root
{
use create;
use update;
use delete;
}
Accessing the projection BDEF:
- The projection BDEF is first accessed using ABAP SQL. The
WHERE
condition of the transactional interface is evaluated and the result set contains only instances that have the value AA in the fieldcarrid
. - Then, the projection BDEF is accessed using ABAP EML. The
WHERE
condition is not evaluated and the result set also contains an instance with the value LH in the fieldcarrid
.
"ABAP SQL read on projection
SELECT FROM DEMO_RAP_INSTANCE_FILTER_TQ
FIELDS carrid, connid, countryfr
INTO TABLE @FINAL(select_res).
"ABAP EML read entity on projection
READ ENTITY DEMO_RAP_INSTANCE_FILTER_TQ
ALL FIELDS WITH VALUE #(
( carrid = 'AA' connid = '0017' )
( carrid = 'AA' connid = '0064' )
( carrid = 'LH' connid = '0400' )
)
RESULT DATA(read_res)
FAILED DATA(read_failed).
FINAL(o) = cl_demo_output=>new( ).
o->next_section( 'SELECT with ABAP SQL'
)->write( select_res
)->next_section( 'READ with ABAP EML'
)->write( read_res
)->display( ).
Result: The entity instance highlighted in red should not be part of the result set. It should be filtered out by the WHERE
clause.
If, however, the projection BDEF specified the addition with managed instance filter
, the WHERE
clause is evaluated.
Projection BDEF with managed instance filter:
projection;
strict ( 2 );
with managed instance filter;
define behavior for DEMO_RAP_INSTANCE_FILTER_TQ1
{
use create;
use update;
use delete;
}
Accessing the projection BDEF with managed instance filter:
- The projection BDEF with a managed instance filter is accessed first using ABAP SQL and then using ABAP EML. The
WHERE
clause is evaluated in both cases and the result set is identical.
"ABAP SQL read on projection
SELECT FROM demo_rap_instance_filter_tq1
FIELDS carrid, connid, countryfr
INTO TABLE @FINAL(select_res1).
"ABAP EML read entity on projection
READ ENTITY demo_rap_instance_filter_tq1
FIELDS ( carrid connid countryfr )
WITH VALUE #( ( %key-carrid = 'AA ' %key-connid = '0017' )
( %key-carrid = 'AA' %key-connid = '0064' )
( %key-carrid = 'LH' %key-connid = '0400' ) )
RESULT DATA(read_res1)
FAILED DATA(read_failed1).
FINAL(o) = cl_demo_output=>new( ).
o->next_section( 'SELECT with ABAP SQL'
)->write( select_res1
)->next_section( 'READ with ABAP EML'
)->write( read_res1
)->display( ).
- Result: