Hello Experts,
In this blog, We will learn how to create a simple RAP OData API and use it to achieve a Deep entity set creation operation which is requested in lot of projects and Custom services especially in complex integration scenarios .
In this example, we will be using a custom cds view entities based on standard tables for header and items, unmanaged behavior definition, service definition and binding .
Step 1 : Parent CDS :
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Production order Cds view'
@Metadata.ignorePropagatedAnnotations: true
define root view entity z7pi_production_order
as select from aufk as _order
composition [1..*] of z7pi_serial_number as _serial
{
key aufnr as Production_order,
auart,
_serial
}
Step 2 : Child CDS :
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'serial Number Cds view'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define view entity z7pi_serial_number
as select from objk
association [0..1] to ser05 on objk.obknr = ser05.obknr
association [0..1] to ser06 on objk.obknr = ser06.obknr
association to parent z7pi_production_order as _order
on $projection.Production_order = _order.Production_order
{
key ser05.ppaufart as Production_order,
key objk.sernr as serial,
key ser06.exidv as hu,
_order
}
Step 3 : Unmanaged Behavior Definition :
unmanaged implementation in class z7pbp_7pi_production_order unique;
strict ( 2 );
define behavior for z7pi_production_order alias _order
//late numbering
lock master
authorization master ( instance )
//etag master <field_name>
{
field ( features : instance ) auart ;
create;
update;
delete;
association _serial { create; }
}
define behavior for z7pi_serial_number alias _serial
//late numbering
lock dependent by _order
authorization dependent by _order
//etag master <field_name>
{
update;
delete;
field ( readonly ) Production_order;
association _order;
}
Step 4 : using quick fix create the implementation Class
Step 5 : comment or remove the create and cba_association create methods
we remove both methods and create a new method using the events from the removed ones
CLASS lhc__order DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.
METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
IMPORTING keys REQUEST requested_authorizations FOR _order RESULT result.
* METHODS create FOR MODIFY
* IMPORTING entities FOR CREATE _order.
METHODS deep_create FOR MODIFY
IMPORTING
order FOR CREATE _order
serial FOR CREATE _order\_serial.
METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE _order.
METHODS delete FOR MODIFY
IMPORTING keys FOR DELETE _order.
METHODS read FOR READ
IMPORTING keys FOR READ _order RESULT result.
METHODS lock FOR LOCK
IMPORTING keys FOR LOCK _order.
METHODS rba_serial FOR READ
IMPORTING keys_rba FOR READ _order\_serial FULL result_requested RESULT result LINK association_links.
METHODS get_instance_features FOR INSTANCE FEATURES
IMPORTING keys REQUEST requested_features FOR _order RESULT result.
* METHODS cba_serial FOR MODIFY
* IMPORTING entities_cba FOR CREATE _order\_serial.
ENDCLASS.
then in the implementation of the new method we should be able to access both the header and child entities
METHOD deep_create.
DATA(lv_import) = order.
DATA(lv_import_sr) = serial.
LOOP AT lv_import_sr ASSIGNING FIELD-SYMBOL(<sr>) .
LOOP AT <sr>-%target ASSIGNING FIELD-SYMBOL(<tr>).
wa_order_ser = VALUE #(
%key-production_order = lv_ord_no
%key-serial = <tr>-serial
production_order = lv_ord_no
).
wa_report_ser = VALUE #(
%key-production_order = lv_ord_no
%key-serial = <tr>-serial
production_order = lv_ord_no
).
wa_fail_ser = VALUE #(
%key-production_order = lv_ord_no
%key-serial = <tr>-serial
production_order = lv_ord_no
).
SELECT SINGLE obknr
FROM objk
WHERE sernr EQ @<tr>-serial
AND taser EQ 'SER05'
INTO (obj_list).
ENDLOOP.
ENDLOOP.
ENDMETHOD.
Finally we create the service binding and service definition
@EndUserText.label: 'Blocking zrep order type serials'
define service Z7PAPI_ZREP_BLOCK {
expose z7pi_production_order as prd_ord;
expose z7pi_serial_number as serial;
}
then we move to testing
as shown you can access your header and child entities however you see fit
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 22 | |
| 19 | |
| 12 | |
| 11 | |
| 11 | |
| 10 | |
| 9 | |
| 9 | |
| 8 | |
| 8 |