KEMBAR78
ABAP RAP Tutorial - Part 1 | PDF | Software | Software Engineering
0% found this document useful (0 votes)
26 views40 pages

ABAP RAP Tutorial - Part 1

This document provides a step-by-step guide for creating ABAP RAP UI applications using a managed scenario, including sample ABAP code for a fully functional application. It covers various components such as database tables, behavior definitions, service definitions, and user interface annotations. The guide aims to facilitate the implementation of functionalities like navigation, CUD operations, and handling attachments.

Uploaded by

Kedar Goje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views40 pages

ABAP RAP Tutorial - Part 1

This document provides a step-by-step guide for creating ABAP RAP UI applications using a managed scenario, including sample ABAP code for a fully functional application. It covers various components such as database tables, behavior definitions, service definitions, and user interface annotations. The guide aims to facilitate the implementation of functionalities like navigation, CUD operations, and handling attachments.

Uploaded by

Kedar Goje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Objective: The aim of this document is to show you how to create ABAP RAP UI
applications using Managed scenario. The document provides working ABAP Code as
reference for implementing a fully functional application with navigation to object
page, child entities, CUD functionalities, Behavior implementation for Actions,
Determinations and Validations, CDS Search Helps, Virtual Elements and handling
attachments.

1
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Contents:

1. Database Tables … 3
2. ABAP console program to maintain test data … 4
3. CDS Data Definitions … 6
4. CDS Value Helps … 9
5. Metadata extension with UI annotations … 10
6. Behavior Definition … 12
7. Implementation of Behavior Definition … 13
8. Service Definition … 15
9. Service Binding … 16
10. Handling CUD operations for Child Entities … 20
11. Demo on Advanced Feature Control …
12. Virtual Elements … 23
13. Demo on Upload Attachments … 32
14. Reference … 39
15. About the author … 40

2
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Step 1: Code to create custom tables (The data element details have not been
provided, these can be created in system as required) *

@EndUserText.label : 'Student Master Record'


@AbapCatalog.enhancement.category : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zpsb_stud_master {

key client : abap.clnt not null;


key aadhar : zpsb_aadhar not null;
fname : zpsb_first_name;
mname : zpsb_middle_name;
lname : zpsb_last_name;
dob : zpsnb_dob;
plofbirth : zpsb_place_of_birth;
class : zpsb_class;
contact : zpsb_contact;
email : zpsb_email;
dtofadmission : zpsb_date_of_admission;
@EndUserText.label : 'Status of Admission'
admissionstatus : abap.char(10);

@EndUserText.label : 'Student-Parent Record'


@AbapCatalog.enhancement.category : #EXTENSIBLE_ANY
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zpsb_stud_parent {

key client : abap.clnt not null;


key studentaadhar : zpsb_aadhar not null;
key parenttype : zpsb_parent_type not null;
fname : zpsb_first_name;
mname : zpsb_middle_name;
lname : zpsb_last_name;
qualification : zpsb_qualification;
occupation : zpsb_occupation;
contact : zpsb_contact;
email : zpsb_email;
salary : abap.dec(15,2);
currency : abap.cuky;

}
3
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

@EndUserText.label : 'Status of Admission'


@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #ALLOWED
define table zpsb_adm_stat {

key client : abap.clnt not null;


key status : abap.char(10) not null;

@EndUserText.label : 'Offered Classes'


@AbapCatalog.enhancement.category : #NOT_EXTENSIBLE
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zpsb_classes {

key client : abap.clnt not null;


key class : zpsb_class not null;

Step 2: Code to maintain data in the custom tables created above for initial setup.

CLASS zcl_psb_maintain_data DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .

PUBLIC SECTION.

INTERFACES if_oo_adt_classrun . Commented [BP1]: ABAP console program


PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.

CLASS zcl_psb_maintain_data IMPLEMENTATION.

METHOD if_oo_adt_classrun~main.
DATA: lt_classes TYPE STANDARD TABLE OF zpsb_classes WITH DEFAULT KEY,
lw_classes TYPE zpsb_classes,
lt_admission_stat TYPE STANDARD TABLE OF zpsb_adm_stat WITH DEFAULT KEY,
lt_studentmaster TYPE STANDARD TABLE OF zpsb_stud_master,
4

lt_studentparent TYPE STANDARD TABLE OF zpsb_stud_parent.


Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

DELETE FROM zpsb_classes.


DELETE FROM zpsb_stud_master.
DELETE FROM zpsb_stud_parent.
DELETE FROM zpsb_adm_stat.
out->write( 'Note all data has been erased.').

DO 12 TIMES.
lw_classes-class = lw_classes-class + 1.
APPEND lw_classes TO lt_classes.
ENDDO.
INSERT zpsb_classes FROM TABLE @lt_classes.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
out->write( 'Data saved for Classes.' ).
ENDIF.

lt_admission_stat = VALUE #( ( status = 'Rejected' ) ( status = 'Accepted' ) ).


INSERT zpsb_adm_stat FROM TABLE @lt_admission_stat.
IF sy-subrc = 0.
COMMIT WORK AND WAIT.
out->write( 'Data saved for admission status.' ).
ENDIF.
* CHECK 1 <> 1.
lt_studentmaster = VALUE #( ( aadhar = '2003110752000000'
admissionstatus = 'Rejected'
dob = '19930301'
dtofadmission =
cl_abap_context_info=>get_system_date( )
fname = 'Prasenjit'
mname = 'Singh'
lname = 'Bist'
class = '12'
contact = '9163512345'
plofbirth = 'Cooch Behar' )

( aadhar = '2003110752000001'
admissionstatus = 'Rejected'
dob = '19930318'
dtofadmission =
cl_abap_context_info=>get_system_date( )
fname = 'Sampat'
mname = 'Singh'
lname = 'Bist'
class = '12'
contact = '9163512445'
plofbirth = 'Dehradun' ) ).

INSERT zpsb_stud_master FROM TABLE @lt_studentmaster.


IF sy-subrc = 0.
COMMIT WORK AND WAIT.
out->write( 'Data saved for student master.' ).
ENDIF.
5

lt_studentparent = VALUE #( ( studentaadhar = '2003110752000000'


Page

parenttype = 'F'

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

contact = '9163554321'
fname = 'Shilpi'
lname = 'Bist'
occupation = 'Housewife'
qualification = 'Graduate' )

