Introduction: - SAP’s Restful ABAP Programming Model (RAP), creating and managing complex transactional applications has become simpler and more structured. One of the powerful capabilities of RAP is Deep Insert, which allows you to handle hierarchical data structures (like headers and items) in a single API call. This feature is particularly useful for scenarios such as creating a Header item with multiple line items or managing a parent-child relationship in one transaction.
In this blog, we will explore Deep Insert in RAP. We will walk through creating a RAP-based application with a deep insert to handle hierarchical data in a single request.
Steps to create the deep insert: -
Step 1: -Create Two Database table (One header and second item): -
A)- Header table. (Inside header table it must to use the data element for the field which is going to use in foreign key relationship)
@EndUserText.label : 'Header table'
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zam_dt_emp_headr {
key client : abap.clnt not null;
key emp_id : zam_de_emp_id not null;
emp_name : abap.char(20);
emp_education_college : abap.char(20);
}
B) Item table .
@EndUserText.label : 'Itemtable for scn blog'
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zam_dt_emp_item {
key client : abap.clnt not null;
@AbapCatalog.foreignKey.screenCheck : false
key emp_id : zam_de_emp_id not null
with foreign key [0..*,1] zam_dt_emp_headr
where emp_id = zam_dt_emp_item.emp_id;
key ph_no : abap.numc(10) not null;
emp_address : abap.char(50);
}
Step 2: -On top of Database view, we have create the Interface view for both the header and for item table: -
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Interface view'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define root view entity ZAM_I_EMP_HEADER
as select from zam_dt_emp_headr
composition [0..*] of ZAM_i_EMP_ITEM as _item
{
key zam_dt_emp_headr.emp_id as EmpId,
zam_dt_emp_headr.emp_name as EmpName,
zam_dt_emp_headr.emp_education_college as EmpEducationCollege,
_item
}
B) -Interface view for item table: -
@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Interface view'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
define view entity ZAM_I_EMP_ITEM
as select from zam_dt_emp_item
association to parent ZAM_I_EMP_HEADER as _header on $projection.EmpId = header.EmpId
{
key zam_dt_emp_item.emp_id as EmpId,
key zam_dt_emp_item.ph_no as PhNo,
zam_dt_emp_item.emp_address as EmpAddress,
_header
}
Step 3: -On top of interface view we have to create the projection view for both header and item view.
A) - Header projection view: -
@EndUserText.label: 'Projection view for scn blog'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define root view entity ZAM_P_EMP_HEADER
as projection on ZAM_I_EMP_HEADER
{
@UI.facet: [{
purpose: #STANDARD,
position: 10,
label: 'Header',
type: #IDENTIFICATION_REFERENCE
},
{
purpose: #STANDARD,
position: 20,
label: 'Item',
type: #LINEITEM_REFERENCE,
targetElement: '_item'
}]
@UI.lineItem: [{ position: 10, label: 'Emp Id' }]
@UI.identification: [{ position: 10, label: 'Emp Id' }]
key EmpId,
@UI.lineItem: [{ position: 20, label: 'Emp Name' }]
@UI.identification: [{ position: 20, label: 'Emp Name' }]
EmpName,
@UI.lineItem: [{ position: 30, label: 'Emp Education College' }]
@UI.identification: [{ position: 30, label: 'Emp Education College' }]
EmpEducationCollege,
_item : redirected to composition child ZAM_P_EMP_ITEM
}
B)- Projection view item table: -
@EndUserText.label: 'Projection view for item table'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity ZAM_P_EMP_ITEM
as projection on ZAM_I_EMP_ITEM
{
@UI.facet: [{
purpose: #STANDARD,
position: 10,
label: 'Item',
type: #IDENTIFICATION_REFERENCE
}]
@UI.lineItem: [{ position: 10, label: 'Emp Id' }]
@UI.identification: [{ position: 10, label: 'Emp Id' }]
key EmpId,
@UI.lineItem: [{ position: 20, label: 'Emp Phone' }]
@UI.identification: [{ position: 20, label: 'Emp Phone' }]
key PhNo,
@UI.lineItem: [{ position: 30, label: 'Emp Address' }]
@UI.identification: [{ position: 30, label: 'Emp Address' }]
EmpAddress,
_header : redirected to parent ZAM_P_EMP_HEADER
}
Step 4: - On top of Header interface view we have to create the behaviour definition: -
managed implementation in class zbp_am_i_emp_header unique;
define behavior for ZAM_I_EMP_HEADER //alias <alias_name>
persistent table zam_dt_emp_headr
lock master
{
create;
update;
delete;
association _item { create; }
field ( readonly : update ) EmpId;
mapping for zam_dt_emp_headr
{
EmpId = emp_id;
EmpName = emp_name;
EmpEducationCollege = emp_education_college ;
}
}
define behavior for ZAM_i_emp_ITEM //alias <alias_name>
persistent table zam_dt_emp_item
lock dependent by _header
{
update;
delete;
field ( readonly : update ) EmpId ;
association _header { }
mapping for zam_dt_emp_item
{
EmpId = emp_id;
PhNo = ph_no;
EmpAddress = emp_address;
}
}
Step 5: - On top of header projection view we have to create the behaviour definition.
projection;
define behavior for ZAM_P_EMP_HEADER //alias <alias_name>
{
use create;
use update;
use delete;
use association _item { create; }
}
define behavior for ZAM_P_EMP_ITEM //alias <alias_name>
{
use update;
use delete;
use association _header;
}
Step 6: - Service definition.
@EndUserText.label: 'Service defination'
define service ZAM_SERV_DEFINATION {
expose ZAM_P_EMP_HEADER;\
expose ZAM_P_EMP_ITEM;
}
Step 7: - On top service definition.
Output screen: -
Creating the header records
Record saves in Header database
Creating multiple item records for each header records.
Record saves in Item database .
Thanks for watching this blogs.