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 - Managed and Unmanaged Behavior Definitions → RAP - EntityBehaviorDefinition → RAP - EntityBehaviorBody → RAP - RAP BO Operations → RAP - Non-Standard Operations → RAP - action → RAP - Examples of Actions →
RAP - Action with Input Parameter
This example demonstrates how a RAP action with input parameter is defined, implemented, and consumed in a managed RAP BO.
Data model
The CDS data model consists of only one root entity DEMO_CDS_ACTION_INPUT_PARAM
. The root entity represents a purchase order item.
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'CDS view entity, managed, action'
define root view entity DEMO_CDS_ACTION_INPUT_PARAM
as select from DEMO_CDS_PURCH_DOC_I
{
key PurchaseDocumentItem,
key PurchaseDocument,
Description,
Vendor,
VendorType,
Price,
Currency,
Quantity,
QuantityUnit,
OverallItemPrice,
/* Associations */
_PurchaseDocument
}
Behavior definition
The RAP behavior definition DEMO_CDS_ACTION_INPUT_PARAM
is defined in RAP BDL as follows:
managed implementation in class bp_demo_cds_action_input_param unique;
strict(2);
define behavior for DEMO_CDS_ACTION_INPUT_PARAM alias PurchaseItem
persistent table demo_purch_doc_i
lock master
authorization master (global)
{
create;
update;
delete;
action deductDiscount parameter DEMO_CDS_ABSTRACT_DISCOUNT result [1] $self;
field(readonly:update) PurchaseDocument, PurchaseDocumentItem;
mapping for DEMO_PURCH_DOC_I corresponding;
}
Action definition
The behavior definition defines one action: deductDiscount
. The action has one input parameter that is typed with the CDS abstract entity DEMO_CDS_ABSTRACT_DISCOUNT
and it defines the output parameter as the same type as the entity for which the action is executed. The purpose is to reduce the purchase order item price by the discount specified via the input parameter.
action deductDiscount
parameter DEMO_CDS_ABSTRACT_DISCOUNT
result [1] $self;
The following CDS abstract entity is used for typing the input parameter:
@EndUserText.label: 'CDS abstract entity, discount'
define abstract entity DEMO_CDS_ABSTRACT_DISCOUNT
{
discount_percent : abap.int1;
}
Behavior implementation
For the above RAP behavior definition, one ABAP behavior pool (ABP) is created. The global class of the behavior pool is BP_DEMO_CDS_ACTION_INPUT_PARAM
. This global class implements the local handler class LHC_DEMO_CDS_ACTION_INPUT_PARA
, which contains the method deductDiscount
to implement the action. This method is a FOR MODIFY
method and it is typed based on the PurchaseItem
entity. Its signature includes the result parameter. The actual implementation takes place in the BP_DEMO_CDS_ACTION_INPUT_PARAMCCIMP
.
Source Code
* Public class definition
CLASS cl_demo_cds_action_reduce DEFINITION
INHERITING FROM cl_demo_classrun
PUBLIC
CREATE PUBLIC.
PUBLIC SECTION.
METHODS main REDEFINITION.
ENDCLASS.
* Public class implementation
CLASS cl_demo_cds_action_reduce IMPLEMENTATION.
METHOD main.
DATA(percentage) = 10.
cl_demo_input=>new(
)->add_field( CHANGING field = percentage
)->request( ).
IF percentage > 100
OR percentage < 0.
out->write( 'Enter a valid percentage between 0 and 100' ).
ENDIF.
DELETE FROM demo_purch_doc_i.
TYPES: BEGIN OF structure,
PurchaseDocumentItem TYPE c LENGTH 10,
PurchaseDocument TYPE c LENGTH 10,
Price TYPE i,
END OF structure.
MODIFY ENTITIES OF demo_cds_action_input_param
ENTITY PurchaseItem
CREATE FIELDS ( PurchaseDocumentItem PurchaseDocument
Price ) WITH VALUE #(
( %cid = 'cid1'
PurchaseDocumentItem = 'a'
PurchaseDocument = '1'
Price = 200 )
( %cid = 'cid2'
PurchaseDocumentItem = 'b'
PurchaseDocument = '2'
Price = 999 ) )
FAILED DATA(failed)
REPORTED DATA(reported).
DATA no_discount TYPE SORTED TABLE OF structure
WITH UNIQUE KEY PurchaseDocumentItem PurchaseDocument.
READ ENTITY demo_cds_action_input_param
ALL FIELDS WITH VALUE #(
( PurchaseDocumentItem = 'a' PurchaseDocument = '1' )
( PurchaseDocumentItem = 'b' PurchaseDocument = '2' ) )
RESULT FINAL(no_discount1).
MOVE-CORRESPONDING no_discount1 TO no_discount.
out->next_section( 'full price'
)->write( no_discount ).
TRY.
MODIFY ENTITIES OF demo_cds_action_input_param
ENTITY PurchaseItem
EXECUTE deductDiscount
FROM VALUE #( ( PurchaseDocumentItem = 'a' PurchaseDocument = '1'
%param-discount_percent = percentage )
( PurchaseDocumentItem = 'b' PurchaseDocument = '2'
%param-discount_percent = percentage ) )
RESULT FINAL(result)
FAILED failed
REPORTED reported.
CATCH cx_sy_conversion_overflow INTO FINAL(exc).
out->write( 'percentage not accepted' ).
ENDTRY.
COMMIT ENTITIES RESPONSE OF demo_cds_action_input_param
FAILED FINAL(commit_failed)
REPORTED FINAL(commit_reported).
IF sy-subrc <> 0.
out->write_doc( `An issue occurred in the RAP save sequence.` ).
ENDIF.
SELECT purchasedocumentitem, purchasedocument, Price
FROM demo_purch_doc_i
INTO TABLE @FINAL(demo_discount).
out->next_section( 'price with discount'
)->write( percentage
)->next_section( 'amount with discount'
)->write( demo_discount ).
ENDMETHOD.
ENDCLASS.
Example Description
Access with ABAP using EML
The above source code uses EML to access the RAP business object from an ABAP class:
- The user is requested to enter the discount percentage.
- Two BO instances are created with the statement
MODIFY ENTITY
. - The action
deductDiscount
is executed on one of the two instances with the statementEXECUTE
. - The
result
,failed
, andreported
parameters are returned. - The changes that were performed by the action in the transactional buffer are committed to the database using the statement
COMMIT ENTITIES RESPONSE OF
. - A
SELECT
statement is used to read the changed data from the persistent tableDEMO_PURCH_DOC_I
. As a result of the action, the price of a purchase order item is reduced by the percentage specified via the input parameter.