( studentaadhar = '2003110752000000'
parenttype = 'M'
contact = '9163554320'
fname = 'Sampat'
lname = 'Bist'
occupation = 'IFS'
qualification = 'Post Graduate' ) ).

INSERT zpsb_stud_parent FROM TABLE @lt_studentparent.


IF sy-subrc = 0.
COMMIT WORK AND WAIT.
out->write( 'Data saved for student master.' ).
ENDIF.

ENDMETHOD.
ENDCLASS.

Step 3: Code to create CDS DDL

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Student Master'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
@Search.searchable: true
define view entity ZPSB_I_STUD_MASTER
as select from zpsb_stud_master
{

@Search.defaultSearchElement: true
@Search.fuzzinessThreshold: 0.9
key aadhar as Aadhar,
fname as FirstName,
mname as MiddleName,
lname as LastName,
dob as DateofBirth,
plofbirth as PlaceofBirth,
6

class as Class,
Page

contact as Contact,

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

email as Email,
dtofadmission as DateofAdmission,
admissionstatus as AdmissionStatus
}

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Student Parent Relation'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
@Search.searchable: true
define view entity ZPSB_I_STUD_PARENT
as select from zpsb_stud_parent
{
@Search.defaultSearchElement: true
key studentaadhar as Studentaadhar,
key parenttype as Parenttype,
fname as FirstName,
mname as MiddleName,
lname as LastName,
qualification as Qualification,
occupation as Occupation,
contact as Contact,
email as Email,

@Semantics.amount: { currencyCode: 'Currency' }


salary as Salary,
currency as Currency
}

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Student Master Root View'
@Metadata.allowExtensions: true
@Metadata.ignorePropagatedAnnotations: true
define root view entity ZPSB_UI_STUDENT_MASTER
as select from ZPSB_I_STUD_MASTER
composition[0..*] of ZPSB_UI_STUD_PARENT as _tostudentparent
{
key Aadhar,
FirstName,
MiddleName,
LastName,
DateofBirth,
PlaceofBirth,
Class,
7

Contact,
Page

Email,

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

DateofAdmission,
AdmissionStatus,
_tostudentparent
}

@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Student Parent Root View'
@Metadata:{ allowExtensions: true, ignorePropagatedAnnotations: true}
define view entity ZPSB_UI_STUD_PARENT
as select from ZPSB_I_STUD_PARENT
association to parent ZPSB_UI_STUDENT_MASTER as _tomaster on
$projection.Studentaadhar = _tomaster.Aadhar
{
key Studentaadhar,
key Parenttype,
FirstName,
MiddleName,
LastName,
Qualification,
Occupation,
Contact,
Email,
Salary,
Currency,
_tomaster
}

