Wednesday 21 December 2022

How to use Side Effects in ABAP RAP (Restful Application Programming) Model

In this beginner blog post we are going to see how we can use Side Effects in ABAP Restful Application Programming Model.

What is Side Effects:


When user makes a change to a field on UI, and this change effect the content of other field, this behavior is called Side Effect.

Annotation to Implement Side Effects:


Some more annotation properties are available but for this demo we are only using below 2 Annotations properties.

Source properties: Properties on which you want to trigger Side Effect behaviour. Source properties are responsible to trigger Determination on action (on modify)

Target properties: Properties you want to manipulate as a result of Side Effects. Target properties contains fields which are updated or modified as result of Determination.

Implement Determination logic:


Implementing Determination Logic to update Salary based on Role. The problem statement is – when user change Role from Teacher <-> Professor then Salary should be updated without refresh of full page.

Above requirement can be achieved by using Side Effects to update dependent field.

1- In behavior Definition file added Determination with method name changeSalary and action “on modify” with source property as Role.

Determination changeSalary will be triggered as soon as Role property is changed.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Create Service API V4


2- To create Service API V4 to use in Fiori Application. Create new Service Binding.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Once created Activate and Publish Local Service Endpoints.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

3- Now we are going to create Fiori Application where we will be using this V4 API.

Create New Fiori Application using Template


SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Select Service created in previous steps.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Click Next. Select main Entity

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Create Module and fill all required Information and click on Finish.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Once Module is ready. Open Annotations Folder and then Annotations.xml file

Update below SideEffects annotation to file.

<Annotations Target=".....">
<Annotation Term="Common.SideEffects">
     <Record>
        <PropertyValue Property="SourceProperties">
           <Collection>
              <PropertyPath>Role</PropertyPath>
           </Collection>
        </PropertyValue>
        <PropertyValue Property="TargetProperties">
           <Collection>
              <PropertyPath>Salary</PropertyPath>
           </Collection>
        </PropertyValue>
    </Record>                    
</Annotation>
</Annotations>

To add namespace click on QuickFix option.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Click on Availablable option to add Namespace for SideEffects Annotation.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Everything is done. Now run the Application.

Application is loaded.

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

For Role teacher Salary is 10,000

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

Change Role to Professor and Hit ENTER, Salary will be updated

SAP ABAP Career, SAP ABAP Skills, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides

No comments:

Post a Comment