SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Enhance the Business Object Behavior With Validations

Enhance the business object behavior using validation with SAP BTP ABAP environment.

Overview

🎓 beginner 15 min. SAP BTP ABAP EnvironmentBeginnerABAP DevelopmentSAP Business Technology Platform

You will learn

  • How to define validations
  • How to implement validations
  • How to preview and test enhanced travel app In the previous exercise, you’ve defined and implemented a determination for setting the initial value of the field OverallStatus to Open (O) during the creation of new instances of business object entity Travel. In the present exercise, you’re going to define and implement two back-end validations, validateCustomer and validateDates, to respectively check if the customer ID that is entered by the consumer is valid and if the begin date is in the future and if the value of the end date is after the begin date. These validations are only performed in the back-end (not on the UI) and are triggered independently of the caller, i.e. Fiori UIs or EML APIs. Hint: Frontend validation & Backend validations Validations are used to ensure the data consistency. As the name suggests, frontend validations are performed on the UI. They are used to improve the user experience by providing faster feedback and avoiding unnecessary roundtrip. In the RAP context, front-end validations are defined using CDS annotation or UI logic. On the other hand, backend validations are performed on the back-end. They are defined in the business object behavior definition and implemented in the respective behavior pools. Frontend validations can be easily bypassed - e.g. by using EML APIs in the RAP context. Therefore, backend validations are a MUST to ensure the data consistency.
Merve Temel M Merve Temel April 17, 2024
Created by March 9, 2023
Contributors

Prerequisites

Prerequisites

  • You need to have access to an SAP BTP, ABAP environment, or SAP S/4HANA Cloud, ABAP environment or SAP S/4HANA (release 2022 or higher) system. For example, you can create free trial user on SAP BTP, ABAP environment.
  • You have downloaded and installed the [latest ABAP Development Tools (ADT)] (https://tools.hana.ondemand.com/#abap) on the latest Eclipse© platform.
  • You have created an ABAP Cloud Project.
  • Make sure, your system has the ABAP flight reference scenario. If your system hasn’t this scenario. You can download it here. The trial systems have the flight scenario included.

Steps

Intro

Reminder: Do not forget to replace the suffix placeholder ### with your chosen or assigned group ID in the exercise steps below.

About: Validations

A validation is an optional part of the business object behavior that checks the consistency of business object instances based on trigger conditions.

A validation is implicitly invoked by the business object’s framework if the trigger condition of the validation is fulfilled. Trigger conditions can be MODIFY operations and modified fields. The trigger condition is evaluated at the trigger time, a predefined point during the BO runtime. An invoked validation can reject inconsistent instance data from being saved by passing the keys of failed instances to the corresponding table in the FAILED structure. Additionally, a validation can return messages to the consumer by passing them to the corresponding table in the REPORTED structure.

Further reading: Validations

Step 1 Define the Validations validateCustomer and validateDates

Define the validations validateCustomer and validateDates.

  1. Open your behavior definition

    behaviordefinition
    behaviordefinition
    ZRAP100_R_TRAVELTP_###.

  2. Because empty values will not be accepted for the fields CustomerID, BeginDate, and EndDate, specify them as mandatory field by adding the following code snippet after the determination as shown on the screenshot below.

```ABAP
field ( mandatory )
CustomerID,
BeginDate,
EndDate; 
```    

Your source code should look like this:

![validation](https://raw.githubusercontent.com/sap-tutorials/abap-core-development/main/tutorials/abap-environment-rap100-validation/p8.png)   
  1. Define the validations validateCustomer and validateDates. For that, add the following code snippet after the determination.
```ABAP
validation validateCustomer on save { create; field CustomerID; }
validation validateDates on save { create; field BeginDate, EndDate; }
```         
  1. In order to have draft instances being checked and determinations being executed before they become active, they have to be specified for the draft determine action prepare in the behavior definition.
Replace the code line **`draft determine action Prepare;`** with the following code snippet as shown on the screenshot below

```ABAP
draft determine action Prepare
{ 
validation validateCustomer;
validation validateDates;    }
```    

Your source code should look like this:

![validation](https://raw.githubusercontent.com/sap-tutorials/abap-core-development/main/tutorials/abap-environment-rap100-validation/p9.png)           

 **Short explanation**:    

  - Validations are always invoked during the save and specified with the keyword `validateCustomer on save`.   

  - `validateCustomer` is a validation with trigger operation `create` and trigger field `CustomerID`.    

  - `validateDates` is a validation with trigger operation `create` and trigger fields `BeginDate` and `EndDate`.            

>**Hint:** In case a validation should be invoked at every change of the BO entity instance, then the trigger conditions createand update must be specified: e.g. validation `validateCustomer on save { create; update; }`
  1. Save

    save icon
    save icon
    and activate
    activate icon
    activate icon
    the changes.

  2. Add the appropriate FOR VALIDATE ON SAVE methods to the local handler class of the behavior pool of the Travel BO entity via quick fix.

    For that, set the cursor on one of the validation names and press Ctrl+1 to open the Quick Assist view and select the entry Add all 2 missing methods of entity zrap100_i_travel_###.

    As a result, the FOR VALIDATE ON SAVE methods validateCustomer and validateDates will be added to the local handler class lcl_travel of the behavior pool of the Travel BO entity

    class icon
    class icon
    ZRAP100_BP_TRAVELTP_###.

    Travel BO Behavior Pool
    Travel BO Behavior Pool

  3. Save

    save icon
    save icon
    and activate
    activate icon
    activate icon
    the changes.

>**Hint:** If you get an error message in the behavior implementation The entity `ZRAP100_R_TRAVELTP_###` does not have a validation `VALIDATECUSTOMER`. try to activate the behavior definition once again.
Step 2 Implement the Validation validateCustomer
+
Step 3 Implement the Validation validateDates
+
Step 4 Preview and test the enhanced travel app
+
Step 5 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 5
1. Define the Validations validateCustomer and validateDates 2. Implement the Validation validateCustomer 3. Implement the Validation validateDates 4. Preview and test the enhanced travel app 5. Test yourself

Learn more →