@EndUserText.label: 'Student Master Root View Projection'


@AccessControl.authorizationCheck: #NOT_REQUIRED
define root view entity ZPSB_UIP_STUDENT_MASTER
as projection on ZPSB_UI_STUDENT_MASTER
{
key Aadhar,
FirstName,
MiddleName,
LastName,
DateofBirth,
PlaceofBirth,
Class,
Contact,
Email,
DateofAdmission,
AdmissionStatus,
_tostudentparent : redirected to composition child ZPSB_UIP_STUD_PARENT
}

@EndUserText.label: 'Student Parent Root View Projection'


8

@AccessControl.authorizationCheck: #NOT_REQUIRED
Page

define view entity ZPSB_UIP_STUD_PARENT

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

as projection on ZPSB_UI_STUD_PARENT
{
key Studentaadhar,
key Parenttype,
FirstName,
MiddleName,
LastName,
Qualification,
Occupation,
Contact,
Email,
Salary,
Currency,
_tomaster : redirected to parent ZPSB_UIP_STUDENT_MASTER
}

Step 4: Create CDS DDL for some search helps required in the demo.

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Search Help on admission status'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
@Search.searchable: true
define view entity ZPSB_I_SHLPAD_M_STAT as select from zpsb_adm_stat
{

@Search.fuzzinessThreshold: 0.8
@Search.defaultSearchElement: true
key status as Status
}

