🎓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.
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.
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.
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.
Step 1Define the Validations validateCustomer and validateDates
—
Define the validations validateCustomer and validateDates.
Open your behavior definitionbehaviordefinitionZRAP100_R_TRAVELTP_###.
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:

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; }
```
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:

**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; }`
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 entityclass iconZRAP100_BP_TRAVELTP_###.
>**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 2Implement the Validation validateCustomer
+
Implement the validation validateCustomer which checks if the customer ID (CustomerID) that is entered by the consumer is valid. An appropriate message should be raised and displayed on the UI for each invalid value.
First, check the interface of the new methods in the declaration part of the local handler class lcl_travel of the behavior pool of the Travel BO entityclass iconZRAP100_BP_TRAVEL_###.
For that, set the cursor on the method name, validateCustomer, press F2 to open the ABAP Element Info view, and examine the full method interface.
Travel BO Behavior Pool
Short explanation:
The addition FOR VALIDATE ON SAVE indicates that the method provides the implementation of a validation executed on save. Validations are always executed on save.
Method signature for the validation method:
IMPORTING parameter keys - an internal table containing the keys of the instances on which the validation should be performed.
failed - table with information for identifying the data set where an error occurred
reported - table with data for instance-specific messages
You can go ahead and implement the validation method.
Now implement the method validateCustomer in the implementation part of the class.
The logic consists of the following main steps:
Read the travel instance(s) of the transferred keys (keys) using the EML statement READ ENTITIES.
The addition FIELDS is used to specify the fields to be read. Only CustomerID is relevant for the present validation.
The addition ALL FIELDS can be used to read all fields.
The addition IN LOCAL MODE is used to exclude feature controls and authorization checks.
Read all the transferred (distinct, non-initial) customer IDs and check if they exist.
Prepare/raise messages for all transferred travel instances with initial and non-existing customer ID (CustomerID) and set the changing parameter reported
In the context of Draft, state-messages are used for reporting problems. A REPORTED-entry becomes a state-message by filling the %STATE_AREA. State-messages are stored together with the state of the draft. As a consequence they need to be cleared before the corresponding check is executed again.
Replace the current method implementation of validateCustomer with following code snippet and replace all occurrences of the placeholder ### with your group ID.
You can use the F1 Help to get detailed information on the different ABAP and EML statements.
ABAP
*********************************************************************** Validation: Check the validity of the entered customer data**********************************************************************METHODvalidateCustomer."read relevant travel instance data
READENTITIESOFZRAP100_R_TravelTP_###INLOCALMODEENTITYTravelFIELDS(CustomerID)WITHCORRESPONDING#(keys)RESULT DATA(travels).DATAcustomersTYPE SORTED TABLE OF/dmo/customerWITH UNIQUE KEYcustomer_id."optimization of DB select: extract distinct non-initial customer IDs
customers=CORRESPONDING#(travelsDISCARDINGDUPLICATESMAPPINGcustomer_id=customerIDEXCEPT*).DELETEcustomersWHEREcustomer_idIS INITIAL.IFcustomersIS NOT INITIAL."check if customer ID exists
SELECTFROM/dmo/customerFIELDScustomer_idFORALLENTRIESIN@customersWHEREcustomer_id=@customers-customer_idINTOTABLE@DATA(valid_customers).ENDIF."raise msg for non existing and initial customer id
LOOP AT travelsINTODATA(travel).APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_CUSTOMER')TOreported-travel.IFtravel-CustomerIDIS INITIAL.APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_CUSTOMER'%msg=NEW/dmo/cm_flight_messages(textid=/dmo/cm_flight_messages=>enter_customer_idseverity=if_abap_behv_message=>severity-error)%element-CustomerID=if_abap_behv=>mk-on)TOreported-travel.ELSEIFtravel-CustomerIDIS NOT INITIALANDNOTline_exists(valid_customers[customer_id=travel-CustomerID]).APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_CUSTOMER'%msg=NEW/dmo/cm_flight_messages(customer_id=travel-customeridtextid=/dmo/cm_flight_messages=>customer_unkownseverity=if_abap_behv_message=>severity-error)%element-CustomerID=if_abap_behv=>mk-on)TOreported-travel.ENDIF.ENDLOOP.ENDMETHOD.
Implement the validation validateDates which checks the validity of entered begin dates (BeginDate) and end dates (EndDate). An appropriate messages should be raised and displayed on the UI for each invalid value.
In your implementation classclassZRAP100_BP_TRAVELTP_### of validateDates with following code snippet and replace all occurrences of the placeholder ### with your group ID. The main implementation steps are similar to the one of method validateCustomer. This validation is performed on the fields BeginDate and EndDate. It checks if the entered begin date (BeginDate) is in the future and if the value of the entered end date (EndDate) is after the begin date (BeginDate).
Replace the current method implementation.
ABAP
*********************************************************************** Validation: Check the validity of begin and end dates**********************************************************************METHODvalidateDates.READENTITIESOFZRAP100_R_TravelTP_###INLOCALMODEENTITYTravelFIELDS(BeginDateEndDateTravelID)WITHCORRESPONDING#(keys)RESULT DATA(travels).LOOP AT travelsINTODATA(travel).APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_DATES')TOreported-travel.IFtravel-BeginDateIS INITIAL.APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_DATES'%msg=NEW/dmo/cm_flight_messages(textid=/dmo/cm_flight_messages=>enter_begin_dateseverity=if_abap_behv_message=>severity-error)%element-BeginDate=if_abap_behv=>mk-on)TOreported-travel.ENDIF.IFtravel-BeginDate<cl_abap_context_info=>get_system_date()ANDtravel-BeginDateIS NOT INITIAL.APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_DATES'%msg=NEW/dmo/cm_flight_messages(begin_date=travel-BeginDatetextid=/dmo/cm_flight_messages=>begin_date_on_or_bef_sysdateseverity=if_abap_behv_message=>severity-error)%element-BeginDate=if_abap_behv=>mk-on)TOreported-travel.ENDIF.IFtravel-EndDateIS INITIAL.APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_DATES'%msg=NEW/dmo/cm_flight_messages(textid=/dmo/cm_flight_messages=>enter_end_dateseverity=if_abap_behv_message=>severity-error)%element-EndDate=if_abap_behv=>mk-on)TOreported-travel.ENDIF.IFtravel-EndDate<travel-BeginDateANDtravel-BeginDateIS NOT INITIALANDtravel-EndDateIS NOT INITIAL.APPENDVALUE#(%tky=travel-%tky)TOfailed-travel.APPENDVALUE#(%tky=travel-%tky%state_area='VALIDATE_DATES'%msg=NEW/dmo/cm_flight_messages(textid=/dmo/cm_flight_messages=>begin_date_bef_end_datebegin_date=travel-BeginDateend_date=travel-EndDateseverity=if_abap_behv_message=>severity-error)%element-BeginDate=if_abap_behv=>mk-on%element-EndDate=if_abap_behv=>mk-on)TOreported-travel.ENDIF.ENDLOOP.ENDMETHOD.
You can either refresh your application in the browser using F5 if the browser is still open - or go to your service binding ZRAP100_UI_TRAVEL_O4_### and start the Fiori elements App preview for the Travel entity set.
Click Create to create a new entry.
Select an Sunshine Travel (70001) as Agency ID, Theresia Buchholm as Customer ID, Mar 16, 2022 as starting date and Mar 14, 2022 as end date and click Create.
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 5
1. Define the Validations validateCustomer and validateDates2. Implement the Validation validateCustomer3. Implement the Validation validateDates4. Preview and test the enhanced travel app5. Test yourself
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.