How to use Side Effects in RAP (Restful Application Programming) Model
2023-12-7 08:11:44 Author: blogs.sap.com(查看原文) 阅读量:8 收藏

Introduction

When a user modifies the content of a field, and it affects the other fields in the UI. This is known as a side effect.

Side Effects annotations indicate which targets are to be refreshed in order to display updated values. These field values change due to the change of a field, action, or decision action. There are various ways to annotate side effects by combining the triggers and the affected components.

Advantage of Side Effects

When a user modifies the data of draft instance on UI, it is not necessary that all the fields will trigger a READ request. This could result in inconsistent data display on the draft instance. This situation can be overcome by the use of side effects.

Types of Side Effects

The following RAP BO attributes have side effects that can be defined in the base behavior definition:

  • Field: The defined targets are reloaded and the side effect is initiated whenever a defined field is modified on the user interface.

          Example:

          – side effects { field MyField affects field Targets, messages; }

          – side effects { field MyField affects entity Targets, messages; }

          – side effects { field MyField affects $self, messages; }

  • Action: The defined targets are reloaded, and the side effect is triggered whenever a defined field is modified on the user interface.

          Example:

          – side effects { action MyAction affects field Targets, messages; }

          – side effects { action MyAction affects entity Targets, messages; }

          – side effects { action MyAction affects  $self, messages;}

  • Determine Action: The define targets are refreshed and the determine action is triggered whenever the defined source is modified.

          Example:

          – side effects { determine action MyDetermineAction executed on sources affects Targets, messages; }

Example of adding number of days to current date

When the user changes the value of InputDays, the determination CalculateDate is called and modifies the values of OutputDate by adding InputDays in CurrentDate. In order to show the updated values without a refresh, side effects is required.

Step 1 – Add a statement to your behavior definition in which you indicate which RAP BO fields are                 affected by other fields.

Code Snippet for Behavior Definition –

side effects
  {   
    field InputDays affects field OutputDate;
  }

  determination CalculateDate on modify {  field InputDays; }

Step 2 – Add ‘use side effects’ in the behavior projection to enable the side effects.

Code Snippet for Behavior Projection –

projection;
strict ( 2 );
use draft;
use side effects;

define behavior for ZC_Calculate_Date alias CalculateDate
use etag

{
  use create;
  use update;
  use delete;

  use action Edit;
  use action Activate;
  use action Discard;
  use action Prepare;
  use action Resume;
}

Output

During User Input –

Output

Output

Summary

I would encourage you to read through other blog posts on such topics at: https://blogs.sap.com/tags/7e44126e-7b27-471d-a379-df205a12b1ff/

You can post and answer questions on related topics at: https://answers.sap.com/tags/7e44126e-7b27-471d-a379-df205a12b1ff

Please provide your feedback and ask questions in the comment section.

Regards,

Anubhav Jain


文章来源: https://blogs.sap.com/2023/12/07/how-to-use-side-effects-in-rap-restful-application-programming-model/
如有侵权请联系:admin#unsafe.sh