@AbapCatalog.viewEnhancementCategory: [#NONE]
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Search Help on Class'
@Metadata.ignorePropagatedAnnotations: true
@ObjectModel.usageType:{
serviceQuality: #X,
sizeCategory: #S,
dataClass: #MIXED
}
@Search:{searchable: true}
define view entity ZPSB_I_SHLP_CLASS
as select from zpsb_classes
{
@UI.hidden: false
9

@EndUserText:{ label: 'Class', quickInfo: 'Class the student has enrolled for' }
Page

@Search.defaultSearchElement: true

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

@Search.fuzzinessThreshold: 0.8
key class as Class

Step 5: Create the UI metadata extensions for the Root View and the View Entity
created in Step 3.

@Metadata.layer: #CORE
@UI.headerInfo.typeName: 'Student'
@UI.headerInfo.typeNamePlural: 'Students'
annotate view ZPSB_UI_STUDENT_MASTER with
{

@UI.facet: [{ id:'Student', purpose: #STANDARD, type: #IDENTIFICATION_REFERENCE, Commented [BP2]: When the object page has to show
label: 'Student Details',position: 10 }, additional fields of the current entity
{ id:'Parent', purpose: #STANDARD, type: #LINEITEM_REFERENCE, label:
'Parent Details', position: 20, targetElement: '_tostudentparent'}] Commented [BP3]: When the object page has to show
details from an association.
@UI:{ selectionField: [{ position: 10 }], Commented [BP4]: Free search
lineItem: [{ position: 10, label: 'Aadhar UID Number' },
{ type: #FOR_ACTION, dataAction: 'Action_AcceptStudent', label: Commented [BP5]: To show an action button on the Fiori
'Accept Admission' }], UI
identification: [{ position: 10, label: 'Aadhar UID Number' }] }
Aadhar; Commented [BP6]: To show the fields in the Object Page

@UI.lineItem: [{ position: 20, label: 'First Name', importance: #HIGH }]


@UI.selectionField: [{ position: 20 }]
@UI.identification: [{ position: 20 , label: 'First Name' }]
FirstName;

@UI.lineItem: [{ position: 30, label: 'Middle Name', importance: #HIGH }]


@UI.selectionField: [{ position: 30 }]
@UI.identification: [{ position: 30, label: 'Middle Name'}]
MiddleName;

@UI.lineItem: [{ position: 40, label: 'Last Name', importance: #HIGH }]


@UI.selectionField: [{ position: 40 }]
@UI.identification: [{ position: 40 , label: 'Last Name' }]
LastName;

@UI.lineItem: [{ position: 50, label: 'Date of Birth', importance: #HIGH }]


@UI.identification: [{ position: 50, label: 'Date of Birth' }]
DateofBirth;

@UI.lineItem: [{ position: 60, label: 'Place of Birth' }]


@UI.selectionField: [{ position: 50 }]
@UI.identification: [{ position: 60, label: 'Place of Birth' }]
PlaceofBirth;

@UI.lineItem: [{ position: 70, label: 'Class', importance: #HIGH }]


10

@UI.selectionField: [{ position: 60 }]
@UI.identification: [{ position: 70, label: 'Class' }]
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

@Consumption.valueHelpDefinition: [{ entity.name: 'ZPSB_I_SHLP_CLASS', Commented [BP7]: Attach a search help


entity.element: 'Class' }]
Class; Commented [BP8]: Name of the CDS Search Help
Commented [BP9]: Name of the attribute from the CDS
@UI.lineItem: [{ position: 80, label: 'Tel/Mobile Number' }] Search Help that will provide the value.
@UI.identification: [{ position: 80, label: 'Tel/Mobile Number' }]
Contact;

@UI.lineItem: [{ position: 90, label: 'Email Address' }]


@UI.identification: [{ position: 90, label: 'Email Address' }]
Email;

@UI.lineItem: [{ position: 100, label: 'Date of Admission' }]


@UI.selectionField: [{ position: 70 }]
@UI.identification: [{ position: 100, label: 'Date of Admission' }]
DateofAdmission;

@UI:{ selectionField: [{ position: 80 }],


lineItem: [{ position: 110, label: 'Admission Status', importance: #HIGH },
{ type: #FOR_ACTION, dataAction: 'Feature_SendLetter', label:
'Send Welcome Letter' }],
identification: [{ position: 110, label: 'Admission Status' }]}
@Consumption.valueHelpDefinition: [{ entity:{ name: 'ZPSB_I_SHLPAD_M_STAT',
element: 'Status' } }]
AdmissionStatus;

@Metadata.layer: #CORE
@UI.headerInfo.typeName: 'Parent'
@UI.headerInfo.typeNamePlural: 'Parents'
annotate view ZPSB_UI_STUD_PARENT with
{

@UI.facet: [{ id:'Parent',purpose: #STANDARD, type: #IDENTIFICATION_REFERENCE,


label: 'Parent Deatils Of The Student', position: 10 }]

@UI:{ selectionField: [{ position: 10 }],


lineItem: [{ position: 10, label: 'Aadhar UID Number' }],
identification: [{ position: 10, label: 'Aadhar UID Number' }] }
Studentaadhar;

@UI:{ selectionField: [{ position: 20 }],


lineItem: [{ position: 20, label: 'Parent Type (Father/Mother/Guardian)',
importance: #HIGH }],
identification: [{ position: 20, label: 'Parent Type
(Father/Mother/Guardian)' }] }
Parenttype;

@UI:{ selectionField: [{ position: 30 }],


11

lineItem: [{ position: 30, label: 'First Name', importance: #HIGH }],


identification: [{ position: 30, label: 'First Name' }] }
Page

FirstName;

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

@UI:{ selectionField: [{ position: 40 }],


lineItem: [{ position: 40, label: 'Middle Name', importance: #HIGH }],
identification: [{ position: 40, label: 'Middle Name' }] }
MiddleName;

@UI:{ selectionField: [{ position: 50 }],


lineItem: [{ position: 50, label: 'Last Name', importance: #HIGH }],
identification: [{ position: 50, label: 'Last Name' }] }
LastName;

@UI:{ selectionField: [{ position: 60 }],


lineItem: [{ position: 60, label: 'Qualification', importance: #LOW }],
identification: [{ position: 60, label: 'Qualification' }] }
Qualification;

@UI:{ selectionField: [{ position: 70 }],


lineItem: [{ position: 70, label: 'Occupation', importance: #LOW }],
identification: [{ position: 70, label: 'Occupation' }] }
Occupation;

@UI:{ selectionField: [{ position: 80 }],


lineItem: [{ position: 80, label: 'Tel/Mobile Number', importance: #HIGH }],
identification: [{ position: 80, label: 'Tel/Mobile Number' }] }
Contact;

@UI:{ selectionField: [{ position: 90 }],


lineItem: [{ position: 90, label: 'Email Address', importance: #HIGH }],
identification: [{ position: 90, label: 'Email Address' }] }
Email;

@UI:{ lineItem: [{ position: 100, label: 'Salary', importance: #LOW }],


identification: [{ position: 100, label: 'Salary' }] }
Salary;

@UI:{ lineItem: [{ position: 110, label: 'Currency', importance: #LOW }],


identification: [{ position: 110, label: 'Currency' }] }
Currency;
}

Step 6: Create the Behavior Definition and project it.

managed implementation in class zbp_psb_ui_student_master unique;


strict ( 2 );

define behavior for ZPSB_UI_STUDENT_MASTER alias StudentMaster


persistent table zpsb_stud_master
lock master
authorization master ( instance )
{
field (readonly:update) Aadhar;
12

field (readonly) DateofAdmission;


field (mandatory : create) Aadhar,
Page

Class,

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

DateofBirth,
Contact,
FirstName,
LastName,
AdmissionStatus;

create;
update;
delete;

validation Validate_Class on save { field Class; create; update;}


determination Determine_DOA on save { field Aadhar; create;}
action Action_AcceptStudent result [1] $self;
action (features : instance) Feature_SendLetter result [1] $self;

mapping for zpsb_stud_master


{
Aadhar = aadhar;
Class = class;
Contact = contact;
DateofAdmission = dtofadmission;
DateofBirth = dob;
Email = email;
FirstName = fname;
LastName = lname;
MiddleName = mname;
PlaceofBirth = plofbirth;
AdmissionStatus = admissionstatus;
}
}

projection;
strict ( 2 );

define behavior for ZPSB_UIP_STUDENT_MASTER alias StudentMaster


{
use create;
use update;
use delete;
use action Action_AcceptStudent;
use action Feature_SendLetter;
}

Step 7: Implement the ABAP code for the behaviors to be implemented.

CLASS lhc_ZPSB_UI_STUDENT_MASTER DEFINITION INHERITING FROM cl_abap_behavior_handler. Commented [BP10]: 1.The behavior definitions are
PRIVATE SECTION. implemented in a local class inside the behavior
13

implementation class.
METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION 2.RAP uses EML for data manipulation
Page

IMPORTING keys REQUEST requested_authorizations FOR StudentMaster RESULT 3.Do not write COMMIT as the framework will take care of
result. all commits.

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

METHODS validate_class FOR VALIDATE ON SAVE


IMPORTING keys FOR studentmaster~validate_class.
METHODS determine_doa FOR DETERMINE ON SAVE
IMPORTING keys FOR studentmaster~determine_doa.
METHODS action_acceptstudent FOR MODIFY
IMPORTING keys FOR ACTION studentmaster~action_acceptstudent RESULT result.
METHODS get_instance_features FOR INSTANCE FEATURES
IMPORTING keys REQUEST requested_features FOR studentmaster RESULT result.

METHODS feature_sendletter FOR MODIFY


IMPORTING keys FOR ACTION studentmaster~feature_sendletter RESULT result.

ENDCLASS.

CLASS lhc_ZPSB_UI_STUDENT_MASTER IMPLEMENTATION.

METHOD get_instance_authorizations.
ENDMETHOD.

METHOD Validate_Class.
READ ENTITIES OF zpsb_ui_student_master IN LOCAL MODE
ENTITY StudentMaster
FIELDS ( Class ) WITH CORRESPONDING #( Keys )
RESULT DATA(lt_studentmaster).

LOOP AT lt_studentmaster INTO DATA(lw_studentmaster).


SELECT SINGLE @abap_true FROM zpsb_classes WHERE class = @lw_studentmaster-
Class INTO @DATA(lv_record_found).
IF lv_record_found = abap_false.
APPEND VALUE #( %tky = lw_studentmaster-%tky ) TO failed-studentmaster.
APPEND VALUE #( %tky = keys[ 1 ]-%tky
%msg = new_message_with_text( severity =
if_abap_behv_message=>severity-error
text = 'Value of class
entered is not allowed' ) ) TO reported-studentmaster.
ENDIF.
ENDLOOP.
ENDMETHOD.

METHOD Determine_DOA.
DATA(lv_current_date) = cl_abap_context_info=>get_system_date( ) .

READ ENTITIES OF zpsb_ui_student_master IN LOCAL MODE


ENTITY StudentMaster
FIELDS ( DateofAdmission ) WITH CORRESPONDING #( keys )
RESULT DATA(lt_studentmaster).

LOOP AT lt_studentmaster INTO DATA(lw_studentmaster).


CHECK lw_studentmaster-Aadhar IS NOT INITIAL.
MODIFY ENTITIES OF zpsb_ui_student_master IN LOCAL MODE
ENTITY StudentMaster
UPDATE
14

FIELDS ( DateofAdmission ) WITH VALUE #( ( %tky = lw_studentmaster-%tky


DateofAdmission = lv_current_date ) ).
Page

ENDLOOP.

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

ENDMETHOD.

METHOD Action_AcceptStudent.
MODIFY ENTITIES OF zpsb_ui_student_master IN LOCAL MODE
ENTITY StudentMaster
UPDATE
FIELDS ( AdmissionStatus ) WITH VALUE #( FOR key IN keys ( %tky = key-%tky
AdmissionStatus = 'Accepted' ) ).

READ ENTITIES OF zpsb_ui_student_master IN LOCAL MODE


ENTITY StudentMaster
ALL FIELDS WITH CORRESPONDING #( keys )
RESULT DATA(lt_studentmaster).

result = VALUE #( FOR lw_studentmaster IN lt_studentmaster ( %tky =


lw_studentmaster-%tky %param = lw_studentmaster ) ).
ENDMETHOD.

METHOD get_instance_features.
READ ENTITIES OF zpsb_ui_student_master IN LOCAL MODE
ENTITY StudentMaster
FIELDS ( AdmissionStatus ) WITH CORRESPONDING #( keys )
RESULT data(lt_studentmaster)
FAILED data(lt_failed).

result = VALUE #( for lw_studentmaster in lt_studentmaster LET statusval = COND


#( WHEN lw_studentmaster-AdmissionStatus = 'Accepted'

THEN if_abap_behv=>fc-o-enabled

ELSE if_abap_behv=>fc-o-disabled )

IN ( %tky = lw_studentmaster-%tky

%action-Feature_SendLetter = statusval ) ).
ENDMETHOD.

METHOD Feature_SendLetter.

ENDMETHOD.

ENDCLASS.

Step 8: Create a service definition using the behavior projection and expose the
entities.

@EndUserText.label: 'Service Definition for Student Master ODATA'


define service ZPSB_UIP_STUDENT_MASTER_SD {
expose ZPSB_UIP_STUDENT_MASTER as StudentMasterUI;
expose ZPSB_I_SHLP_QUALIF as QualificationList;
expose ZPSB_I_SHLP_CLASS as ClassesForEnrollment;
15

expose ZPSB_I_SHLPAD_M_STAT as AdmissionStatus;


expose ZPSB_UIP_STUD_PARENT as StudentParentUI;
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Step 9: Create the Service Binding for ODATA V2- UI.

Screenshot-01

Step 10: Test


16

Screenshot-02
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Screenshot-03

In the screenshot above as the Admission Status is Accepted you are allowed to send the welcome
letter.

Screenshot-04

In the screenshot above as the Admission Status is Rejected and you are not allowed to send the
welcome letter, the UI greys out the button.

Select the record with Admission Status as Rejected and click on Accept Admission and the status will
change to Accepted and the Send Welcome Letter will be active.
17
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Screenshot-05

See the Over-View Page with the 2 UI Facets implemented-

18

Screenshot-06
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Try to Edit the Student Details and enter Class as 13, the validation will kick in and show the error-

Screenshot-07

We also had a determination so, let’s create a new entry via the UI now.

Screenshot-08

Press Create.
19
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Screenshot-09

The Date of Admission is auto-populated as per the code for the determination in the behavior pool
class.

Additional Features:

1. To enable the CUD for the composition child, we must enhance the behavior definition and the
behavior projection as follows. The code highlighted in yellow is the new code added to the
existing code we wrote for the basic application.

managed ;
strict ( 2 );

define behavior for ZPSB_UI_STUDENT_MASTER alias StudentMaster


implementation in class zbp_psb_ui_student_master unique
persistent table zpsb_stud_master
lock master Commented [BP11]: As the parent entity StudentMaster
authorization master ( instance ) is set as the lock master all composition child entities must
{ have an association pointing back to it and will be dependent
field (readonly:update) Aadhar; on the master for their locks.
field (readonly) DateofAdmission;
20

field (mandatory : create) Aadhar,


Class,
Page

DateofBirth,

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Contact,
FirstName,
LastName,
AdmissionStatus;

create;
update;
delete;

validation Validate_Class on save { field Class; create; update;}


determination Determine_DOA on save { field Aadhar; create;}
action Action_AcceptStudent result [1] $self;
action (features : instance) Feature_SendLetter result [1] $self;

association _tostudentparent {create; }

mapping for zpsb_stud_master


{
Aadhar = aadhar;
Class = class;
Contact = contact;
DateofAdmission = dtofadmission;
DateofBirth = dob;
Email = email;
FirstName = fname;
LastName = lname;
MiddleName = mname;
PlaceofBirth = plofbirth;
AdmissionStatus = admissionstatus;
}
}

define behavior for ZPSB_UI_STUD_PARENT alias StudentParent


implementation in class zbp_psb_ui_student_parent unique
persistent table zpsb_stud_parent
lock dependent by _tomaster
authorization master (instance)
{
field (readonly) Studentaadhar;
field (readonly : update) Parenttype;
field (mandatory : create) FirstName,
LastName,
Parenttype,
Contact;

association _tomaster;
update;
delete;

mapping for zpsb_stud_parent


{
21

Studentaadhar = studentaadhar;
Parenttype = parenttype;
Page

FirstName = fname;

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

MiddleName = mname;
LastName = lname;
Contact = contact;
Email = email;
Occupation = occupation;
Qualification = qualification;
Salary = salary;
Currency = currency;
}
}

projection;
strict ( 2 );

define behavior for ZPSB_UIP_STUDENT_MASTER alias StudentMaster


{
use create;
use update;
use delete;
use association _tostudentparent {create;}
use action Action_AcceptStudent;
use action Feature_SendLetter;
}

define behavior for ZPSB_UIP_STUD_PARENT alias StudentParent


{
use update;
use delete; Commented [BP12]: Note that the CREATE functionality
use association _tomaster; cannot be defined in the child entity but only in the behavior
} definition of the parent.

Test the application.

We can see that the CUD options are now available for the UI Facet Parent Details
implemented by the composition child ZPSB_UI_STUD_PARENT (See the screenshot below).
So, the new changes are working as expected.
22
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Screenshot-10

2. Now let’s have some more control on the create feature on the composition
child, if the age of the student is equal to or over 18 years then we do not
need the parent details and the create feature should be disabled. This
basically means we are controlling the features of the child entities now.

3. Add a virtual element in the Student Header that shows the current academic
performance of the student as Good, Mediocre or Poor.

The points to note are that the Virtual Element is not part of the persistent
layer and only defined in the selection list of the projection root view
entity. The value for the Virtual Element is calculated by an ABAP Class that
implements the interface - if_sadl_exit_calc_element_read.

So, in our demo let’s call that virtual element as AcademicPerformance, and
let’s add it to the projection view entity of StudentMaster.
23
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

The annotation @ObjectModel.virtualElementCalculateBy links the reference ABAP


class with the field. This class is responsible to provide the value to it in
the run time.

So, let’s implement the class – ZCL_PSB_ACADEMICS. This class will implement
the interface- IF_SADL_EXIT_CALC_ELEMENT_READ and this has 2 important
methods.

▪ get_calculation_info: This method is called before the actual data retrieval.


It ensures that all the relevant elements that are needed for the calculation
of the virtual element are selected.

▪ calculate: This method is called after the data retrieval. It uses the values
of the relevant elements to calculate the value for the virtual element.

In our example, we will read the academic details available for the latest
enrolled class of the student and calculate the % to label it as Good,
Mediocre or Poor. If the % is >= 70 it’s Good, % < 70 and % >= 50 then
Mediocre and if the % < 50 then it would be called Poor.
24
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

CLASS zcl_psb_academics DEFINITION


PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_sadl_exit_calc_element_read .
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS .

CLASS zcl_psb_academics IMPLEMENTATION.


METHOD if_sadl_exit_calc_element_read~get_calculation_info.
LOOP AT it_requested_calc_elements ASSIGNING FIELD-
SYMBOL(<fs_calc_element>).
CASE <fs_calc_element>.
WHEN 'AcademicPerformance'.
APPEND 'Aadhar' TO et_requested_orig_elements.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
ENDMETHOD.

METHOD if_sadl_exit_calc_element_read~calculate.
DATA: lt_original_data TYPE STANDARD TABLE OF zpsb_uip_student_master
WITH DEFAULT KEY,
lv_total TYPE p LENGTH 5 DECIMALS 2,
lv_percentage TYPE p LENGTH 5 DECIMALS 2,
lv_performance TYPE c LENGTH 10.

lt_original_data = CORRESPONDING #( it_original_data ).


TRY.
DATA(lv_aadhar) = lt_original_data[ 1 ]-aadhar.
SELECT * FROM zpsb_academics WHERE aadhar = @lv_aadhar INTO TABLE
@DATA(lt_academics).
CHECK sy-subrc = 0.
SORT lt_academics BY class DESCENDING.
DATA(ls_academics) = lt_academics[ 1 ].
lv_total = ls_academics-marks1
+
ls_academics-marks2
+
ls_academics-marks3
+
ls_academics-marks4
+
ls_academics-marks5.

lv_percentage = ( lv_total / 500 ) * 100.


25

IF lv_percentage >= 70.


lv_performance = 'Good'.
Page

ELSEIF lv_percentage < 50.

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

lv_performance = 'Poor'.
ELSEIF lv_percentage < 70 AND lv_percentage >= 50.
lv_performance = 'Mediocre'.
ENDIF.

LOOP AT lt_original_data ASSIGNING FIELD-


SYMBOL(<lfs_original_data>).
<lfs_original_data>-AcademicPerformance = lv_performance.
ENDLOOP.

ct_calculated_data = CORRESPONDING #( lt_original_data ).

CATCH cx_sy_itab_line_not_found.
ENDTRY.
ENDMETHOD.
ENDCLASS.

Now, we must create a metadata extension for the Projected Root View Entity.
This is because of the Virtual Element that exists in the Projected Root View
Entity and not the Root View Entity

First add the annotation @Metadata: { allowExtensions: true } to allow the


creation of the metadata extension for ZPSB_UIP_STUDENT_MASTER.

26
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Next create the metadata extension for ZPSB_UIP_STUDENT_MASTER by copying what


we wrote for ZPSB_UI_STUDENT_MASTER but enhancing it with the details for the
Virtual Element AcademicPerformance.

Let’s check the output now-


27
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Now how about adding some criticality to add more visual impact.

So, let’s add the criticality based on the admission status. If Accepted then
Green else if Rejected then Red and for exceptions it is Grey (Neutral).

So, create a new field in the Root View Entity called →


AdmissionStatusCriticality.

28
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Add it in the projection of the Root View Entity →


29
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Now enhance the metadata extension created for the Projection Root View
Entity- ZPSB_UIP_STUDENT_MASTER. The UI annotation for AdmissionStatus- Line
Item now adds a new component called criticality that refers to the field –
AdmissionStatusCriticality.
30
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Activate all the artifacts, refresh the service binding, and execute the
preview to check the output.

Select the student and press the action Accept Admission and then we have-

31
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4. Upload attachment (picture of the student) in the parent entity for the
student master record.

In most real-world cases, people would like to add attachments. In the past the RAP
model did not support it but now it does. And the steps to add an attachment in our
Student Header- Parent entity would be a few simple steps described below.
32
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4.1 Add the 3 new fields highlighted in the screenshot in the ZPSB_STUD_MASTER
table. The 3 fields are Attachment, File name and the Mime Type.

33
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4.2 Add the 3 new fields added in the database table to the interface view
ZPSB_I_STUD_MASTER. Note the annotations used @semantics.largeObject and
@semantics.mimeType. Commented [BP13]: Large Objects (LOB) allows the
handling of attachment in the ABAP RAP Framework

34
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4.3 Add the fields in the View Entity and the Projection of the View Entity.

35
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

36
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4.4 Update the metadata extension of the Parent Entity with the new fields.

37
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

4.5 Update the field-mapping of the root entity in the behavior definition.

38
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

Reference(s):

1. https://help.sap.com/docs/ABAP_PLATFORM_NEW/fc4c71aa50014fd1b43721701471913d/2
89477a81eec4d4e84c0302fb6835035.html

2. https://open.sap.com/courses/cp13

3. https://developers.sap.com/tutorials/abap-environment-behavior.html

4. https://help.sap.com/docs/abap-cloud/abap-rap/working-with-large-
objects?version=sap_btp

5. https://help.sap.com/docs/ABAP_PLATFORM_NEW/fc4c71aa50014fd1b43721701471913d/d
f0ef4aac2d34fdd948b8a8883df3a1f.html?q=virtual%20

39
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com


ABAP RESTful Application Programming Managed Scenario- Step by Step Guide

About the Author:

Prasenjit Singh Bist is an SAP Certified ABAP Developer, with almost 16 years of
experience in SAP custom development with strong expertise in latest technologies.
He has worked in multiple SAP ECC and S/4HANA implementation projects. He has strong
expertise in ABAP Objects, Design patterns, ABAP CDS, ABAP RESTful Application
Programming, ABAP on HANA, BOPF, IDOC and ODATA services.

40
Page

©Prasenjit Singh Bist,2023 Email- prasenjit.singhbist@outlook.com

You might also like