RAP Unmanaged Scenario with CRUD Implementations
Share

[[{“value”:”

Hello Everyone,

In this blog, we will explore a basic example of how to implement unmanaged CRUD operations in SAP RAP for an on-premise system.


Step 1: Creating a Database Table

@EndUserText.label : ‘Employee Table’
@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zun_emp_tab {
key client : abap.clnt not null;
key emp_id : zempid not null;
first_name : abap.char(30);
last_name : abap.char(30);
date_of_birth : abap.dats;
email : abap.sstring(60);
hire_date : abap.dats;
@Semantics.amount.currencyCode : ‘zun_emp_tab.curry’
salary : abap.curr(15,2);
curry : abap.cuky;
}

Step 2: Create a Root View for the Table

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Interface view’
@Metadata.ignorePropagatedAnnotations: true
define root view entity zun_emp_root as select from zun_emp_tab
{
key emp_id as EmpId,
first_name as FirstName,
last_name as LastName,
date_of_birth as DateOfBirth,
email as Email,
hire_date as HireDate,
@Semantics.amount.currencyCode: ‘curry’
salary as Salary,
curry as Curry
}

Step 3: Create a Projection View for the Root View

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: ‘Consumption View’
@Metadata.ignorePropagatedAnnotations: true
@Metadata.allowExtensions: true
define root view entity zun_emp_proj as projection on zun_emp_root
{
key EmpId,
FirstName,
LastName,
DateOfBirth,
Email,
HireDate,
@Semantics.amount.currencyCode: ‘Curry’
Salary,
Curry
}

Step 4: Create a Behavior Definition for the Root View

unmanaged implementation in class zbp_un_emp_root unique;
strict ( 2 );
define behavior for zun_emp_root alias EMP
late numbering
lock master
authorization master ( instance )
//etag master <field_name>
{
create;
update;
delete;
}

Step 5: Create a Behavior Definition for the Projection View

projection;
strict ( 2 );

define behavior for zun_emp_proj alias EMP
{
use create;
use update;
use delete;
}

Step 6: Create a Metadata Extension for the Projection View

@Metadata.layer: #CUSTOMER

@UI.headerInfo: {
typeName: ‘Employee’,
typeNamePlural: ‘Employees’,
title: {
type: #STANDARD,
label: ‘Employee ID’,
value: ‘EmpId’
},
description: {
label: ‘First Name’,
value: ‘FirstName’
}
}

annotate view zun_emp_proj with {

//=========================
// Facets (sections)
//=========================
.facet: [
{
id: ‘GeneralInfo’,
type: #IDENTIFICATION_REFERENCE,
label: ‘General Information’,
position: 10
}
]

//=========================
// General Information Fields
//=========================
.identification: [{ position: 10, label: ‘Employee ID’ }]
.selectionField: [{ position: 10 }]
.lineItem: [{ position: 10 }]
@EndUserText.label: ‘Employee ID’
EmpId;

.identification: [{ position: 20, label: ‘First Name’ }]
.selectionField: [{ position: 20 }]
.lineItem: [{ position: 20 }]
@EndUserText.label: ‘First Name’
FirstName;

.identification: [{ position: 30, label: ‘Last Name’ }]
.selectionField: [{ position: 30 }]
.lineItem: [{ position: 30 }]
@EndUserText.label: ‘Last Name’
LastName;

.identification: [{ position: 40, label: ‘Date of Birth’ }]
.lineItem: [{ position: 40 }]
.selectionField: [{ position: 40 }]
@EndUserText.label: ‘Date of Birth’
DateOfBirth;

.identification: [{ position: 50, label: ‘Email’ }]
.lineItem: [{ position: 50 }]
@EndUserText.label: ‘Email’
Email;

.identification: [{ position: 60, label: ‘Hire Date’ }]
.selectionField: [{ position: 50 }]
.lineItem: [{ position: 60 }]
@EndUserText.label: ‘Hire Date’
HireDate;

.identification: [{ position: 70, label: ‘Salary’ }]
.lineItem: [{ position: 70 }]
@EndUserText.label: ‘Salary’
Salary;

.identification: [{ position: 80, label: ‘Currency’ }]
.lineItem: [{ position: 80 }]
@EndUserText.label: ‘Currency’
Curry;

};

Step 7: Implement a Class for the Root View and Write Logic for CRUD Operations

” Buffer class to hold create/update/delete entries temporarily
CLASS lcl_buffer DEFINITION CREATE PRIVATE.
PUBLIC SECTION.
TYPES: BEGIN OF ty_buffer,
flag TYPE c LENGTH 1, ” ‘C’ = Create, ‘U’ = Update, ‘D’ = Delete
lv_data TYPE zun_emp_tab,
END OF ty_buffer.

CLASS-DATA mt_buffer TYPE STANDARD TABLE OF ty_buffer WITH EMPTY KEY.

CLASS-METHODS get_instance
RETURNING VALUE(ro_instance) TYPE REF TO lcl_buffer.

METHODS add_to_buffer
IMPORTING
iv_flag TYPE c
is_employee TYPE zun_emp_tab.

PRIVATE SECTION.
CLASS-DATA go_instance TYPE REF TO lcl_buffer.
ENDCLASS.

CLASS lcl_buffer IMPLEMENTATION.

METHOD get_instance.
IF go_instance IS NOT BOUND.
go_instance = NEW #( ).
ENDIF.
ro_instance = go_instance.
ENDMETHOD.

METHOD add_to_buffer.
INSERT VALUE #( flag = iv_flag lv_data = is_employee ) INTO TABLE mt_buffer.
ENDMETHOD.

ENDCLASS.

CLASS zcx_my_exception DEFINITION INHERITING FROM cx_static_check.
PUBLIC SECTION.
INTERFACES if_t100_message.
METHODS constructor IMPORTING
textid LIKE if_t100_message=>t100key OPTIONAL.

ENDCLASS.

CLASS zcx_my_exception IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor
EXPORTING
previous = previous.
me->if_t100_message~t100key = textid.
ENDMETHOD.
ENDCLASS.

” Behavior Handler Class for EMP entity
CLASS lhc_EMP DEFINITION INHERITING FROM cl_abap_behavior_handler.
PRIVATE SECTION.
METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
IMPORTING keys REQUEST requested_authorizations FOR emp RESULT result.

METHODS create FOR MODIFY
IMPORTING entities FOR CREATE emp.

METHODS update FOR MODIFY
IMPORTING entities FOR UPDATE emp.

METHODS delete FOR MODIFY
IMPORTING keys FOR DELETE emp.

METHODS read FOR READ
IMPORTING keys FOR READ emp RESULT result.

METHODS lock FOR LOCK
IMPORTING keys FOR LOCK emp.
ENDCLASS.

CLASS lhc_EMP IMPLEMENTATION.

METHOD get_instance_authorizations.
” Implement authorization logic if needed
ENDMETHOD.

METHOD create.
DATA(lo_buffer) = lcl_buffer=>get_instance( ).

LOOP AT entities ASSIGNING FIELD-SYMBOL(<ls_entity>).
DATA ls_employee TYPE zun_emp_tab.

” Map entity fields to employee structure
ls_employee-emp_id = <ls_entity>-EmpId.
ls_employee-first_name = <ls_entity>-FirstName.
ls_employee-last_name = <ls_entity>-LastName.
ls_employee-date_of_birth = <ls_entity>-DateOfBirth.
ls_employee-email = <ls_entity>-email.
ls_employee-hire_date = <ls_entity>-HireDate.
ls_employee-salary = <ls_entity>-salary.
ls_employee-curry = <ls_entity>-curry.

” Add to buffer as create
lo_buffer->add_to_buffer( iv_flag = ‘C’ is_employee = ls_employee ).

” Return mapped keys (required by RAP)
INSERT VALUE #( %cid = <ls_entity>-%cid empID = ls_employee-emp_id ) INTO TABLE mapped-emp.
ENDLOOP.
ENDMETHOD.

METHOD update.
DATA(lo_buffer) = lcl_buffer=>get_instance( ).

LOOP AT entities ASSIGNING FIELD-SYMBOL(<ls_entity>).
” Fetch existing employee from DB to buffer with flag U
DATA ls_db TYPE zun_emp_tab.
SELECT SINGLE * FROM zun_emp_tab WHERE emp_id = @<ls_entity>-EmpId INTO _db.
” Overwrite fields if updated in <ls_entity>
IF <ls_entity>-FirstName IS NOT INITIAL.
ls_db-first_name = <ls_entity>-FirstName.
ENDIF.
IF <ls_entity>-LastName IS NOT INITIAL.
ls_db-last_name = <ls_entity>-LastName.
ENDIF.
IF <ls_entity>-DateOfBirth IS NOT INITIAL.
ls_db-date_of_birth = <ls_entity>-DateOfBirth.
ENDIF.
IF <ls_entity>-email IS NOT INITIAL.
ls_db-email = <ls_entity>-email.
ENDIF.
IF <ls_entity>-HireDate IS NOT INITIAL.
ls_db-hire_date = <ls_entity>-HireDate.
ENDIF.
IF <ls_entity>-salary IS NOT INITIAL.
ls_db-salary = <ls_entity>-salary.
ENDIF.
IF <ls_entity>-curry IS NOT INITIAL.
ls_db-curry = <ls_entity>-curry.
ENDIF.

” Add updated employee to buffer with flag ‘U’
lo_buffer->add_to_buffer( iv_flag = ‘U’ is_employee = ls_db ).

” Return mapped keys
INSERT VALUE #( %cid = <ls_entity>-%cid_ref empID = ls_db-emp_id ) INTO TABLE mapped-emp.
ENDLOOP.
ENDMETHOD.

METHOD delete.
DATA(lo_buffer) = lcl_buffer=>get_instance( ).

LOOP AT keys ASSIGNING FIELD-SYMBOL(<ls_key>).
” Create minimal structure for deletion
DATA ls_employee TYPE zun_emp_tab.
ls_employee-emp_id = <ls_key>-EmpId.

” Add to buffer with flag ‘D’
lo_buffer->add_to_buffer( iv_flag = ‘D’ is_employee = ls_employee ).

” Return mapped keys
INSERT VALUE #( %cid = <ls_key>-%cid_ref empID = <ls_key>-EmpId ) INTO TABLE mapped-emp.
ENDLOOP.
ENDMETHOD.

METHOD read.
” Implement read logic here if required
ENDMETHOD.

METHOD lock.
” Implement lock logic if required
ENDMETHOD.

ENDCLASS.

” Saver class to persist data with validation and proper error handling
CLASS lsc_ZUN_EMP_ROOT DEFINITION INHERITING FROM cl_abap_behavior_saver.
PROTECTED SECTION.
METHODS finalize REDEFINITION.
METHODS check_before_save REDEFINITION.
METHODS adjust_numbers REDEFINITION.
METHODS save REDEFINITION.
METHODS cleanup REDEFINITION.
METHODS cleanup_finalize REDEFINITION.
ENDCLASS.

CLASS lsc_ZUN_EMP_ROOT IMPLEMENTATION.

METHOD finalize.
” Optional post-save logic
ENDMETHOD.

METHOD check_before_save.
” Optional pre-save validations
ENDMETHOD.

METHOD adjust_numbers.

ENDMETHOD.

METHOD save.
DATA(lo_buffer) = lcl_buffer=>get_instance( ).
LOOP AT lo_buffer->mt_buffer ASSIGNING FIELD-SYMBOL(<ls_buf>).
CASE <ls_buf>-flag.
WHEN ‘C’.
INSERT zun_emp_tab FROM <ls_buf>-lv_data.
WHEN ‘U’.
UPDATE zun_emp_tab FROM <ls_buf>-lv_data.
WHEN ‘D’.
DELETE FROM zun_emp_tab WHERE emp_id = <ls_buf>-lv_data-emp_id.
ENDCASE.
ENDLOOP.
CLEAR lo_buffer->mt_buffer.
ENDMETHOD.

METHOD cleanup.
” Optional cleanup logic
ENDMETHOD.

METHOD cleanup_finalize.
” Optional final cleanup
ENDMETHOD.

ENDCLASS.

Step 8: Create a Service Definition for the Projection View

@EndUserText.label: ‘Service defination For Projection’
define service ZUN_SRV_DD {
expose zun_emp_proj as EMP;
}

Step 9: Create a Service Binding for the Service Definition

manijangiti_0-1761568261790.png

Step 10: Now, Let’s Preview and Test All CRUD Operations
Output of the Application on Read when the “Go” button is pressed

manijangiti_1-1761568417027.png

Creating a Record

manijangiti_2-1761568614868.png

Giving Required Data And Press Create 

manijangiti_3-1761568731612.png

Successfully Created a Record

manijangiti_4-1761568815818.png

manijangiti_5-1761568897922.png

Updating  a Record 

manijangiti_6-1761569226689.png

manijangiti_7-1761569274424.png

manijangiti_8-1761569324456.png

Deleting the Record

manijangiti_9-1761569429892.png

manijangiti_10-1761569470336.png

manijangiti_11-1761569544963.png

Record Deleted successfully

 

“}]] 

  Read More Technology Blog Posts by Members articles 

#abap

By ali

Leave a Reply