Validations , Determination and Additional binding in ABAP Restful Application Programming
Satya Prakash Tiwari
Certified SAP ABAP on HANA /ABAP/Fiori consultant at Deloitte | Ex-IBM | Ex-Accenture | Ex-Wiproite
Introduction
Within ABAP, the concept of restful application programming has gained significant traction due to its ability to create lightweight, scalable, and loosely coupled APIs. It allows developers to expose ABAP-based functionalities as RESTful services, facilitating seamless integration with other systems and enabling the development of modern, user-centric applications.
In the realm of ABAP restful application programming, validations and determination plays a vital role in shaping the architecture and behaviour APIs.
Validation, as a core aspect of ABAP restful application programming, ensures that incoming requests comply with predefined rules and constraints.
By leveraging the power of ABAP's built-in validation framework, developers can enforce data integrity, improve security, and provide meaningful feedback to consumers.
Validation is a process of checking and ensuring that something is correct, accurate, or meets specific requirements. In everyday life, validation is like double-checking to make sure things are done the right way.
For example, imagine we are filling out a form online to sign up for a new account.
When we enter email address, the system performs validation by checking if it follows the correct format (e.g., contains the "@" symbol and a domain name like "gmail.com").
If we accidentally enter an email address without the "@" symbol, the system will show an error message, letting us know that email address is not valid.
This helps ensure that only properly formatted email addresses are accepted, reducing the chance of mistakes or incorrect data being entered.
Determination, on the other hand, enables dynamic decision-making within the API based on various runtime factors.
Determination is all about making decisions based on different factors or conditions.
We can make use of determination in ABAP RAP in a scenario where we need to fill one field data based on some condition or input driven from other factors.
Lets say your company has decided to give bonus to employees based on their position .
If Employee position is above Manager then Bonus amount would be 5 %.
If Employee position is Team lead then Bonus amount would be 10 %.
Below team lead bonus amount would be 8 %.
In this scenario , we can put determination on employee position and accordingly bonus would be assigned to respective employees.
Let us see these concepts as example in ABAP Restful application programming.
Validation
Requirement : No user should able to select begin date of their travel in past (less than today/system date).If user is selecting date in past, error must be throw on screen saying invalid date.
To achieve this expectation, We need to add validation on behaviour defination with below syntax.
Validation VALIDATIONNAME on [save] {field field1;<operation>}
where,
validation - is a keyword ,
VALIDATIONNAME - name of method which will hold validation logic
[save] - triggers on save
field - field name which needs to be validated
<operation> - on SAVE/MODIFY/DELETE
Once Validation defined on behaviour defination , we need to create method in implementation class to write business logic.
Click CNTRL+1 on validation method name and framework will add validation method in implementation class.
Let us write custom code to validate begin date field of travel that should not be in past.
We have used EML to read UI data ,Please refer my EML article to get familiar with its syntax.
Activate all including service binding and refresh UI to check validation on field begin date.
Note : Date of travel creation(system date ) is 22 May 2023
Since entered date is in past system has validated Starting date of travel and thrown error message.
That's how we can validate any field on screen and enforce data integrity.
领英推荐
Determination
Requirement : Calculate duration of travel in days based on date provided by user.
We need to define Determination on behaviour defination level with below syntax.
Determination <method name> <SAVE> {field : <Trigger Field>}
Where,
Determination - is a keyword use to define determination on behaviour defination
<method name> - that will contain business logic
<SAVE> - trigger time
<trigger field> - source field on which determination is based
Next step is to add determination method on implementation class , press CNTRL+1 on validation method name(duration) and framework will add determination method in class.
Now we have determination method ready to write business logic .
We are reading entered data on screen by user with the help of EML and calculating duration of travel(lv_duration) from starting date and end date of travel.
Total duration (lv_duration ) would be updated in field duration with the help of EML.
Activate all including service binding and create new travel record or modify existing one.
Here we will modify existing record which is having total duration 43 , Lets change End Date of travel to 10th July 2023.
After changes , Duration has been auto changed to 27 days based on determination logic.
That's how we can make use of determination in ABAP restful application programming.
Additional Binding
When we define a value help using the @Consumption.valueHelpDefinition annotation, we can use the additionalBinding property to specify additional fields that should be displayed in the value help dialog.
These additional fields are typically used to display related information that provides context or additional details for the selected value.
Lets take an example of school record , the moment school admin is selecting student roll no from F4 help ,student name should also get populated in field student name along with field roll number.
Lets see additional binding example in our travel application , where when user is making booking and selecting travel status ( O , A , X ) its relevant description should also get fetched from database field "Remark" should get updated automatically.
We need to add below annotation on field status on CDS level .
@Consumption.valueHelpDefinition: [{? entity: {name: 'entityRef', element: 'elementRef},
? additionalBinding: [{? element: 'targetfieldincds', localElement: 'sourcefieldfromf4cds' }] }]??
Where highlighted element is target field of CDS i.e. Description field
LocalElement : is field is coming from source/F4 CDS
lets see the behaviour on UI.
We will F4 on field Status.
If user selects O then Remarks would be updated as open , Approved in case user selects status A.
Field Remark auto populated with Open as user selects O as status.
We can generate or map multiple additional field as part of additional binding.
Software Engineer - ABAP | ODATA - CDS | Embedded and Analytics Apps Fiori | RAP
1 年Good job!! easy to follow and apply!
SAP DEVELOPER SPECIALIST na NTT DATA Business Solutions Brazil
1 年Tks for sharing